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

Bước 3: Xử lý các giá trị tham số Cấu hình từ xa trong mã ứng dụng của bạn


Giới thiệu: Thử nghiệm việc áp dụng định dạng quảng cáo AdMob mới bằng Firebase
Bước 1: Sử dụng AdMob để tạo biến thể đơn vị quảng cáo mới để 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ố Cấu hình từ xa trong mã ứng dụng của bạn

Bước 4: Bắt đầu thử nghiệm A/B và xem lại kết quả thử nghiệm trong bảng điều khiển Firebase
Bước 5: Quyết định có nên 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 tham số Cấu hình từ xa ( SHOW_NEW_AD_KEY ). Trong bước này, bạn sẽ thêm logic vào mã ứng dụng để biết ứng dụng của bạn sẽ hiển thị 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 SDK cần thiết

Trước khi sử dụng Cấu hình từ xa trong mã ứng dụng của bạn, hãy thêm cả SDK cấu hình từ xa và SDK Firebase cho Google Analytics vào 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 vào podfile 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 vào tệp build.gradle của bạn:

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'

Đoàn kết

Tải xuống và cài đặt Firebase Unity SDK, 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 phiên bản Cấu hình từ xa

Để sử dụng các giá trị tham số Cấu hình từ xa, hãy đặt cấu hình phiên bản Cấu hình từ xa để nó được thiết lập nhằm tìm nạp các giá trị mới cho phiên bản ứng dụng khách.

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

Nhanh

remoteConfig = RemoteConfig.remoteConfig()
let settings = RemoteConfigSettings()
settings.minimumFetchInterval = 3600
remoteConfig.configSettings = settings

Mục tiêu-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)

Đoàn kết

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 Cấu hình từ xa

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

Bạn sẽ muốn thực hiện lệnh gọi này sớm nhất có thể trong giai đoạn tải ứ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ị Cấu hình từ xa để ứng dụng của bạn biết có hiển thị quảng cáo hay không.

Nhanh

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

Mục tiêu-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()
        }

Đoàn kết

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ố Cấu hình từ xa mà bạn đã tạo trong quá trình thiết lập thử nghiệm A/B trước đó trong hướng dẫn này.

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

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

Nhanh

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

Mục tiêu-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.
    }
}

Đoàn kết

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 kiểm tra khác cho giá trị tham số

Có những khu vực khác trong mã ứng dụng mà bạn cần kiểm tra giá trị của thông số Cấu hình từ xa này để xác định trải nghiệm quảng cáo nào sẽ được tải. Ví dụ: bạn có thể quyết định có tải lại quảng cáo sau khi người dùng xem xong quảng cáo hiện tại hay không.

Trước tiên, bạn phải thực hiện lệnh gọi tìm nạp và kích hoạt để nhận bất kỳ thay đổi nào về giá trị tham số — 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 tham số bằng các lệnh gọi sau:

Nhanh

remoteConfig["showNewAdKey"].boolValue

Mục tiêu-C

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

Java

mFirebaseRemoteConfig.getBoolean(SHOW_NEW_AD_KEY)

Kotlin+KTX

remoteConfig.getBoolean(SHOW_NEW_AD_KEY)

Đoàn kết

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 tùy thuộc vào việc phiên bản đó được đặt trong nhóm kiểm soát hay nhóm biến thể quảng cáo mới, trừ khi có bất kỳ thay đổi nào được thực hiện trong bảng điều khiển Firebase đã đượ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