步驟 3:設定 Firebase Remote Config,顯示特定廣告體驗
| 簡介: 使用 AdMob、Google Analytics 和 Firebase 最佳化混合型營利 |
| 步驟 1: 使用 AdMob 建立新的廣告單元,用於顯示廣告 |
| 步驟 2: 設定 Google Analytics |
|
步驟 3: 設定 Firebase Remote Config,顯示特定廣告體驗 |
在最後一個步驟中,您已瞭解 Google Analytics 目標對象。在本步驟中,您將建立 Remote Config 布林值控制的參數 (稱為 ad_control_switch),並運用「購買者」目標對象。接著,請在應用程式的程式碼中加入邏輯,根據該參數的值決定應用程式應顯示的內容。
在 Firebase 控制台中設定 Remote Config 參數和條件
在 Firebase 控制台中,開啟 Firebase 專案。
在左側窗格中展開「參與」Engage部分,然後選取「遠端設定」Remote Config。
按一下「建立設定」 (或「新增參數」,如果您先前使用過 Remote Config)。
在「建立參數」面板中,完成下列步驟:
在「參數名稱」欄位中輸入
ad_control_switch。在
Data type下拉式選單中,選取「布林值」。按一下「建立新條件」,然後選取「建立新條件」。
在「定義新條件」對話方塊中,完成下列步驟:
在「Name」(名稱) 欄位中輸入
Purchasers Group(或條件的任何其他容易辨識的名稱)。在「適用條件」下拉式選單中,選取「使用者目標對象」。
在「選取目標對象」下拉式選單中,選取「購買者」。
按一下 [Save Condition]。
返回「建立參數」面板,完成下列步驟:
在「購買者群組」的「值」部分,選取「false」。
在「預設值」中,選取「true」。
依序點按「儲存」和「發布變更」。
這項設定會檢查使用者是否屬於「購買者」目標對象 (也就是付費使用者):
如果使用者屬於「購買者」目標對象,則 Remote Config 會傳回
ad_control_switch參數的false值。如果使用者不在「購買者」目標對象中,則 Remote Config 會為
ad_control_switch參數傳回true的值。
在後續步驟中,您將在應用程式中實作 Remote Config,以處理這些參數值。
在應用程式中新增 Remote Config SDK
在應用程式程式碼中使用 Remote Config 前,請先將 Remote Config SDK 新增至應用程式的程式碼庫。請注意,您的應用程式應已具備 Google Mobile Ads (AdMob) SDK 和 Google Analytics for Firebase SDK,這是本教學課程先前步驟的內容。
Swift
在 Podfile 中新增及安裝 Remote Config pod:
pod 'Firebase/RemoteConfig'
Android
在 build.gradle 檔案中加入 Remote Config 程式庫依附元件:
implementation 'com.google.firebase:firebase-config:23.1.0'
Flutter
在 Flutter 專案的根目錄中,執行下列指令來安裝 Remote Config 外掛程式:
flutter pub add firebase_remote_config
Unity
下載並安裝最新版 Firebase Unity SDK,然後將 Remote Config 封裝新增至專案:
FirebaseRemoteConfig.unitypackage
設定 Remote Config 執行個體
如要讓應用程式使用 Remote Config 參數值,請設定 Remote Config 執行個體,以便為用戶端應用程式執行個體擷取新值。
在本例中,Remote Config 已設為每小時檢查是否有新的參數值。
Swift
remoteConfig = RemoteConfig.remoteConfig()
let settings = RemoteConfigSettings()
settings.minimumFetchInterval = 3600
remoteConfig.configSettings = settings
Kotlin
remoteConfig = Firebase.remoteConfig
val configSettings = remoteConfigSettings {
minimumFetchIntervalInSeconds = 3600
}
remoteConfig.setConfigSettingsAsync(configSettings)
Java
mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder()
.setMinimumFetchIntervalInSeconds(3600)
.build();
mFirebaseRemoteConfig.setConfigSettingsAsync(configSettings);
Flutter
remoteConfig = FirebaseRemoteConfig.instance;
final configSettings = FirebaseRemoteConfigSettings(
minimumFetchInterval: Duration(hours: 1),
);
await remoteConfig.setConfigSettings(configSettings);
// Use the `onConfigUpdated` callback to listen for changes to the config settings.
remoteConfig.onConfigUpdated.listen((_) {
print('Config settings confirmed');
});
Unity
var remoteConfig = FirebaseRemoteConfig.DefaultInstance;
var configSettings = new ConfigSettings {
MinimumFetchInternalInMilliseconds =
(ulong)(new TimeSpan(1, 0, 0).TotalMilliseconds)
};
remoteConfig.SetConfigSettingsAsync(configSettings)
.ContinueWithOnMainThread(task => {
Debug.Log("Config settings confirmed");
}
擷取並啟用 Remote Config
擷取並啟用 Remote Config 參數,應用程式就會開始使用新的參數值。
請盡早在應用程式的載入階段進行此呼叫,因為這是非同步呼叫,您需預先擷取 Remote Config 值,應用程式才知道是否要顯示廣告。
Swift
remoteConfig.fetch() { (status, error) -> Void in
if status == .success {
print("Config fetched!")
self.remoteConfig.activate() { (changed, error) in
// ...
}
} else {
print("Config not fetched")
print("Error: \(error?.localizedDescription ?? "No error available.")")
}
self.loadAdUnit()
}
Kotlin
remoteConfig.fetchAndActivate()
.addOnCompleteListener(this) { task ->
if (task.isSuccessful) {
val updated = task.result
Log.d(TAG, "Config params updated: $updated")
} else {
Log.d(TAG, "Config params failed to update")
}
loadAdUnit()
}
Java
mFirebaseRemoteConfig.fetchAndActivate()
.addOnCompleteListener(this, new OnCompleteListener<Boolean>() {
@Override
public void onComplete(@NonNull Task<Boolean> task) {
if (task.isSuccessful()) {
boolean updated = task.getResult();
Log.d(TAG, "Config params updated: " + updated);
} else {
Log.d(TAG, "Config params failed to update");
}
loadAdUnit();
}
});
Flutter
remoteConfig = FirebaseRemoteConfig.instance;
// Fetch and activate the latest Remote Config values.
final updated = await remoteConfig.fetchAndActivate();
// Check if the config params were updated successfully.
if (updated) {
print('Config params updated');
} else {
print('Config params failed to update');
}
// Load the ad unit.
_loadAdUnit();
Unity
remoteConfig.FetchAndActivateAsync().ContinueWithOnMainThread(task => {
if (task.IsFaulted) {
Debug.LogWarning("Config params failed to update");
} else {
Debug.Log("Config params updated: " + task.Result);
}
LoadAdUnit();
});
應用程式現在已設定完畢,可以處理您在本步驟中建立的 Remote Config 參數。
使用 Remote Config 參數值
在 loadAdUnit() 函式中使用預先擷取的 Remote Config 值,判斷應用程式執行個體是否應執行下列其中一項操作:
ad_control_switch參數值會解析為true:顯示中繼插頁式廣告 (因為使用者不是付費使用者)。ad_control_switch參數值會解析為false:不要顯示廣告 (因為使用者是付費使用者)。
Swift
private func loadAdUnit() {
let showAds = remoteConfig["ad_control_switch"].boolValue
if showAds {
// Load interstitial ad (implemented ad unit)
// per AdMob instructions (the first step of this tutorial).
} else {
// Don't show ads.
}
}
Kotlin
private fun loadAdUnit() {
var showAds = remoteConfig.getBoolean(ad_control_switch)
if (showAds) {
// Load interstitial ad (implemented ad unit)
// per AdMob instructions (the first step of this tutorial).
} else {
// Don't show ads.
}
}
Java
private void loadAdUnit() {
boolean showAds =
mFirebaseRemoteConfig.getBoolean(ad_control_switch);
if (showAds) {
// Load interstitial ad (implemented ad unit)
// per AdMob instructions (the first step of this tutorial).
} else {
// Don't show ads.
}
}
Flutter
void _loadAdUnit() {
bool showAds = remoteConfig.getBool(ad_control_switch);
if (showAds) {
// Load interstitial ad (implemented ad unit)
// per AdMob instructions (the first step of this tutorial).
} else {
// Don't show ads.
}
}
Unity
void LoadAdUnit() {
bool showAds =
remoteConfig.GetValue("ad_control_switch").BooleanValue;
if (showAds) {
// Load interstitial ad (implemented ad unit)
// per AdMob instructions (the first step of this tutorial).
} else {
// Don't show ads.
}
}
發布應用程式
由於是否顯示廣告的邏輯位於程式碼集內,因此您需要發布含有這項邏輯的新版應用程式。
如果您已按照本教學課程的步驟操作,應用程式應會立即開始向使用者放送自訂應用程式內廣告。您可以在 AdMob 帳戶和 Google Analytics 資訊主頁 (Firebase 控制台或 Google Analytics 使用者介面) 中,監控廣告收益。
這樣就完成了!您已完成本教學課程,瞭解如何使用 AdMob、Google Analytics 和 Firebase,盡量提高混合式營利效益。
相關資源
查看其他解決方案指南:
觀看一系列影片:運用 Firebase 和AdMob