チュートリアル: AdMob、Google アナリティクス、Firebase を使用してハイブリッド型の収益化を最適化する

ステップ 3: 特定の広告エクスペリエンスを表示するように Firebase Remote Config を設定する


概要: AdMobGoogle Analytics、Firebase を使用してハイブリッド型の収益化を最適化する
ステップ 1: AdMob を使用して、表示用の新しい広告ユニットを作成する
ステップ 2: Google アナリティクスを設定する

ステップ 3: 特定の広告エクスペリエンスを表示するように Firebase Remote Config を設定する


前回のステップの最後で、Google アナリティクスのオーディエンスについて学びました。このステップでは、「購入者」オーディエンスを活用する Remote Config ブール値制御パラメータ(ad_control_switch という名前)を作成します。次に、パラメータの値に基づいてアプリが表示する内容を決めるロジックをアプリのコードに追加します。

Firebase コンソールで Remote Config パラメータと条件を設定する

  1. Firebase コンソールで Firebase プロジェクトを開きます。

  2. 左側のパネルで [エンゲージ] セクションを開き、[リモート設定] を選択します。

  3. [構成を作成] をクリックします(以前に Remote Config を使用している場合は [パラメータを追加] をクリックします)。

  4. [Create parameter] パネルで、次の手順を完了します。

    1. [Parameter name] フィールドに「ad_control_switch」と入力します。

    2. Data type プルダウン メニューから [ブール値] を選択します。

    3. [新規作成] をクリックし、[新しい条件を作成] を選択します。

  5. [新しい条件を定義] ダイアログで、次の手順を実施します。

    1. [名前] フィールドに「Purchasers Group」(または条件を簡単に識別できる名前)と入力します。

    2. [Applies if...] プルダウン メニューから [ユーザー オーディエンス] を選択します。

    3. [対象ユーザーを選択] プルダウン メニューから [購入者] を選択します。

    4. [Save Condition] をクリックします。

  6. [パラメータを作成] パネルに戻り、次の手順を完了します。

    1. [購入者グループ] の [] で [false] を選択します。

    2. [デフォルト値] で [true] を選択します。

  7. [保存]、[変更を公開] の順にクリックします。

この設定では、ユーザーが「購入者」オーディエンス(有料ユーザー)に属しているかどうかを確認します。

  • ユーザーが「購入者」オーディエンス内にいる場合、Remote Configad_control_switch パラメータの値 false を返します。

  • ユーザーが「購入者」オーディエンスに含まれない場合Remote Configad_control_switch パラメータの true の値を返します。

次の手順では、アプリに Remote Config を実装して、これらのパラメータ値を処理します。

アプリに Remote Config SDK を追加する

アプリケーション コードで Remote Config を使用する前に、アプリのコードベースに Remote Config SDK を追加します。アプリには、このチュートリアルの前の手順で追加した Google Mobile AdsAdMob)SDK と Firebase 向け Google アナリティクス 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 が新しいパラメータ値を 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 を使用してハイブリッド収益化を最適化するチュートリアルを完了しました。




ステップ 2: Google アナリティクスを設定する