リワード インタースティシャル広告(ベータ版)

リワード インタースティシャルは、アプリの画面が変わる自然なタイミングで自動的に表示される広告に対して報酬を提供できる、インセンティブ広告フォーマットの一種です。リワード広告とは異なり、ユーザーはリワード インタースティシャルを表示するためにオプトインする必要はありません。

前提条件

先に進む前に、Google Mobile Ads Unity Plugin を設定してください。

常にテスト広告でテストする

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

ただし、AdMob ウェブ インターフェースでアプリを登録し、アプリで使用する独自の広告ユニット ID を作成した後は、開発中に使用するデバイスを明示的にテストデバイスとして設定する必要があります。

Android

ca-app-pub-3940256099942544/1033173712

iOS

ca-app-pub-3940256099942544/4411468910

Google Mobile Ads Unity Plugin を初期化する

広告を読み込む前に MobileAds.Initialize() を呼び出して、アプリで Google Mobile Ads Unity Plugin を初期化します。この処理は 1 回だけ行います(アプリの起動時に行うのが理想的です)。

using GoogleMobileAds;
using GoogleMobileAds.Api;

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

メディエーションを使用している場合、すべてのメディエーション アダプターを確実に初期化できるよう、広告の読み込みはコールバックが発生してから行います。

リワード インタースティシャル広告を読み込む

リワード インタースティシャル広告を読み込むには、RewardedInterstitialAd クラスで静的 Load メソッドを使用します。読み込まれた RewardedInterstitialAd オブジェクトは、完了ハンドラのパラメータとして提供されます。次のサンプルでは、リワード インタースティシャル広告を読み込んでいます。

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

// Send the request to load the ad.
RewardedInterstitialAd.Load(AD_UNIT_ID, adRequest,
    (RewardedInterstitialAd ad, LoadAdError error) =>
    {
        if (error != null)
        {
            // The ad failed to load.
            Debug.LogError($"Rewarded interstitial ad failed to load: {error}");
            return;
        }
        // The ad loaded successfully.
        Debug.Log("Rewarded interstitial ad loaded.");
    });

AD_UNIT_ID は、実際の広告ユニット ID に置き換えてください。

[任意] サーバーサイド認証(SSV)コールバックを検証する

サーバーサイド認証コールバックで追加データを必要とする場合は、リワード インタースティシャル広告のカスタムデータ機能を使用してください。リワード インタースティシャル広告オブジェクトに設定されている文字列値はすべて、SSV コールバックの custom_data クエリ パラメータに渡されます。カスタムデータ値が設定されていない場合、SSV コールバックは custom_data クエリ パラメータ値を持ちません。

次のコード例は、リワード インタースティシャル広告の読み込み後に SSV の各種オプションを設定する方法を示したものです。

// Create and pass the SSV options to the rewarded interstitial ad.
var options = new ServerSideVerificationOptions
{
    CustomData = "SAMPLE_CUSTOM_DATA_STRING"
};

rewardedInterstitialAd.SetServerSideVerificationOptions(options);

SAMPLE_CUSTOM_DATA_STRINGは、実際のカスタムデータに置き換えてください。

カスタム リワード文字列を設定する場合は、広告を表示する前に行う必要があります。

リワード インタースティシャル広告を報酬のコールバック付きで表示する

広告を表示する際には、ユーザーへの報酬を処理するコールバックを指定する必要があります。読み込んだ広告は 1 回だけ表示できます。広告を表示する準備ができていることを確認するには、CanShowAd メソッドを使用します。

次のコードは、リワード インタースティシャル広告を表示しています。

if (rewardedInterstitialAd != null && rewardedInterstitialAd.CanShowAd())
{
    rewardedInterstitialAd.Show((Reward reward) =>
    {
        // The ad was shown and the user earned a reward.
        Debug.Log($"Rewarded interstitial ad rewarded user: {reward.Type}, {reward.Amount}");
    });
}

リワード インタースティシャル広告のイベントをリッスンする

広告の動作をより細かくカスタマイズするには、広告のライフサイクルで生じるさまざまなイベントを利用します。次の例では、広告イベントをリッスンしています。

rewardedInterstitialAd.OnAdPaid += (AdValue adValue) =>
{
    // Raised when the ad is estimated to have earned money.
    Debug.Log($"Rewarded interstitial ad paid {adValue.Value} {adValue.CurrencyCode}.");
};
rewardedInterstitialAd.OnAdImpressionRecorded += () =>
{
    // Raised when an impression is recorded for an ad.
    Debug.Log("Rewarded interstitial ad recorded an impression.");
};
rewardedInterstitialAd.OnAdClicked += () =>
{
    // Raised when a click is recorded for an ad.
    Debug.Log("Rewarded interstitial ad was clicked.");
};
rewardedInterstitialAd.OnAdFullScreenContentOpened += () =>
{
    // Raised when the ad opened full screen content.
    Debug.Log("Rewarded interstitial ad full screen content opened.");
};
rewardedInterstitialAd.OnAdFullScreenContentClosed += () =>
{
    // Raised when the ad closed full screen content.
    Debug.Log("Rewarded interstitial ad full screen content closed.");
};
rewardedInterstitialAd.OnAdFullScreenContentFailed += (AdError error) =>
{
    // Raised when the ad failed to open full screen content.
    Debug.LogError($"Rewarded interstitial ad failed to open full screen content: {error}");
};

リワード インタースティシャル広告をクリーンアップする

RewardedInterstitialAd オブジェクトを使い終えたら、その参照を破棄する前に、必ず Destroy メソッドを呼び出すようにしてください。

if (rewardedInterstitialAd != null)
{
    rewardedInterstitialAd.Destroy();
}

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

次のリワード インタースティシャル広告をプリロードする

RewardedInterstitialAd は使い捨てオブジェクトです。一度リワード インタースティシャル広告を表示した後、同じオブジェクトを再度使うことはできません。次にリワード インタースティシャル広告をリクエストするには、新しい RewardedInterstitialAd オブジェクトを読み込んでください。

次のインプレッション獲得の機会に備えてリワード インタースティシャル広告を用意するには、広告イベント OnAdFullScreenContentClosed または OnAdFullScreenContentFailed が発生した時点でリワード インタースティシャル広告をプリロードするようにします。

rewardedInterstitialAd.OnAdFullScreenContentClosed += () =>
{
    // Reload the ad so that you can show another as soon as possible.
    var adRequest = new AdRequest();
    RewardedInterstitialAd.Load(AD_UNIT_ID, adRequest,
        (RewardedInterstitialAd ad, LoadAdError error) =>
        {
            // Handle ad loading here.
            Debug.Log("Rewarded interstitial ad reloaded.");
        });
};

AD_UNIT_ID は、実際の広告ユニット ID に置き換えてください。

参考情報