教學:使用 AdMob、Google Analytics 和 Firebase 優化混合創收

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


簡介:使用 AdMob、Google Analytics 和 Firebase 優化混合創收
步驟 1: 使用 AdMob 建立新的廣告單元進行展示
第 2 步:設定 Google Analytics(分析)

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


在最後一步結束時,您了解了 Google Analytics 受眾群體。在此步驟中,您將建立一個利用「購買者」受眾的遠端設定布林控制參數(稱為ad_control_switch )。然後,您將根據該參數的值將邏輯新增至應用程式的程式碼中,以決定應用程式應顯示的內容。

在 Firebase 控制台中設定遠端設定參數和條件

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

  2. 在左側窗格中,展開參與部分,然後選擇遠端設定

  3. 按一下建立配置(如果您之前使用過遠端配置,請按一下新增參數)。

  4. 建立參數面板中,完成以下步驟:

    1. 參數名稱欄位中,輸入ad_control_switch

    2. Data type下拉式選單中,選擇布林值

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

  5. 「定義新條件」對話方塊中,完成以下步驟:

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

    2. 「適用於...」下拉式選單中,選擇「使用者受眾」

    3. 選擇受眾下拉式選單中,選擇購買者

    4. 點選儲存條件

  6. 返回建立參數面板,完成以下步驟:

    1. 對於購買者組價值,選擇false

    2. 對於預設值,選擇true

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

此配置將檢查用戶是否屬於「購買者」受眾(即,他們是付費用戶):

  • 如果使用者屬於「購買者」受眾群體,則遠端配置將為ad_control_switch參數傳回false值。

  • 如果使用者不屬於「購買者」受眾群體,則遠端配置將為ad_control_switch參數傳回true值。

在以下步驟中,您將在應用程式中實作遠端配置來處理這些參數值。

將遠端配置 SDK 新增至您的應用程式

在應用程式程式碼中使用遠端配置之前,請將遠端配置 SDK 新增至應用程式的程式碼庫。請注意,您的應用程式應該已經具有本教學課程前面步驟中的 Google 行動廣告 (AdMob) SDK 和 Google Analytics for Firebase SDK。

迅速

在 podfile 中新增並安裝 Remote Config pod:

pod 'Firebase/RemoteConfig'

安卓

將遠端設定庫依賴項新增至您的build.gradle檔案:

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

從 Flutter 專案的根目錄中,執行以下命令來安裝遠端設定外掛程式:

flutter pub add firebase_remote_config

統一

下載並安裝最新的Firebase Unity SDK ,然後將遠端設定包新增至您的專案:
FirebaseRemoteConfig.unitypackage

配置遠端配置實例

為了讓您的應用程式可以使用遠端配置參數值,請配置遠端配置實例,以便它可以為客戶端應用程式實例取得新值。

在此範例中,遠端配置配置為每小時檢查一次新參數值。

迅速

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

Kotlin+KTX

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

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

統一

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

取得並啟動遠端配置

取得並啟動遠端配置參數,以便它可以開始使用新的參數值。

您希望在應用程式的載入階段儘早進行此調用,因為此調用是異步的,並且您需要預先取得遠端配置值,以便您的應用程式知道是否顯示廣告。

迅速

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

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

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

統一

remoteConfig.FetchAndActivateAsync().ContinueWithOnMainThread(task => {
  if (task.IsFaulted) {
    Debug.LogWarning("Config params failed to update");
  } else {
    Debug.Log("Config params updated: " + task.Result);
  }
  LoadAdUnit();
});

您的應用程式現在已配置為處理您先前在此步驟中建立的遠端設定參數。

使用遠端配置參數值

使用loadAdUnit()函數中預先設定的遠端設定值來決定應用程式實例是否應執行下列操作之一:

  • ad_control_switch參數值解析為true :顯示插頁式廣告(因為使用者是非付費使用者)。

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

迅速

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+KTX

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.
    }
}

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.
  }
}

統一

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(分析)