ステップ 3: 特定の広告エクスペリエンスを表示するように Firebase Remote Config を設定する
| 概要: AdMob、Google Analytics、Firebase を使用してハイブリッド型の収益化を最適化する |
| ステップ 1: AdMob を使用して、ディスプレイ用の新しい広告ユニットを作成します。 |
| ステップ 2: Google アナリティクスを設定する |
|
ステップ 3: 特定の広告エクスペリエンスを表示するように Firebase Remote Config を設定する |
前回のステップの最後では、Google アナリティクスのオーディエンスについて学びました。このステップでは、「購入者」オーディエンスを活用する Remote Config ブール値制御パラメータ(ad_control_switch)を作成します。次に、このパラメータの値に基づいてアプリに表示する内容を決めるロジックをアプリのコードに追加します。
Firebase コンソールで Remote Config パラメータと条件を設定する
Firebase コンソールで Firebase プロジェクトを開きます。
左側のペインで [エンゲージメント] セクションを開き、[Remote Config] を選択します。
[構成を作成](または、以前に Remote Config を使用したことがある場合は [パラメータを追加])をクリックします。
[パラメータを作成] パネルで、次の操作を行います。
[パラメータ名] フィールドに「
ad_control_switch」と入力します。Data typeプルダウン メニューから [Boolean] を選択します。[新規作成] をクリックし、[新しい条件を作成] を選択します。
[新しい条件を定義する] ダイアログで、次の手順を行います。
[名前] フィールドに「
Purchasers Group」(または条件を簡単に識別できる名前)を入力します。[適用条件...] プルダウン メニューから [ユーザー オーディエンス] を選択します。
[Select audiences(s)] プルダウン メニューから、[Purchasers] を選択します。
[Save Condition] をクリックします。
[パラメータを作成] パネルに戻り、次の操作を行います。
[Purchasers Group] の [Value] で、[false] を選択します。
[デフォルト値] で [true] を選択します。
[保存] をクリックし、[変更を公開] をクリックします。
この構成では、ユーザーが「購入者」オーディエンス(つまり、有料ユーザー)に属しているかどうかが確認されます。
ユーザーが「購入者」オーディエンスに属している場合、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 と Firebase 向け Google アナリティクス SDK がすでに含まれていることに注意してください。
Swift
Podfile に Remote Config Pod を追加してインストールします。
pod 'Firebase/RemoteConfig'
Android
build.gradle ファイルに Remote Config ライブラリの依存関係を追加します。
implementation 'com.google.firebase:firebase-config:23.1.0'
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 が新しいパラメータ値を 1 時間ごとにチェックするように構成されています。
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 パラメータ値を使用する
プリフェッチされた Remote Config 値を loadAdUnit() 関数で使用して、アプリ インスタンスが次のいずれを行うかを決定します。
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 アナリティクスのダッシュボード(Firebase コンソールまたは Google アナリティクスの UI)の両方でモニタリングできます。
これで完了です。AdMob、Google アナリティクス、Firebase を使用してハイブリッド収益化を最適化するチュートリアルを完了しました。
関連資料
その他のソリューション ガイドもご覧ください。
動画シリーズ Firebase と AdMob でアプリの収益を最適化するを見る。