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

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



ในตอนท้ายของขั้นตอนสุดท้าย คุณได้สร้างพารามิเตอร์ Remote Config (SHOW_NEW_AD_KEY) แล้ว ในขั้นตอนนี้ คุณจะต้องเพิ่มตรรกะลงในโค้ดของแอปสำหรับสิ่งที่แอปควรแสดงโดยอิงตามค่าของพารามิเตอร์นั้น ซึ่งก็คือ true (แสดงโฆษณาใหม่) เทียบกับ false (ไม่แสดงโฆษณาใหม่)

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

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

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

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

เพิ่มทรัพยากร Dependency ของไลบรารีต่อไปนี้ลงในไฟล์ 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 ได้รับการกําหนดค่าให้ตรวจสอบค่าพารามิเตอร์ใหม่ทุกชั่วโมง

Swift Objective-C JavaKotlin Unity
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 ล่วงหน้าเพื่อให้แอปทราบว่าควรแสดงโฆษณาหรือไม่

Swift Objective-C JavaKotlin Unity
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 ที่คุณสร้างขึ้นแล้วในระหว่างการตั้งค่าการทดสอบ A/B ก่อนหน้านี้ในบทแนะนํานี้

ใช้ค่าพารามิเตอร์ Remote Config

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

Swift Objective-C JavaKotlin Unity
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 นี้เพื่อกําหนดว่าระบบจะโหลดประสบการณ์การใช้งานโฆษณาใด เช่น คุณเลือกได้ว่าจะโหลดโฆษณาซ้ำหรือไม่หลังจากที่ผู้ใช้ดูโฆษณาปัจจุบันจบแล้ว

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

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

Swift Objective-C JavaKotlin Unity
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: ตั้งค่าการทดสอบ A/B ในFirebaseคอนโซล ขั้นตอนที่ 4: เริ่มการทดสอบ A/B และตรวจสอบผลการทดสอบ