第 3 步:處理應用代碼中的遠程配置參數值
簡介:使用 Firebase 優化 AdMob 廣告頻率 |
第 1 步:使用 AdMob 創建新的廣告單元變體以進行測試 |
第 2 步:在 Firebase 控制台中設置 A/B 測試 |
第 3 步:處理應用代碼中的遠程配置參數值 |
第 4 步:開始 A/B 測試並在 Firebase 控制台中查看測試結果 |
第 5 步:決定是否推出新的廣告格式 |
在最後一步結束時,您創建了一個遠程配置參數 ( INTERSTITIAL_AD_KEY
)。在此步驟中,您將根據該參數的值將邏輯添加到您的應用程序代碼中,以了解您的應用程序應顯示的內容。
添加所需的 SDK
在您的應用程序代碼中使用 Remote Config 之前,請將 Remote Config SDK 和 Firebase SDK for Google Analytics 添加到您的項目構建文件中。
迅速
在您的 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'
Java
將以下庫依賴項添加到build.gradle
文件中:
implementation 'com.google.android.gms:play-services-ads:22.4.0'
implementation 'com.google.firebase:firebase-analytics:21.3.0'
implementation 'com.google.firebase:firebase-config:21.4.1'
Kotlin+KTX
將以下庫依賴項添加到build.gradle
文件中:
implementation 'com.google.android.gms:play-services-ads:22.4.0'
implementation 'com.google.firebase:firebase-analytics-ktx:21.3.0'
implementation 'com.google.firebase:firebase-config-ktx:21.4.1'
統一
下載並安裝 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");
}
獲取並激活遠程配置
獲取並激活遠程配置參數,以便它可以開始使用新的參數值。
您需要在應用的加載階段儘早進行此調用,因為此調用是異步的,您需要預先獲取 Remote Config 值,以便您的應用知道要展示哪個廣告。
迅速
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 測試並查看測試結果