บทแนะนํา: ทดสอบการใช้รูปแบบโฆษณา AdMob ใหม่

ขั้นตอนที่ 3: จัดการค่าพารามิเตอร์ Remote Config ในโค้ดของแอป


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

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

ขั้นตอนที่ 4: เริ่ม การทดสอบ A/B และดูผลการทดสอบในคอนโซล 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

เพิ่มทรัพยากร Dependency ของไลบรารีต่อไปนี้ลงในไฟล์ 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

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

  • FirebaseAnalytics.unitypackage
  • FirebaseRemoteConfig.unitypackage

กำหนดค่าอินสแตนซ์ Remote Config

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

ในตัวอย่างนี้ มีการกำหนดค่า Remote Config เพื่อตรวจหาพารามิเตอร์ใหม่ 1 ครั้งต่อชั่วโมง

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 ที่คุณสร้างแล้ว ระหว่างการทดสอบ A/B ที่ตั้งค่าไว้ก่อนหน้านี้ในบทแนะนำนี้

ใช้ค่าพารามิเตอร์ 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: สร้างการทดสอบ A/B ในคอนโซล Firebase ขั้นตอนที่ 4: เริ่มการทดสอบ A/B และ ดูผลการทดสอบ