教學課程:利用 AdMob、Google Analytics (分析) 和 Firebase 改善混合型營利策略

步驟 3:設定 Firebase Remote Config 以顯示特定廣告體驗


前言:使用 AdMobGoogle Analytics 和 Firebase 最佳化混合型營利
步驟 1: 使用 AdMob 建立用於顯示的廣告單元
步驟 2:設定 Google Analytics

步驟 3: 設定Firebase Remote Config,以便顯示特定廣告體驗


在上一節課中,您已瞭解 Google Analytics 目標對象。在這個步驟中,您將建立 Remote Config 布林值控制參數 (稱為 ad_control_switch),用於運用「購買者」目標對象。接著,您將在應用程式程式碼中加入邏輯,讓應用程式根據該參數的值顯示適當內容。

Firebase 控制台中設定 Remote Config 參數和條件

  1. Firebase 控制台中開啟 Firebase 專案。

  2. 在左側窗格中展開「Engage」部分,然後選取「Remote Config」

  3. 按一下「建立設定」 (如果您之前曾使用 Remote Config,請按一下「新增參數」)。

  4. 在「Create parameter」面板中,完成下列步驟:

    1. 在「參數名稱」欄位中輸入 ad_control_switch

    2. Data type 下拉式選單中,選取「布林值」

    3. 按一下「建立新條件」,然後選取「建立新條件」

  5. 在「Define a new condition」對話方塊中,完成下列步驟:

    1. 在「Name」欄位中輸入 Purchasers Group (或任何其他容易識別的條件名稱)。

    2. 在「適用於...」下拉式選單中,選取「使用者目標對象」

    3. 在「選取目標對象」下拉式選單中,選取「購買者」

    4. 按一下 [Save Condition]

  6. 回到「Create parameter」面板,完成下列步驟:

    1. 在「購買者群組」的「值」中,選取「false」

    2. 在「預設值」中,選取「true」

  7. 按一下「儲存」,然後點選「發布變更」

這項設定會檢查使用者是否屬於「購買者」目標對象 (也就是付費使用者):

  • 如果使用者屬於「購買者」目標對象,Remote Config 就會針對 ad_control_switch 參數傳回 false 的值。

  • 如果使用者不屬於「購買者」目標對象,Remote Config 就會針對 ad_control_switch 參數傳回 true 的值。

在後續步驟中,您將在應用程式中實作 Remote Config,以便處理這些參數值。

在應用程式中加入 Remote Config SDK

在應用程式程式碼中使用 Remote Config 之前,請先將 Remote Config SDK 新增至應用程式程式碼庫。請注意,您的應用程式應已包含本教學課程前幾個步驟中所述的 Google Mobile Ads (AdMob) SDK 和 Google Analytics for Firebase SDK。

Swift

在 Podfile 中新增及安裝 Remote Config Pod:

pod 'Firebase/RemoteConfig'

Android

Remote Config 程式庫依附元件新增至 build.gradle 檔案:

implementation 'com.google.firebase:firebase-config:22.0.1'

Flutter

在 Flutter 專案的根目錄中執行下列指令,安裝 Remote Config 外掛程式:

flutter pub add firebase_remote_config

Unity

下載並安裝最新的 Firebase Unity SDK,然後將 Remote Config 套件新增至專案:
FirebaseRemoteConfig.unitypackage

設定 Remote Config 執行個體

為讓應用程式使用 Remote Config 參數值,請設定 Remote Config 例項,讓它為用戶端應用程式例項擷取新的值。

在本例中,Remote Config 已設為每小時檢查一次是否有新的參數值。

Swift

remoteConfig = RemoteConfig.remoteConfig()
let settings = RemoteConfigSettings()
settings.minimumFetchInterval = 3600
remoteConfig.configSettings = settings

Kotlin

remoteConfig = Firebase.remoteConfig
val configSettings = remoteConfigSettings {
    minimumFetchIntervalInSeconds = 3600
}
remoteConfig.setConfigSettingsAsync(configSettings)

Java

mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder()
        .setMinimumFetchIntervalInSeconds(3600)
        .build();
mFirebaseRemoteConfig.setConfigSettingsAsync(configSettings);

Flutter

remoteConfig = FirebaseRemoteConfig.instance;
  final configSettings = FirebaseRemoteConfigSettings(
    minimumFetchInterval: Duration(hours: 1),
  );
  await remoteConfig.setConfigSettings(configSettings);

  // Use the `onConfigUpdated` callback to listen for changes to the config settings.
  remoteConfig.onConfigUpdated.listen((_) {
    print('Config settings confirmed');
  });

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

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

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

Flutter

remoteConfig = FirebaseRemoteConfig.instance;

// Fetch and activate the latest Remote Config values.
final updated = await remoteConfig.fetchAndActivate();

// Check if the config params were updated successfully.
if (updated) {
  print('Config params updated');
} else {
  print('Config params failed to update');
}

// Load the ad unit.
_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 參數值

請在 loadAdUnit() 函式中使用預先擷取的 Remote Config 值,判斷應用程式執行個體是否應執行下列其中一種操作:

  • ad_control_switch 參數值會解析為 true:顯示全螢幕廣告 (因為使用者是非付費使用者)。

  • ad_control_switch 參數值會解析為 false:請勿顯示廣告 (因為使用者是付費使用者)。

Swift

private func loadAdUnit() {
  let showAds = remoteConfig["ad_control_switch"].boolValue

  if showAds {
    // Load interstitial ad (implemented ad unit)
    // per AdMob instructions (the first step of this tutorial).
  } else {
    // Don't show ads.
  }
}

Kotlin

private fun loadAdUnit() {
  var showAds = remoteConfig.getBoolean(ad_control_switch)

  if (showAds) {
      // Load interstitial ad (implemented ad unit)
      // per AdMob instructions (the first step of this tutorial).
    } else {
      // Don't show ads.
    }
}

Java

private void loadAdUnit() {
    boolean showAds =
      mFirebaseRemoteConfig.getBoolean(ad_control_switch);

    if (showAds) {
      // Load interstitial ad (implemented ad unit)
      // per AdMob instructions (the first step of this tutorial).
    } else {
      // Don't show ads.
    }
}

Flutter

void _loadAdUnit() {
  bool showAds = remoteConfig.getBool(ad_control_switch);

  if (showAds) {
    // Load interstitial ad (implemented ad unit)
    // per AdMob instructions (the first step of this tutorial).
  } else {
    // Don't show ads.
  }
}

Unity

void LoadAdUnit() {
  bool showAds =
      remoteConfig.GetValue("ad_control_switch").BooleanValue;

  if (showAds) {
    // Load interstitial ad (implemented ad unit)
    // per AdMob instructions (the first step of this tutorial).
  } else {
    // Don't show ads.
  }
}

發布應用程式

由於顯示廣告的邏輯位於程式碼集中,因此您需要發布包含此邏輯的應用程式新版本。

如果您按照本教學課程的步驟操作,應用程式應會立即開始為使用者提供自訂的應用程式內廣告體驗。您可以在 AdMob 帳戶和 Google Analytics 資訊主頁 (Firebase 控制台或 Google Analytics UI) 中監控廣告收益。


大功告成!您已完成使用 AdMob、Google Analytics 和 Firebase 最佳化混合式營利功能的教學課程。




步驟 2:設定 Google Analytics