برنامج تعليمي: اختبار استخدام أشكال إعلانات AdMob الجديدة

الخطوة 3: التعامل مع قيم مَعلمة Remote Config في رمز تطبيقك



في نهاية الخطوة الأخيرة، أنشأت مَعلمة Remote Config (SHOW_NEW_AD_KEY). في هذه الخطوة، ستضيف المنطق إلى رمز تطبيقك لتحديد ما يجب أن يعرضه تطبيقك استنادًا إلى قيمة هذه المَعلمة، أي true (عرض الإعلان الجديد) مقابل false (عدم عرض الإعلان الجديد).

إضافة حِزم تطوير البرامج (SDK) المطلوبة

قبل استخدام Remote Config في رمز تطبيقك، أضِف كلًّا من حزمة تطوير البرامج (SDK) لنظام التشغيل Remote Config وحزمة تطوير البرامج (SDK) لمنصّة Google Analytics إلى ملفات إنشاء مشروعك.

أضِف مجموعات الإعلانات المتسلسلة التالية وثبِّتها في ملف podfile:

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

أضِف مكتبات الاعتماد التالية إلى ملف build.gradle:

implementation 'com.google.android.gms:play-services-ads:24.0.0'
implementation 'com.google.firebase:firebase-analytics:22.3.0'
implementation 'com.google.firebase:firebase-config:22.1.0'

نزِّل حزمة 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 التي أنشأتها أثناء إعداد اختبار أ/ب في وقت سابق من هذا الدليل التعليمي.

استخدام قيمة المَعلمة 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: إعداد اختبار أ/ب في وحدة تحكّم Firebase الخطوة 4: بدء اختبار أ/ب ومراجعة نتائج الاختبار