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

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


مقدّمة: اختبار اعتماد شكل الإعلان الجديد في AdMob باستخدام FirebaseAdMob
الخطوة 1: استخدام AdMob لإنشاء شكل جديد من الوحدة الإعلانية لأغراض الاختبار
الخطوة 2: إعداد اختبار أ/ب في وحدة التحكّم Firebase

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

الخطوة 4: بدء اختبار أ/ب ومراجعة نتائج الاختبار في وحدة تحكّم Firebase
الخطوة 5: تحديد ما إذا كنت تريد طرح شكل الإعلان الجديد


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

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

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

الأنظمة الأساسية من Apple

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

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

Android

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

implementation 'com.google.android.gms:play-services-ads:25.2.0'
implementation 'com.google.firebase:firebase-analytics:23.2.0'
implementation 'com.google.firebase:firebase-config:23.1.0'

Unity

نزِّل حزمة Firebase Unity SDK وثبِّتها، ثم أضِف حِزم Unity التالية إلى مشروعك:

  • FirebaseAnalytics.unitypackage
  • FirebaseRemoteConfig.unitypackage

ضبط النسخة الافتراضية من Remote Configالإعداد عن بُعد

لاستخدام قيم مَعلمات Remote Config، اضبط النسخة الافتراضية لـ Remote Config على استرجاع قيم جديدة لنسخة تطبيق العميل الافتراضية.

في هذا المثال، تم ضبط قيمة Remote Config بشكل يتيح البحث عن قيم مَعلمات جديدة مرة واحدة كل ساعة.

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

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

استرجاع وتفعيل Remote Config

استرِجع مَعلمات Remote Config وفعِّلها حتى تتمكّن من بدء استخدام قيم المَعلمات الجديدة.

ستحتاج إلى إجراء هذا الطلب في أسرع وقت ممكن خلال مرحلة تحميل التطبيق لأنّ هذا الطلب يكون غير متزامن. ستحتاج أيضًا إلى استرجاع قيمة Remote Config بشكل مُسبق حتى يتمكّن تطبيقك من معرفة ما إذا كان سيعرض الإعلان أم لا.

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

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

أصبح تطبيقك الآن جاهزًا لمعالجة المَعلمة Remote Config التي أنشأتها أثناء إعداد مجموعة اختبار أ/ب في وقت سابق من هذا البرنامج التعليمي.

استخدام قيمة مَعلمة Remote Config

استخدِم قيمة Remote Config التي تم استرجاعها مُسبقًا في الدالة loadAdUnit() لتحديد ما إذا كان يجب أن تعرض (قيمة المَعلمة true) أو لا تعرض (قيمة المَعلمة false) نسخة التطبيق الافتراضية وحدة "الإعلانات البينية مقابل مكافأة" الجديدة.

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

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

إضافة عمليات تحقّق أخرى لقيمة المَعلمة

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

يجب إجراء طلبَي الاسترجاع والتفعيل أولاً للحصول على أي تغييرات في قيمة المَعلمة، مثلاً إذا قرّرت إنهاء تجربة أو إنشاء تجربة جديدة.

من هنا، يمكنك دائمًا التحقّق من قيمة المَعلمة باستخدام الطلبَين التاليَين:

Swift

remoteConfig["showNewAdKey"].boolValue

Objective-C

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

Java

mFirebaseRemoteConfig.getBoolean(SHOW_NEW_AD_KEY)

Kotlin

remoteConfig.getBoolean(SHOW_NEW_AD_KEY)

Unity

remoteConfig.GetValue("SHOW_NEW_AD_KEY").BooleanValue

سيعرض هذان الطلبان دائمًا القيمة نفسها لنسخة التطبيق الافتراضية، وذلك استنادًا إلى ما إذا تم وضعها في مجموعة التحكّم أو مجموعة شكل الإعلان الجديد، ما لم يتم إجراء أي تغييرات في وحدة تحكّم Firebase تم استرجاعها وتفعيلها في الطلبات السابقة.




الخطوة 2: إعداد اختبار أ/ب في وحدة تحكّم Firebase الخطوة 4: بدء اختبار أ/ب ومراجعة نتائج الاختبار