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

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


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

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

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


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

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

قبل استخدام Remote Config في رمز تطبيقك، أضِف كلاً من حزمة تطوير البرامج (SDK) Remote Config وحزمة تطوير البرامج (SDK) لمنصة Firebase للموقع الإلكتروني 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:23.3.0'
implementation 'com.google.firebase:firebase-analytics:22.1.0'
implementation 'com.google.firebase:firebase-config:22.0.0'

Unity

عليك تنزيل حزمة تطوير البرامج (SDK) لمنصّة Firebase Unity وتثبيتها، ثم إضافة حزمة 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+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");
}

جلب 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+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();
});

تطبيقك جاهز الآن للتعامل مع معلَمة 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+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.
  }
}

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

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

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

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

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

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




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