アプリ起動時広告

アプリ起動時広告は、アプリの読み込み画面を収益化したいパブリッシャー向けの特別な広告フォーマットです。アプリ起動時広告はいつでも閉じることができ、ユーザーがアプリをフォアグラウンドに戻したときに表示されるように設計されています。

アプリ起動時広告では、小さなブランド領域が自動的に表示されるため、ユーザーはアプリを操作していることがわかります。アプリ起動時広告は、次のような表示になります。

前提条件

必ずテスト広告でテストする

次のサンプルコードには、テスト広告のリクエストに使用できる広告ユニット ID が含まれています。この ID は、すべてのリクエストに対して実際の広告ではなくテスト広告を返すように設定されており、安全に使用できます。

ただし、Ad Manager 管理画面でアプリを登録し、アプリで使用する独自の広告ユニット ID を作成したら、開発中に明示的にデバイスをテストデバイスとして設定します。

/6499/example/app-open

実装

アプリ起動時広告を組み込む主な手順は、以下のとおりです。

  1. ユーティリティ クラスを作成する
  2. アプリ起動時広告を読み込む
  3. アプリ起動時広告のイベントをリッスンする
  4. 広告の有効期限を考慮する
  5. アプリの状態イベントをリッスンする
  6. アプリ起動時広告を表示する
  7. アプリ起動時広告をクリーンアップする
  8. 次のアプリ起動時広告をプリロードする

ユーティリティ クラスを作成する

AppOpenAdController という新しいクラスを作成して、広告を読み込みます。このクラスの役割は、読み込まれた広告と各プラットフォームの広告ユニット ID をトラッキングするためのインスタンス変数を制御することです。

using System;
using UnityEngine;
using GoogleMobileAds.Api;
using GoogleMobileAds.Common;

/// <summary>
/// Demonstrates how to use the Google Mobile Ads app open ad format.
/// </summary>
[AddComponentMenu("GoogleMobileAds/Samples/AppOpenAdController")]
public class AppOpenAdController : MonoBehaviour
{

    // This ad unit is configured to always serve test ads.
    private string _adUnitId = "/6499/example/app-open";

    public bool IsAdAvailable
    {
        get
        {
            return _appOpenAd != null;
        }
    }

    public void Start()
    {
        // Initialize the Google Mobile Ads SDK.
        MobileAds.Initialize((InitializationStatus initStatus) =>
        {
            // This callback is called once the MobileAds SDK is initialized.
        });
    }

    /// <summary>
    /// Loads the app open ad.
    /// </summary>
    public void LoadAppOpenAd()
    {
    }

    /// <summary>
    /// Shows the app open ad.
    /// </summary>
    public void ShowAppOpenAd()
    {
    }
}

アプリ起動時広告を読み込む

アプリ起動時広告の読み込みは、AppOpenAd クラスで静的 Load() メソッドを使用して行います。読み込みメソッドには、広告ユニット ID と AdManagerAdRequest オブジェクトのほか、広告の読み込みの成功時または失敗時に呼び出される完了ハンドラが必要です。読み込まれた AppOpenAd オブジェクトは、完了ハンドラのパラメータとして提供されます。以下の例は、AppOpenAd を読み込む方法を示しています。


  // This ad unit is configured to always serve test ads.
  private string _adUnitId = "/6499/example/app-open";

  private AppOpenAd appOpenAd;

  /// <summary>
  /// Loads the app open ad.
  /// </summary>
  public void LoadAppOpenAd()
  {
      // Clean up the old ad before loading a new one.
      if (appOpenAd != null)
      {
            appOpenAd.Destroy();
            appOpenAd = null;
      }

      Debug.Log("Loading the app open ad.");

      // Create our request used to load the ad.
      var adRequest = new AdManagerAdRequest();

      // send the request to load the ad.
      AppOpenAd.Load(_adUnitId, adRequest,
          (AppOpenAd ad, LoadAdError error) =>
          {
              // if error is not null, the load request failed.
              if (error != null || ad == null)
              {
                  Debug.LogError("app open ad failed to load an ad " +
                                 "with error : " + error);
                  return;
              }

              Debug.Log("App open ad loaded with response : "
                        + ad.GetResponseInfo());

              appOpenAd = ad;
              RegisterEventHandlers(ad);
          });
  }

アプリ起動時広告のイベントをリッスンする

広告の動作をさらにカスタマイズするには、広告のライフサイクルで生じるさまざまなイベント(起動、終了など)を利用します。このようなイベントをリッスンするには、次のようにデリゲートを登録します。

private void RegisterEventHandlers(AppOpenAd ad)
{
    // Raised when the ad is estimated to have earned money.
    ad.OnAdPaid += (AdValue adValue) =>
    {
        Debug.Log(String.Format("App open ad paid {0} {1}.",
            adValue.Value,
            adValue.CurrencyCode));
    };
    // Raised when an impression is recorded for an ad.
    ad.OnAdImpressionRecorded += () =>
    {
        Debug.Log("App open ad recorded an impression.");
    };
    // Raised when a click is recorded for an ad.
    ad.OnAdClicked += () =>
    {
        Debug.Log("App open ad was clicked.");
    };
    // Raised when an ad opened full screen content.
    ad.OnAdFullScreenContentOpened += () =>
    {
        Debug.Log("App open ad full screen content opened.");
    };
    // Raised when the ad closed full screen content.
    ad.OnAdFullScreenContentClosed += () =>
    {
        Debug.Log("App open ad full screen content closed.");
    };
    // Raised when the ad failed to open full screen content.
    ad.OnAdFullScreenContentFailed += (AdError error) =>
    {
        Debug.LogError("App open ad failed to open full screen content " +
                       "with error : " + error);
    };
}

広告の有効期限を考慮する

期限切れの広告を表示しないようにするには、メソッドを AppOpenAdController に追加して、広告の読み込みからの経過時間と、広告の有効性を確認できるようにします。

アプリ起動時広告は 4 時間でタイムアウトします。読み込み時間を _expireTime 変数にキャッシュしておきましょう。

// send the request to load the ad.
AppOpenAd.Load(_adUnitId, adRequest,
    (AppOpenAd ad, LoadAdError error) =>
    {
        // If the operation failed, an error is returned.
        if (error != null || ad == null)
        {
            Debug.LogError("App open ad failed to load an ad with error : " +
                            error);
            return;
        }

        // If the operation completed successfully, no error is returned.
        Debug.Log("App open ad loaded with response : " + ad.GetResponseInfo());

        // App open ads can be preloaded for up to 4 hours.
        _expireTime = DateTime.Now + TimeSpan.FromHours(4);

        _appOpenAd = ad;
    });

IsAdAvailable プロパティを更新して _expireTime をチェックし、読み込み済みの広告がまだ有効であることを確認します。

public bool IsAdAvailable
{
    get
    {
        return _appOpenAd != null
               && _appOpenAd.IsLoaded()
               && DateTime.Now < _expireTime;
    }
}

アプリの状態イベントをリッスンする

AppStateEventNotifier を使用して、アプリケーションのフォアグラウンド化およびバックグラウンド化イベントをリッスンします。このクラスは、アプリがフォアグラウンド化またはバックグラウンド化するたびに、AppStateChanged イベントを発生させます。

private void Awake()
{
    // Use the AppStateEventNotifier to listen to application open/close events.
    // This is used to launch the loaded ad when we open the app.
    AppStateEventNotifier.AppStateChanged += OnAppStateChanged;
}

private void OnDestroy()
{
    // Always unlisten to events when complete.
    AppStateEventNotifier.AppStateChanged -= OnAppStateChanged;
}

AppState.Foreground 状態をハンドルする際に IsAdAvailabletrue であれば、ShowAppOpenAd() を呼び出して広告を表示します。

private void OnAppStateChanged(AppState state)
{
    Debug.Log("App State changed to : "+ state);

    // if the app is Foregrounded and the ad is available, show it.
    if (state == AppState.Foreground)
    {
        if (IsAdAvailable)
        {
            ShowAppOpenAd();
        }
    }
}

アプリ起動時広告を表示する

読み込み済みのアプリ起動時広告を表示するには、AppOpenAd インスタンスで Show() メソッドを呼び出します。読み込んだ広告は 1 回だけ表示できます。広告を表示する準備ができていることを確認するには、CanShowAd() メソッドを使用します。

/// <summary>
/// Shows the app open ad.
/// </summary>
public void ShowAppOpenAd()
{
    if (appOpenAd != null && appOpenAd.CanShowAd())
    {
        Debug.Log("Showing app open ad.");
        appOpenAd.Show();
    }
    else
    {
        Debug.LogError("App open ad is not ready yet.");
    }
}

アプリ起動時広告をクリーンアップする

使い終わった AppOpenAd への参照を破棄する前に、必ず Destroy() メソッドを呼び出してください。

appOpenAd.Destroy();

これによって、そのオブジェクトはもう使用されておらず、オブジェクトが占有していたメモリを回収しても問題ないことを、プラグインに通知できます。このメソッドを呼び出さないとメモリリークが発生します。

次のアプリ起動時広告をプリロードする

AppOpenAd は使い捨てオブジェクトです。一度アプリ起動時広告を表示した後、同じオブジェクトを再度使うことはできません。次にアプリ起動時広告をリクエストするには、新しい AppOpenAd オブジェクトを作成する必要があります。

次のインプレッションの機会に備えてアプリ起動時広告を用意するには、広告イベント OnAdFullScreenContentClosed または OnAdFullScreenContentFailed が発生した時点でアプリ起動時広告をプリロードするようにします。

private void RegisterReloadHandler(AppOpenAd ad)
{
    ...
    // Raised when the ad closed full screen content.
    ad.OnAdFullScreenContentClosed += ()
    {
        Debug.Log("App open ad full screen content closed.");

        // Reload the ad so that we can show another as soon as possible.
        LoadAppOpenAd();
    };
    // Raised when the ad failed to open full screen content.
    ad.OnAdFullScreenContentFailed += (AdError error) =>
    {
        Debug.LogError("App open ad failed to open full screen content " +
                       "with error : " + error);

        // Reload the ad so that we can show another as soon as possible.
        LoadAppOpenAd();
    };
}

コールド スタートと読み込み画面

これまでのドキュメントでは、メモリ内でアプリが一時停止しているときにユーザーがアプリをフォアグラウンド表示したときにのみ、アプリ起動時広告が表示されることを前提としています。「コールド スタート」は、アプリを起動したが、以前にメモリで一時停止していなかったときに発生します。

コールド スタートの例としては、ユーザーが初めてアプリを開いた場合が挙げられます。コールド スタートでは、以前に読み込まれたアプリ起動時広告を直ちに表示することはできません。広告をリクエストしてから広告が返されるまでには、遅延が発生するため、ユーザーがアプリを少しの間だけアプリを使用できる状態になっていて、コンテキスト外の広告に驚かれる可能性があります。ユーザー エクスペリエンスが低下するため、これを避ける必要があります。

コールド スタートでアプリ起動時広告を使用する場合、読み込み画面を使用してゲームまたはアプリアセットを読み込むこと、そして読み込み画面からのみ広告を表示することが推奨されます。アプリの読み込みが完了し、アプリのメイン コンテンツにユーザーを誘導した場合は、広告を表示しない。

ベスト プラクティス

アプリ起動時広告は、アプリの初回起動時とアプリの切り替え時にアプリの読み込み画面を収益化する際に役立ちますが、ユーザーがアプリを快適に利用できるよう、以下のベスト プラクティスを念頭に置くことが重要です。

  • ユーザーがアプリを数回使用した後に、アプリ起動時広告を初めて表示する。
  • 本来はユーザーがアプリの読み込みを待っているときに、アプリ起動時広告を表示します。
  • アプリ起動時広告の下に読み込み画面があり、広告が閉じられる前に読み込み画面の読み込みが完了した場合は、OnAdDidDismissFullScreenContent イベント ハンドラで読み込み画面を閉じます。
  • iOS プラットフォームでは、AppStateEventNotifierAppStateEventClient GameObject をインスタンス化します。この GameObject はイベントの呼び出しに必要なため、破棄しないでください。GameObject が破棄されると、イベントの呼び出しは停止します。

補足資料