步驟 3:處理應用程式碼中的遠端設定參數值
簡介:使用 Firebase 最佳化 AdMob 廣告頻率 |
步驟 1:使用 AdMob 建立新的廣告單元變體進行測試 |
步驟 2:在 Firebase 控制台中設定 A/B 測試 |
步驟 3:處理應用程式碼中的遠端設定參數值 |
步驟 4:啟動 A/B 測試並在 Firebase 控制台中檢視測試結果 |
第 5 步:決定是否推出新的廣告格式 |
在最後一步結束時,您建立了一個遠端配置參數 ( INTERSTITIAL_AD_KEY
)。在此步驟中,您將向應用程式程式碼新增邏輯,以便應用程式應根據該參數的值顯示內容。
添加所需的 SDK
在應用程式程式碼中使用遠端設定之前,請將遠端設定 SDK 和適用於 Google Analytics 的 Firebase SDK 新增至專案建置檔案。
迅速
在 podfile 中新增並安裝以下 pod:
pod 'Google-Mobile-Ads-SDK'
pod 'Firebase/Analytics'
pod 'Firebase/RemoteConfig'
Objective-C
在 podfile 中新增並安裝以下 pod:
pod 'Google-Mobile-Ads-SDK'
pod 'Firebase/Analytics'
pod 'Firebase/RemoteConfig'
安卓
將以下庫依賴項新增至您的build.gradle
檔案:
implementation 'com.google.android.gms:play-services-ads:23.0.0'
implementation 'com.google.firebase:firebase-analytics:21.6.1'
implementation 'com.google.firebase:firebase-config:21.6.3'
統一
下載並安裝 Firebase Unity SDK,然後將以下 Unity 套件新增到您的專案中:
-
FirebaseAnalytics.unitypackage
-
FirebaseRemoteConfig.unitypackage
配置遠端配置實例
若要使用遠端配置參數值,請配置遠端配置實例,以便將其設定為為客戶端應用程式實例以取得新值。
在此範例中,遠端配置配置為每小時檢查一次新參數值。
迅速
remoteConfig = RemoteConfig.remoteConfig()
let settings = RemoteConfigSettings()
settings.minimumFetchInterval = 3600
remoteConfig.configSettings = settings
Objective-C
self.remoteConfig = [FIRRemoteConfig remoteConfig];
FIRRemoteConfigSettings *remoteConfigSettings = [[FIRRemoteConfigSettings alloc] init];
remoteConfigSettings.minimumFetchInterval = 3600;
self.remoteConfig.configSettings = remoteConfigSettings;
Java
mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder()
.setMinimumFetchIntervalInSeconds(3600)
.build();
mFirebaseRemoteConfig.setConfigSettingsAsync(configSettings);
Kotlin+KTX
remoteConfig = Firebase.remoteConfig
val configSettings = remoteConfigSettings {
minimumFetchIntervalInSeconds = 3600
}
remoteConfig.setConfigSettingsAsync(configSettings)
統一
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");
}
取得並啟動遠端配置
取得並啟動遠端配置參數,以便它可以開始使用新的參數值。
您需要在應用程式的載入階段儘早進行此調用,因為此調用是異步的,並且您需要預先獲取遠端配置值,以便您的應用程式知道要顯示哪個廣告。
迅速
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()
}
Objective-C
[self.remoteConfig fetchWithCompletionHandler:^(FIRRemoteConfigFetchStatus status, NSError *error) {
if (status == FIRRemoteConfigFetchStatusSuccess) {
NSLog(@"Config fetched!");
[self.remoteConfig activateWithCompletion:^(BOOL changed, NSError * _Nullable error) {
// ...
}];
} else {
NSLog(@"Config not fetched");
NSLog(@"Error %@", error.localizedDescription);
}
[self 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();
}
});
Kotlin+KTX
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()
}
統一
remoteConfig.FetchAndActivateAsync().ContinueWithOnMainThread(task => {
if (task.IsFaulted) {
Debug.LogWarning("Config params failed to update");
} else {
Debug.Log("Config params updated: " + task.Result);
}
LoadAdUnit();
});
您的應用程式現在已準備好處理您在本教程前面設定的 A/B 測試期間建立的遠端設定參數。
使用遠端配置參數值
使用loadAdUnit()
函數中預先設定的遠端設定值來決定應為此應用程式實例顯示哪種廣告頻率變體。
迅速
private func loadAdUnit() {
let adUnitId = remoteConfig["INTERSTITIAL_AD_KEY"].stringValue;
let request = GADRequest()
GADInterstitialAd.load(withAdUnitID: adUnitId,
request: request,
completionHandler: { [self] ad, error in
if let error = error {
print("Failed to load: \(error.localizedDescription)")
return
}
interstitial = ad
// Register for callbacks.
}
)
}
// Register for callbacks.
Objective-C
- (void)loadAdUnit {
NSString *adUnitId =
self.remoteConfig[@"INTERSTITIAL_AD_KEY"].stringValue;
GADRequest *request = [GADRequest request];
[GADInterstitialAd loadAdWithAdUnitId:adUnitId
request:request
completionHandler:^(GADInterstitialAd *ad,
NSError *error) {
if (error) {
NSLog(@"Failed to load interstitial ad with error: %@",
[error localizedDescription]);
return;
}
self.interstitial = ad;
}];
}
Java
private void loadAdUnit() {
String adUnitId =
mFirebaseRemoteConfig.getString("INTERSTITIAL_AD_KEY");
// Load Interstitial Ad (assume adUnitId not null)
AdRequest adRequest = new AdRequest.Builder().build();
InterstitialAd.load(this, adUnitId, adRequest, new
InterstitialAdLoadCallback() {
@Override
public void onAdLoaded(@NonNull InterstitialAd intertitialAd) {
mInterstitialAd = interstitialAd;
}
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
mInterstitialAd = null;
}
});
}
Kotlin+KTX
private fun loadAdUnit() {
String adUnitId = remoteConfig.getString("INTERSTITIAL_AD_KEY")
var adRequest = AdRequestBuilder.Builder().build()
AdRequestBuilder.load(this, adUnitId, adRequest, object :
InterstitialAdLoadCallback() {
override fun onAdFailedToLoad(adError: LoadAdError) {
mInterstitialAd = null
}
override fun onAdLoaded(interstitialAd: InterstitialAd) {
mInterstitialAd = interstitialAd
}
})
}
統一
void LoadAdUnit() {
// Note that you may want to encode and parse two sets of ad unit IDs for
// Android / iOS in the Unity implementation.
String adUnitId = remoteConfig.GetValue("INTERSTITIAL_AD_KEY").StringValue;
this.interstitial = new InterstitialAd(adUnitId);
}
新增對參數值的其他檢查
您的應用程式程式碼中還有其他區域需要檢查此遠端配置參數的值,以指示將載入哪種廣告體驗。例如,您可以決定在使用者看完目前廣告後是否重新載入廣告。
應先進行 fetch 和 activate 呼叫以取得任何參數值變更 - 例如,如果您決定結束或建立新實驗。
從那裡,您始終可以使用以下呼叫檢查參數的值:
迅速
remoteConfig["INTERSTITIAL_AD_KEY"].stringValue
Objective-C
self.remoteConfig[@"INTERSTITIAL_AD_KEY"].stringValue;
Java
mFirebaseRemoteConfig.getString(INTERSTITIAL_AD_KEY)
Kotlin+KTX
remoteConfig.getString(INTERSTITIAL_AD_KEY)
統一
remoteConfig.GetValue("INTERSTITIAL_AD_KEY").StringValue
這些呼叫將始終為應用程式實例傳回相同的值,具體取決於應用程式實例是放置在控制組還是新的廣告變體組之一中,除非在Firebase 控制台中進行了在先前的呼叫中取得並啟動的任何更改。
步驟 2 :在 Firebase 控制台中設定 A/B 測試步驟 4 :啟動 A/B 測試並檢視測試結果