מדריך: בדיקת ההטמעה של פורמטים חדשים של מודעות ב-AdMob

שלב 3: טיפול בערכי הפרמטרים של Remote Config בקוד של האפליקציה



בסוף השלב האחרון יצרתם פרמטר Remote Config (SHOW_NEW_AD_KEY). בשלב הזה תוסיפו לקוד של האפליקציה את הלוגיקה של מה שהאפליקציה צריכה להציג על סמך הערך של הפרמטר הזה – true (הצגת המודעה החדשה) לעומת false (לא הצגת המודעה החדשה).

הוספת ערכות ה-SDK הנדרשות

לפני שמשתמשים ב-Remote Config בקוד האפליקציה, צריך להוסיף את ה-SDK של Remote Config ואת ה-SDK של Firebase ל-Google Analytics לקובצי ה-build של הפרויקט.

מוסיפים את ה-pods הבאים ל-Podfile ומתקינים אותם:

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

מוסיפים לקובץ build.gradle את יחסי התלות הבאים בספריות:

implementation 'com.google.android.gms:play-services-ads:23.6.0'
implementation 'com.google.firebase:firebase-analytics:22.1.2'
implementation 'com.google.firebase:firebase-config:22.0.1'

מורידים ומתקינים את Firebase Unity SDK, ואז מוסיפים לפרויקט את חבילות Unity הבאות:

  • FirebaseAnalytics.unitypackage
  • FirebaseRemoteConfig.unitypackage

הגדרת מכונה של Remote Config

כדי להשתמש בערכי הפרמטר Remote Config, צריך להגדיר את המכונה Remote Config כך שתשלוף ערכים חדשים למכונה של אפליקציית הלקוח.

בדוגמה הזו, Remote Config מוגדר לבדוק ערכים חדשים של פרמטרים פעם בשעה.

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

אחזור והפעלה של Remote Config

מאחזרים ומפעילים את הפרמטרים Remote Config כדי שאפשר יהיה להתחיל להשתמש בערכי הפרמטרים החדשים.

מומלץ לבצע את הקריאה הזו מוקדם ככל האפשר בשלב הטעינה של האפליקציה, כי הקריאה הזו היא אסינכררונית וצריך לאחזר מראש את הערך של Remote Config כדי שהאפליקציה תדע אם להציג את המודעה.

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

עכשיו האפליקציה מוכנה לטפל בפרמטר Remote Config שיצרתם במהלך הגדרת בדיקת ה-A/B מוקדם יותר במדריך הזה.

שימוש בערך הפרמטר Remote Config

משתמשים בערך Remote Config שאוחזר מראש בפונקציה loadAdUnit() כדי לקבוע אם מופע האפליקציה צריך להציג (ערך הפרמטר הוא true) או לא להציג (ערך הפרמטר הוא false) את היחידה החדשה של המודעות המתגמלות מסוג מעברון.

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

הוספת בדיקות אחרות לערך הפרמטר

יש אזורים אחרים בקוד האפליקציה שבהם תצטרכו לבדוק את הערך של הפרמטר Remote Config כדי לקבוע איזו חוויית צפייה במודעות תוריד. לדוגמה, אתם יכולים להחליט אם לטעון מחדש מודעה אחרי שהמשתמש מסיים לצפות במודעה הנוכחית.

קודם צריך לבצע את הקריאות לאחזור ולהפעלה כדי לקבל עדכונים לגבי ערכי הפרמטרים – לדוגמה, אם מחליטים לסיים ניסוי או ליצור ניסוי חדש.

לאחר מכן, תמיד תוכלו לבדוק את הערך של הפרמטר באמצעות הקריאות הבאות:

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

הקריאות האלה תמיד יחזירו את אותו ערך למכונה של האפליקציה, בהתאם למיקום שלה בקבוצת הבקרה או בקבוצה החדשה של וריאנטים של מודעות, אלא אם בוצעו שינויים במסוף Firebase שאוחזרו והופעלו בקריאות הקודמות.




שלב 2: מגדירים בדיקת A/B במסוף Firebase שלב 4: מפעילים את בדיקת ה-A/B ובודקים את תוצאות הבדיקה