查看 2022 年 Google I/O 大会上介绍的 Firebase 新动态。了解详情

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

Bước 4: Bắt đầu kiểm tra A / B và xem lại kết quả kiểm tra 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 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ã của ứng dụng để biết những gì ứ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 các SDK bắt buộc

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 xây dựng dự án của bạn.

Nhanh

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'

Objective-C

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'

Java

Thêm các 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:22.4.0'
implementation 'com.google.firebase:firebase-analytics:21.3.0'
implementation 'com.google.firebase:firebase-config:21.4.1'

Kotlin+KTX

Thêm các 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:22.4.0'
implementation 'com.google.firebase:firebase-analytics-ktx:21.3.0'
implementation 'com.google.firebase:firebase-config-ktx:21.4.1'

Đoàn kết

Tải xuống và cài đặt SDK Firebase 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 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 định cấu hình phiên bản Cấu hình từ xa để nó được thiết lập để 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

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)

Đ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 của mình 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()
}

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

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

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

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 sẽ hiển thị (giá trị thông số là true ) hay không hiển thị (giá trị thông số là 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.
  }
}

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

Đ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 của bạn, nơi bạn sẽ cần kiểm tra giá trị của thông số Cấu hình từ xa này để chỉ đị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.

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

Nhanh

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)

Đoàn kết

remoteConfig.GetValue("SHOW_NEW_AD_KEY").BooleanValue

Các lệnh gọi này sẽ luôn trả lại cùng một giá trị cho một phiên bản ứng dụng tùy thuộc vào việc nó được đặt trong nhóm kiểm soát hay nhóm biến thể quảng cáo mới, trừ khi 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 kiểm tra A / B trong bảng điều khiển Firebase Bước 4 : Bắt đầu kiểm tra A / B và xem xét kết quả kiểm tra