教學課程:利用 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. 展開左側窗格中的「互動」部分,然後選取 遠端設定

  3. 按一下「建立設定」 (如果已使用,則點按「新增參數」)。 Remote Config前)。

  4. 在「建立參數」面板中完成下列步驟:

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

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

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

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

    1. 在「Name」(名稱) 欄位中輸入 Purchasers Group (或其他可輕鬆輸入的) 可供條件識別的名稱)。

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

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

    4. 按一下 [Save Condition]

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

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

    2. 在「預設值」部分選取「是」

  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.0'

飄逸袖

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

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

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

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