บทแนะนํา: เพิ่มประสิทธิภาพความถี่ของโฆษณา AdMob

ขั้นตอนที่ 3: จัดการค่าพารามิเตอร์การกำหนดค่าระยะไกลในโค้ดของแอป


บทนำ: เพิ่มประสิทธิภาพความถี่ของโฆษณา AdMob โดยใช้ Firebase
ขั้นตอนที่ 1: ใช้ AdMob สร้างตัวแปรของหน่วยโฆษณาใหม่สำหรับการทดสอบ
ขั้นตอนที่ 2: ตั้งค่าการทดสอบ A/B ในคอนโซล Firebase

ขั้นตอนที่ 3: จัดการค่าพารามิเตอร์การกำหนดค่าระยะไกลในโค้ดของแอป

ขั้นตอนที่ 4: เริ่มการทดสอบ A/B และตรวจสอบผลการทดสอบในคอนโซล Firebase
ขั้นตอนที่ 5: ตัดสินใจว่าจะเปิดตัวโฆษณารูปแบบใหม่หรือไม่


ในตอนท้ายของขั้นตอนสุดท้าย คุณได้สร้างพารามิเตอร์การกำหนดค่าระยะไกล (INTERSTITIAL_AD_KEY) ในขั้นตอนนี้ คุณจะต้องเพิ่มตรรกะลงในโค้ดของแอปเพื่อดูว่าแอปควรแสดงอะไรโดยอิงตามค่าของพารามิเตอร์นั้น

เพิ่ม SDK ที่จำเป็น

ก่อนที่จะใช้การกำหนดค่าระยะไกลในโค้ดของแอปพลิเคชัน ให้เพิ่มทั้ง SDK การกำหนดค่าระยะไกลและ Firebase SDK สำหรับ Google Analytics ลงในไฟล์บิลด์ของโปรเจ็กต์

Swift

เพิ่มและติดตั้งพ็อดต่อไปนี้ใน Podfile

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

Objective-C

เพิ่มและติดตั้งพ็อดต่อไปนี้ใน Podfile

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

Android

เพิ่มทรัพยากร Dependency ของไลบรารีต่อไปนี้ลงในไฟล์ build.gradle

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

Unity

ดาวน์โหลดและติดตั้ง Firebase Unity SDK จากนั้นเพิ่มแพ็กเกจ Unity ต่อไปนี้ลงในโปรเจ็กต์ของคุณ

  • FirebaseAnalytics.unitypackage
  • FirebaseRemoteConfig.unitypackage

กำหนดค่าอินสแตนซ์การกำหนดค่าระยะไกล

หากต้องการใช้ค่าพารามิเตอร์การกำหนดค่าระยะไกล ให้กำหนดค่าอินสแตนซ์การกำหนดค่าระยะไกลเพื่อให้มีการตั้งค่าให้ดึงค่าใหม่สำหรับอินสแตนซ์แอปไคลเอ็นต์

ในตัวอย่างนี้ การกำหนดค่าระยะไกลมีการกำหนดค่าเพื่อตรวจสอบค่าพารามิเตอร์ใหม่ทุกชั่วโมง

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

ดึงข้อมูลและเปิดใช้งานการกำหนดค่าระยะไกล

ดึงข้อมูลและเปิดใช้งานพารามิเตอร์การกำหนดค่าระยะไกลเพื่อให้เริ่มใช้ค่าพารามิเตอร์ใหม่ได้

คุณต้องเรียกใช้นี้โดยเร็วที่สุดในระยะการโหลดของแอป เนื่องจากการเรียกนี้เป็นแบบไม่พร้อมกันและคุณจะต้องดึงค่าการกำหนดค่าระยะไกล ล่วงหน้าเพื่อให้แอปรู้ว่าจะแสดงโฆษณาใด

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

ตอนนี้แอปของคุณพร้อมที่จะจัดการพารามิเตอร์การกำหนดค่าระยะไกลที่คุณสร้างขึ้นระหว่างการทดสอบ A/B ที่ตั้งค่าไว้ก่อนหน้านี้ในบทแนะนำนี้แล้ว

ใช้ค่าพารามิเตอร์การกำหนดค่าระยะไกล

ใช้ค่าการกำหนดค่าระยะไกลที่ดึงข้อมูลไว้ล่วงหน้าในฟังก์ชัน loadAdUnit() เพื่อกำหนดรูปแบบความถี่ของโฆษณาที่ควรจะแสดงสำหรับอินสแตนซ์แอปนี้

Swift

private func loadAdUnit() {
  let adUnitId = remoteConfig["INTERSTITIAL_AD_KEY"].stringValue;
  let request = GADRequest()
  GADInterstitialAd.load(withAdUnitID: adUnitId,
                               request: request,
                     completionHandler: { [self] ad, error in
                       if let error = error {
                         print("Failed to load: \(error.localizedDescription)")
                         return
                       }
                       interstitial = ad
                       // Register for callbacks.
                     }
  )
}

// Register for callbacks.

Objective-C

- (void)loadAdUnit {
    NSString *adUnitId =
      self.remoteConfig[@"INTERSTITIAL_AD_KEY"].stringValue;

  GADRequest *request = [GADRequest request];
  [GADInterstitialAd loadAdWithAdUnitId:adUnitId
                         request:request
                         completionHandler:^(GADInterstitialAd *ad,
                             NSError *error) {
    if (error) {
      NSLog(@"Failed to load interstitial ad with error: %@",
        [error localizedDescription]);
      return;
    }

    self.interstitial = ad;
  }];
}

Java

private void loadAdUnit() {
    String adUnitId =
      mFirebaseRemoteConfig.getString("INTERSTITIAL_AD_KEY");

    // Load Interstitial Ad (assume adUnitId not null)
    AdRequest adRequest = new AdRequest.Builder().build();

    InterstitialAd.load(this, adUnitId, adRequest, new
        InterstitialAdLoadCallback() {
          @Override
          public void onAdLoaded(@NonNull InterstitialAd intertitialAd) {
            mInterstitialAd = interstitialAd;
          }

          @Override
          public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
            mInterstitialAd = null;
          }
    });
}

Kotlin+KTX

private fun loadAdUnit() {
  String adUnitId = remoteConfig.getString("INTERSTITIAL_AD_KEY")
  var adRequest = AdRequestBuilder.Builder().build()

  AdRequestBuilder.load(this, adUnitId, adRequest, object :
    InterstitialAdLoadCallback() {
      override fun onAdFailedToLoad(adError: LoadAdError) {
        mInterstitialAd = null
      }

      override fun onAdLoaded(interstitialAd: InterstitialAd) {
        mInterstitialAd = interstitialAd
      }
    })
}

Unity

void LoadAdUnit() {

  // Note that you may want to encode and parse two sets of ad unit IDs for
  // Android / iOS in the Unity implementation.
  String adUnitId = remoteConfig.GetValue("INTERSTITIAL_AD_KEY").StringValue;
  this.interstitial = new InterstitialAd(adUnitId);
}

เพิ่มการตรวจสอบอื่นๆ สำหรับค่าพารามิเตอร์

มีส่วนอื่นๆ ในโค้ดของแอปพลิเคชันที่คุณจะต้องตรวจสอบค่าของพารามิเตอร์การกำหนดค่าระยะไกลนี้เพื่อกำหนดประสบการณ์การใช้งานโฆษณาที่จะโหลด ตัวอย่างเช่น คุณสามารถตัดสินใจว่าจะโหลดโฆษณาซ้ำหรือไม่หลังจากที่ผู้ใช้ดูโฆษณาปัจจุบันจบแล้ว

คุณควรเรียกใช้การดึงข้อมูลและเปิดใช้งานก่อนเพื่อรับการเปลี่ยนแปลงค่าพารามิเตอร์ เช่น ในกรณีที่ตัดสินใจสิ้นสุดหรือสร้างการทดสอบใหม่

คุณสามารถตรวจสอบค่าของพารามิเตอร์ได้เสมอโดยใช้การเรียกต่อไปนี้

Swift

remoteConfig["INTERSTITIAL_AD_KEY"].stringValue

Objective-C

self.remoteConfig[@"INTERSTITIAL_AD_KEY"].stringValue;

Java

mFirebaseRemoteConfig.getString(INTERSTITIAL_AD_KEY)

Kotlin+KTX

remoteConfig.getString(INTERSTITIAL_AD_KEY)

Unity

remoteConfig.GetValue("INTERSTITIAL_AD_KEY").StringValue

การเรียกเหล่านี้จะส่งคืนค่าเดียวกันสำหรับอินสแตนซ์แอปเสมอ โดยขึ้นอยู่กับว่ามีการวางไว้ในกลุ่มควบคุมหรือกลุ่มตัวแปรโฆษณาใหม่กลุ่มใด เว้นแต่มีการเปลี่ยนแปลงในคอนโซล Firebase ที่มีการดึงข้อมูลและเปิดใช้งานในการเรียกครั้งก่อน




ขั้นตอนที่ 2: สร้างการทดสอบ A/B ในคอนโซล Firebase ขั้นตอนที่ 4: เริ่มการทดสอบ A/B และตรวจสอบผลการทดสอบ