Hướng dẫn: Thử nghiệm việc sử dụng các định dạng quảng cáo mới của AdMob

Bước 3: Xử lý giá trị tham số Remote Config trong mã của ứng dụng



Ở cuối bước cuối cùng, bạn đã tạo một thông số Remote Config (SHOW_NEW_AD_KEY). Trong bước này, bạn sẽ thêm logic vào mã của ứng dụng để ứng dụng hiển thị nội dung dựa trên giá trị của thông số đó – true (hiển thị quảng cáo mới) so với false (không hiển thị quảng cáo mới).

Thêm các SDK bắt buộc

Trước khi sử dụng Remote Config trong mã ứng dụng, hãy thêm cả SDK Remote Config và SDK Firebase cho Google Analytics vào các tệp bản dựng dự án.

Thêm và cài đặt các nhóm sau trong podfile của bạn:

pod 'Google-Mobile-Ads-SDK'
pod 'Firebase/Analytics'
pod 'Firebase/RemoteConfig'

Thêm các phần phụ thuộc thư viện sau vào tệp build.gradle:

implementation 'com.google.android.gms:play-services-ads:24.0.0'
implementation 'com.google.firebase:firebase-analytics:22.3.0'
implementation 'com.google.firebase:firebase-config:22.1.0'

Tải và cài đặt SDK Firebase cho Unity, sau đó thêm các gói Unity sau vào dự án của bạn:

  • FirebaseAnalytics.unitypackage
  • FirebaseRemoteConfig.unitypackage

Định cấu hình thực thể Remote Config

Để sử dụng các giá trị tham số Remote Config, hãy định cấu hình bản sao Remote Config sao cho bản sao đó có thể tìm nạp các giá trị mới cho bản sao ứng dụng của khách hàng.

Trong ví dụ này, Remote Config được định cấu hình để kiểm tra các giá trị tham số mới mỗi giờ một lần.

remoteConfig = RemoteConfig.remoteConfig()
let settings = RemoteConfigSettings()
settings.minimumFetchInterval = 3600
remoteConfig.configSettings = settings
self.remoteConfig = [FIRRemoteConfig remoteConfig];
FIRRemoteConfigSettings *remoteConfigSettings = [[FIRRemoteConfigSettings alloc] init];
remoteConfigSettings.minimumFetchInterval = 3600;
self.remoteConfig.configSettings = remoteConfigSettings;
mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder()
        .setMinimumFetchIntervalInSeconds(3600)
        .build();
mFirebaseRemoteConfig.setConfigSettingsAsync(configSettings);
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");
}

Tìm nạp và kích hoạt Remote Config

Tìm nạp và kích hoạt các tham số Remote Config để có thể bắt đầu sử dụng các giá trị tham số mới.

Bạn cần thực hiện lệnh gọi này càng sớm càng tốt trong giai đoạn tải của ứng dụng vì lệnh gọi này không đồng bộ và bạn sẽ cần tìm nạp trước giá trị Remote Config để ứng dụng biết có cần hiển thị quảng cáo hay không.

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()
}
[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];
}];
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();
            }
        });
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();
});

Giờ đây, ứng dụng của bạn đã sẵn sàng xử lý thông số Remote Config mà bạn đã tạo trong quá trình thiết lập thử nghiệm A/B ở phần trước của hướng dẫn này.

Sử dụng giá trị thông số Remote Config

Sử dụng giá trị Remote Config được tìm nạp trước trong hàm loadAdUnit() để xác định xem phiên bản ứng dụng có nên hiển thị (giá trị tham số là true) hay không hiển thị (giá trị tham số là false) đơn vị quảng cáo xen kẽ có tặng thưởng mới.

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.
  }
}
- (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.
    }
}
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.
    }
}
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.
  }
}

Thêm các bước kiểm tra khác cho giá trị thông số

Bạn cũng cần kiểm tra giá trị của thông số Remote Config này tại các khu vực khác của mã xử lý ứng dụng để cho biết trải nghiệm quảng cáo nào sẽ được tải. Ví dụ: bạn có thể quyết định xem có tải lại một quảng cáo sau khi người dùng xem xong quảng cáo hiện tại không.

Trước tiên, bạn cần thực hiện lệnh gọi tìm nạp và kích hoạt để biết mọi thay đổi về giá trị thông số, ví dụ: nếu bạn quyết định chấm dứt hoặc tạo một thử nghiệm mới.

Sau đó, bạn luôn có thể kiểm tra giá trị của thông số bằng cách sử dụng những lệnh gọi sau:

remoteConfig["showNewAdKey"].boolValue
self.remoteConfig[@"SHOW_NEW_AD_KEY"].boolValue;
mFirebaseRemoteConfig.getBoolean(SHOW_NEW_AD_KEY)
remoteConfig.getBoolean(SHOW_NEW_AD_KEY)
remoteConfig.GetValue("SHOW_NEW_AD_KEY").BooleanValue

Những lệnh gọi này sẽ luôn trả về cùng một giá trị cho một bản sao ứng dụng tuỳ thuộc vào việc bản sao này được đặt trong nhóm đối chứng hay nhóm biến thể quảng cáo mới, ngoại trừ trường hợp có bất kỳ thay đổi nào trong bảng điều khiển của Firebase mà đã được tìm nạp và kích hoạt trong các lệnh gọi trước đó.




Bước 2: Thiết lập thử nghiệm A/B trong bảng điều khiển Firebase Bước 4: Bắt đầu thử nghiệm A/B và xem xét kết quả thử nghiệm