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ý các giá trị tham số Remote Config trong mã của ứng dụng


Giới thiệu: Thử nghiệm áp dụng định dạng quảng cáo AdMob mới trong Firebase
Bước 1: Mục đích sử dụng AdMob để tạo một biến thể đơn vị quảng cáo mới cho mục đích thử nghiệm
Bước 2: Thiết lập thử nghiệm A/B trong bảng điều khiển Firebase

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

Bước 4: Bắt đầu thử nghiệm A/B và xem xét kết quả thử nghiệm trong bảng điều khiển Firebase
Bước 5: Quyết định có triển khai định dạng quảng cáo mới hay không


Ở cuối bước cuối cùng, bạn đã tạo một thông số Remote Config (SHOW_NEW_AD_KEY). Ở bước này, bạn sẽ thêm logic vào mã của ứng dụng cho nội dung mà ứng dụng của bạn sẽ cho thấy dựa trên giá trị của tham 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ã xử lý ứng dụng, hãy thêm cả hai thuộc tính SDK Remote Config và Firebase SDK cho Google Analytics để các tệp bản dựng dự án của bạn.

Nền tảng của Apple

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

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

Android

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

implementation 'com.google.android.gms:play-services-ads:23.3.0'
implementation 'com.google.firebase:firebase-analytics:22.0.2'
implementation 'com.google.firebase:firebase-config:22.0.0'

Unity

Tải xuống và cài đặt Firebase Unity SDK, sau đó thêm Unity sau đây 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 thực thể Remote Config để có thể tìm nạp các giá trị mới cho thuộc tính phiên bản ứng dụng khách.

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

Swift

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)

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");
}

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ị thông số mới.

Bạn nê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 giá trị Remote Config tìm nạp trước để ứng dụng của bạn biết có hiển thị quảng cáo hay không.

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

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

Unity

remoteConfig.FetchAndActivateAsync().ContinueWithOnMainThread(task => {
  if (task.IsFaulted) {
    Debug.LogWarning("Config params failed to update");
  } else {
    Debug.Log("Config params updated: " + task.Result);
  }
  LoadAdUnit();
});

Ứng dụng của bạn hiện đã sẵn sàng xử lý tham số Remote Config mà bạn đã tạo trong quá trình thiết lập thử nghiệm A/B 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 sẽ hiển thị (giá trị tham số của true) hay không hiển thị (giá trị thông số của false) đơn vị quảng cáo xen kẽ có tặng thưởng mới.

Swift

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

Unity

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ố

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

Bạn phải thực hiện lệnh gọi tìm nạp và kích hoạt trước để nhận giá trị thông số bất kỳ thay đổi — ví dụ: nếu bạn quyết định kết thúc hoặc tạo một thử nghiệm mới.

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

Swift

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)

Unity

remoteConfig.GetValue("SHOW_NEW_AD_KEY").BooleanValue

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




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