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ị thông số Cấu hình từ xa trong mã ứng dụng


Giới thiệu: Thử nghiệm việc sử dụng định dạng quảng cáo AdMob mới bằng Firebase
Bước 1: 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 của Firebase

Bước 3: Xử lý giá trị thông số Cấu hình từ xa 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 của 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ố Cấu hình từ xa (SHOW_NEW_AD_KEY). Trong bước này, bạn sẽ thêm logic vào mã của ứng dụng để biết ứng dụng sẽ hiển thị nội dung gì 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 Cấu hình từ xa trong mã xử lý ứng dụng, hãy thêm cả SDK Cấu hình từ xa và Firebase SDK cho Google Analytics vào tệp bản dựng dự á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.1.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 các gói Unity sau vào dự án:

  • FirebaseAnalytics.unitypackage
  • FirebaseRemoteConfig.unitypackage

Định cấu hình bản sao Cấu hình từ xa

Để sử dụng các giá trị thông số Cấu hình từ xa, hãy định cấu hình bản sao Cấu hình từ xa sao cho bản sao này có thể 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ị thông số mới 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 Cấu hình từ xa

Tìm nạp và kích hoạt các thông số Cấu hình từ xa để Cấu hình từ xa 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 cần tìm nạp trước giá trị Cấu hình từ xa để ứng dụng biết liệu 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ý thông 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 đó của hướng dẫn này.

Sử dụng giá trị thông số Cấu hình từ xa

Sử dụng giá trị Cấu hình từ xa đã tìm nạp trước trong hàm loadAdUnit() để xác định xem bản sao ứng dụng sẽ hiển thị (giá trị thông số 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ó những vùng khác trong mã xử lý ứng dụng mà bạn sẽ cần kiểm tra giá trị của thông số Cấu hình từ xa này để 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 hay không.

Trước tiên, bạn nên thực hiện lệnh gọi tìm nạp và kích hoạt để nhận mọi thay đổi 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ị cho tham 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 bản sao ứng dụng, tuỳ thuộc vào việc bản sao đó được đặt trong nhóm đối chứng hay nhóm biến thể quảng cáo mới, trừ phi có bất kỳ thay đổi nào trong bảng điều khiển của 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 của Firebase Bước 4: Bắt đầu thử nghiệm A/B và xem xét kết quả thử nghiệm