Catch up on highlights from Firebase at Google I/O 2023. Learn more

教程:測試採用新的 AdMob 廣告格式

第 3 步:處理應用代碼中的遠程配置參數值


簡介:使用 Firebase 測試採用新的 AdMob 廣告格式
第 1 步:使用 AdMob 創建新的廣告單元變體進行測試
第 2 步:在 Firebase 控制台中設置 A/B 測試

第 3 步:處理應用代碼中的遠程配置參數值

第 4 步:開始 A/B 測試並在 Firebase 控制台中查看測試結果
第 5 步:決定是否推出新的廣告格式


在最後一步結束時,您創建了一個遠程配置參數 ( SHOW_NEW_AD_KEY )。在此步驟中,您將根據該參數的值將邏輯添加到您的應用程序代碼中,以確定您的應用程序應顯示的內容 - true (顯示新廣告)與false顯示新廣告)。

添加所需的 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.1.0'
implementation 'com.google.firebase:firebase-analytics:21.3.0'
implementation 'com.google.firebase:firebase-config:21.4.0'

Kotlin+KTX

將以下庫依賴項添加到build.gradle文件中:

implementation 'com.google.android.gms:play-services-ads:22.1.0'
implementation 'com.google.firebase:firebase-analytics-ktx:21.3.0'
implementation 'com.google.firebase:firebase-config-ktx:21.4.0'

統一

下載並安裝 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()函數中預取的遠程配置值來確定應用實例是否應該顯示(參數值為true )或顯示(參數值為false )新的激勵插頁式廣告單元。

迅速

private func loadAdUnit() {
  let showNewAdFormat = remoteConfig["users"].boolValue
  if showNewAdFormat {
      // Load Rewarded Interstitial Ad.
      // This should load your new implemented ad unit
      // as per AdMob instructions (the first step of this tutorial).
  } else {
    // Show the existing ad unit.
  }
}

Objective-C

- (void)loadAdUnit {
    BOOL showAds = self.remoteConfig[@"SHOW_NEW_AD_KEY"].boolValue;
    if (showAds) {
      // Load Rewarded Interstitial Ad.
      // This should load your new implemented ad unit
      // per AdMob instructions (the first step of this tutorial).
    } else {
      // Show the existing ad unit.
    }
}

Java

private void loadAdUnit() {
    boolean showNewAdFormat =
      mFirebaseRemoteConfig.getBoolean(SHOW_NEW_AD_KEY);

    if (showNewAdFormat) {
      // Load Rewarded Interstitial Ad.
      // This should load your new implemented ad unit
      // per AdMob instructions (the first step of this tutorial).
    } else {
      // Show the existing ad unit.
    }
}

Kotlin+KTX

private fun loadAdUnit() {
  var showNewAdFormat = remoteConfig.getBoolean(SHOW_NEW_AD_KEY)

  if (showNewAdFormat) {
      // Load Rewarded Interstitial Ad.
      // This should load your new implemented ad unit
      // per AdMob instructions (the first step of this tutorial).
    } else {
      // Show the existing ad unit.
    }
}

統一

void LoadAdUnit() {
  bool showNewAdFormat =
      remoteConfig.GetValue("SHOW_NEW_AD_KEY").BooleanValue;

  if (showNewAdFormat) {
    // Load Rewarded Interstitial Ad (new implemented ad unit)
    // per AdMob instructions (the first step of this tutorial).
  } else {
    // Show the existing ad unit.
  }
}

為參數值添加其他檢查

在您的應用程序代碼中的其他區域,您需要檢查此遠程配置參數的值,以指示將加載哪種廣告體驗。例如,您可以決定是否在用戶查看完當前廣告後重新加載廣告。

應該首先進行 fetch 和 activate 調用以獲取任何參數值更改 - 例如,如果您決定結束或創建新實驗。

從那裡,您始終可以使用以下調用檢查參數的值:

迅速

remoteConfig["showNewAdKey"].boolValue

Objective-C

self.remoteConfig[@"SHOW_NEW_AD_KEY"].boolValue;

Java

mFirebaseRemoteConfig.getBoolean(SHOW_NEW_AD_KEY)

Kotlin+KTX

remoteConfig.getBoolean(SHOW_NEW_AD_KEY)

統一

remoteConfig.GetValue("SHOW_NEW_AD_KEY").BooleanValue

這些調用將始終為應用實例返回相同的值,具體取決於應用實例是放在對照組還是新的廣告變體組中,除非在之前調用中獲取並激活的 Firebase 控制台中進行了任何更改。




第 2 步:在 Firebase 控制台中設置 A/B 測試第 4 步:開始 A/B 測試並查看測試結果