インタースティシャル広告のカスタム イベント
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
前提条件
カスタム イベントのセットアップを完了します。
インタースティシャル広告をリクエストする
ウォーターフォール メディエーション チェーンでカスタム イベント広告申込情報に到達すると、カスタム イベントの作成時に指定したクラス名で loadInterstitialAd()
メソッドが呼び出されます。この場合、メソッドは SampleCustomEvent
にあり、loadInterstitialAd()
で SampleInterstitialCustomEventLoader
を呼び出します。
インタースティシャル広告をリクエストするには、Adapter
を拡張して loadInterstitialAd()
を実装するクラスを作成または変更します。また、MediationInterstitialAd
を実装するための新しいクラスを作成します。
カスタム イベントの例では、SampleCustomEvent
が Adapter
クラスを拡張し、その後 SampleInterstitialCustomEventLoader
にデリゲートします。
Java
package com.google.ads.mediation.sample.customevent;
import com.google.android.gms.ads.mediation.Adapter;
import com.google.android.gms.ads.mediation.MediationAdConfiguration;
import com.google.android.gms.ads.mediation.MediationAdLoadCallback;
import com.google.android.gms.ads.mediation.MediationInterstitialAd;
import com.google.android.gms.ads.mediation.MediationInterstitialAdCallback;
...
public class SampleCustomEvent extends Adapter {
private SampleInterstitialCustomEventLoader interstitialLoader;
@Override
public void loadInterstitialAd(
@NonNull MediationInterstitialAdConfiguration adConfiguration,
@NonNull
MediationAdLoadCallback<MediationInterstitialAd, MediationInterstitialAdCallback>
callback) {
interstitialLoader = new SampleInterstitialCustomEventLoader(adConfiguration, callback);
interstitialLoader.loadAd();
}
}
SampleInterstitialCustomEventLoader
の役割は次のとおりです。
インタースティシャル広告を読み込み、読み込みが完了したら MediationAdLoadCallback
メソッドを呼び出します。
MediationInterstitialAd
インターフェースを実装します。
広告イベント コールバックを受信して Google Mobile Ads SDK に報告する。
AdMob の管理画面で定義されたオプション パラメータは、広告設定に含まれます。このパラメータには adConfiguration.getServerParameters().getString(MediationConfiguration.CUSTOM_EVENT_SERVER_PARAMETER_FIELD)
を介してアクセスできます。このパラメータは通常、広告オブジェクトをインスタンス化する際に広告ネットワーク SDK が要求する広告ユニット ID です。
Java
package com.google.ads.mediation.sample.customevent;
import com.google.android.gms.ads.mediation.Adapter;
import com.google.android.gms.ads.mediation.MediationInterstitialAdConfiguration;
import com.google.android.gms.ads.mediation.MediationAdLoadCallback;
import com.google.android.gms.ads.mediation.MediationInterstitialAd;
import com.google.android.gms.ads.mediation.MediationInterstitialAdCallback;
...
public class SampleInterstitialCustomEventLoader extends SampleAdListener
implements MediationInterstitialAd {
/** A sample third-party SDK interstitial ad. */
private SampleInterstitial sampleInterstitialAd;
/** Configuration for requesting the interstitial ad from the third-party network. */
private final MediationInterstitialAdConfiguration mediationInterstitialAdConfiguration;
/** Callback for interstitial ad events. */
private MediationInterstitialAdCallback interstitialAdCallback;
/** Callback that fires on loading success or failure. */
private final MediationAdLoadCallback<MediationInterstitialAd, MediationInterstitialAdCallback>
mediationAdLoadCallback;
/** Constructor. */
public SampleInterstitialCustomEventLoader(
@NonNull MediationInterstitialAdConfiguration mediationInterstitialAdConfiguration,
@NonNull MediationAdLoadCallback<MediationInterstitialAd, MediationInterstitialAdCallback>
mediationAdLoadCallback) {
this.mediationInterstitialAdConfiguration = mediationInterstitialAdConfiguration;
this.mediationAdLoadCallback = mediationAdLoadCallback;
}
/** Loads the interstitial ad from the third-party ad network. */
public void loadAd() {
// All custom events have a server parameter named "parameter" that returns
// back the parameter entered into the UI when defining the custom event.
Log.i("InterstitialCustomEvent", "Begin loading interstitial ad.");
String serverParameter = mediationInterstitialAdConfiguration.getServerParameters().getString(
MediationConfiguration.CUSTOM_EVENT_SERVER_PARAMETER_FIELD);
Log.d("InterstitialCustomEvent", "Received server parameter.");
sampleInterstitialAd =
new SampleInterstitial(mediationInterstitialAdConfiguration.getContext());
sampleInterstitialAd.setAdUnit(serverParameter);
// Implement a SampleAdListener and forward callbacks to mediation.
sampleInterstitialAd.setAdListener(this);
// Make an ad request.
Log.i("InterstitialCustomEvent", "start fetching interstitial ad.");
sampleInterstitialAd.fetchAd(
SampleCustomEvent.createSampleRequest(mediationInterstitialAdConfiguration));
}
public SampleAdRequest createSampleRequest(
MediationAdConfiguration mediationAdConfiguration) {
SampleAdRequest request = new SampleAdRequest();
request.setTestMode(mediationAdConfiguration.isTestRequest());
request.setKeywords(mediationAdConfiguration.getMediationExtras().keySet());
return request;
}
}
広告の取得が成功したかエラーが発生したかに応じて、onSuccess()
または onFailure()
を呼び出します。onSuccess()
は、MediationInterstitialAd
を実装するクラスのインスタンスを渡すことで呼び出されます。
通常、これらのメソッドは、アダプタが実装するサードパーティ SDK のコールバック内に実装されます。この例では、関連するコールバックを含む SampleAdListener
が「Sample SDK」に含まれています。
Java
@Override
public void onAdFetchSucceeded() {
interstitialAdCallback = mediationAdLoadCallback.onSuccess(this);
}
@Override
public void onAdFetchFailed(SampleErrorCode errorCode) {
mediationAdLoadCallback.onFailure(SampleCustomEventError.createSampleSdkError(errorCode));
}
MediationInterstitialAd
では、広告を表示するために showAd()
メソッドを実装する必要があります。
Java
@Override
public void showAd(@NonNull Context context) {
sampleInterstitialAd.show();
}
onSuccess()
が呼び出されると、返された MediationInterstitialAdCallback
オブジェクトをアダプタが使用することにより、プレゼンテーション イベントをサードパーティ SDK から Google Mobile Ads SDK に転送できます。SampleInterstitialCustomEventLoader
クラスは SampleAdListener
インターフェースを拡張して、サンプル広告ネットワークからのコールバックを Google Mobile Ads SDK に転送します。
カスタム イベントには、こうしたコールバックを可能な限り漏れなく転送させましょう。これにより、アプリは同等イベントを Google Mobile Ads SDK から受け取ることができます。コールバックの使用例を以下に示します。
Java
@Override
public void onAdFullScreen() {
interstitialAdCallback.reportAdImpression();
interstitialAdCallback.onAdOpened();
}
@Override
public void onAdClosed() {
interstitialAdCallback.onAdClosed();
}
これで、インタースティシャル広告のカスタム イベントの実装が完了しました。サンプル全体は GitHub で入手できます。このサンプルは、すでにサポートされている広告ネットワークで使用することができます。また、変更を加えてカスタム イベント インタースティシャル広告を表示することもできます。
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-08-31 UTC。
[null,null,["最終更新日 2025-08-31 UTC。"],[[["\u003cp\u003eTo request an interstitial ad with a custom event, create a class extending \u003ccode\u003eAdapter\u003c/code\u003e and implement the \u003ccode\u003eloadInterstitialAd()\u003c/code\u003e method, delegating the loading process to a separate loader class.\u003c/p\u003e\n"],["\u003cp\u003eThe loader class is responsible for loading the ad, implementing the \u003ccode\u003eMediationInterstitialAd\u003c/code\u003e interface, and handling ad event callbacks to the Google Mobile Ads SDK.\u003c/p\u003e\n"],["\u003cp\u003eUpon successful ad fetch, the loader invokes \u003ccode\u003eonSuccess()\u003c/code\u003e on the \u003ccode\u003eMediationAdLoadCallback\u003c/code\u003e, passing an instance of the \u003ccode\u003eMediationInterstitialAd\u003c/code\u003e implementation.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eMediationInterstitialAdCallback\u003c/code\u003e is then used to forward ad events, such as impressions and clicks, back to the Google Mobile Ads SDK, ensuring consistent ad tracking and reporting.\u003c/p\u003e\n"],["\u003cp\u003eA full example demonstrating custom event implementation for interstitial ads is available on GitHub for reference and adaptation.\u003c/p\u003e\n"]]],[],null,["# Interstitial ads custom events\n\nPrerequisites\n-------------\n\nComplete the [custom events setup](/admob/android/custom-events/setup).\n\nRequest an interstitial ad\n--------------------------\n\nWhen the custom event line item is reached in the waterfall mediation chain,\nthe `loadInterstitialAd()` method is called on the class name you provided when\n[creating a custom\nevent](/admob/android/custom-events/setup#create). In this case,\nthat method is in `SampleCustomEvent`, which then calls the\n`loadInterstitialAd()` method in `SampleInterstitialCustomEventLoader`.\n\nTo request an interstitial ad, create or modify a class that extends `Adapter`\nto implement `loadInterstitialAd()`. Additionally, create a new class to\nimplement `MediationInterstitialAd`.\n\nIn our [custom event example](//github.com/googleads/googleads-mobile-android-mediation/blob/main/Example/customevent/src/main/java/com/google/ads/mediation/sample/customevent/SampleCustomEvent.java),\n`SampleCustomEvent` extends the `Adapter` class and then delegates to\n`SampleInterstitialCustomEventLoader`. \n\n### Java\n\n```java\npackage com.google.ads.mediation.sample.customevent;\n\nimport com.google.android.gms.ads.mediation.Adapter;\nimport com.google.android.gms.ads.mediation.MediationAdConfiguration;\nimport com.google.android.gms.ads.mediation.MediationAdLoadCallback;\nimport com.google.android.gms.ads.mediation.MediationInterstitialAd;\nimport com.google.android.gms.ads.mediation.MediationInterstitialAdCallback;\n...\n\npublic class SampleCustomEvent extends Adapter {\n private SampleInterstitialCustomEventLoader interstitialLoader;\n @Override\n public void loadInterstitialAd(\n @NonNull MediationInterstitialAdConfiguration adConfiguration,\n @NonNull\n MediationAdLoadCallback\u003cMediationInterstitialAd, MediationInterstitialAdCallback\u003e\n callback) {\n interstitialLoader = new SampleInterstitialCustomEventLoader(adConfiguration, callback);\n interstitialLoader.loadAd();\n }\n}\n```\n\n`SampleInterstitialCustomEventLoader` is responsible for the following tasks:\n\n- Loading the interstitial ad and invoking a\n [`MediationAdLoadCallback`](/admob/android/reference/com/google/android/gms/ads/mediation/MediationAdLoadCallback)\n method once loading completes.\n\n- Implementing the `MediationInterstitialAd` interface.\n\n- Receiving and reporting ad event callbacks to\n\n Google Mobile Ads SDK\n\n .\n\nThe optional parameter defined in the AdMob UI is\nincluded in the ad configuration. The parameter can be accessed through\n`adConfiguration.getServerParameters().getString(MediationConfiguration.CUSTOM_EVENT_SERVER_PARAMETER_FIELD)`.\nThis parameter is typically an ad unit identifier that an ad network SDK\nrequires when instantiating an ad object. \n\n### Java\n\n```java\npackage com.google.ads.mediation.sample.customevent;\n\nimport com.google.android.gms.ads.mediation.Adapter;\nimport com.google.android.gms.ads.mediation.MediationInterstitialAdConfiguration;\nimport com.google.android.gms.ads.mediation.MediationAdLoadCallback;\nimport com.google.android.gms.ads.mediation.MediationInterstitialAd;\nimport com.google.android.gms.ads.mediation.MediationInterstitialAdCallback;\n...\n\npublic class SampleInterstitialCustomEventLoader extends SampleAdListener\n implements MediationInterstitialAd {\n\n /** A sample third-party SDK interstitial ad. */\n private SampleInterstitial sampleInterstitialAd;\n\n /** Configuration for requesting the interstitial ad from the third-party network. */\n private final MediationInterstitialAdConfiguration mediationInterstitialAdConfiguration;\n\n /** Callback for interstitial ad events. */\n private MediationInterstitialAdCallback interstitialAdCallback;\n\n /** Callback that fires on loading success or failure. */\n private final MediationAdLoadCallback\u003cMediationInterstitialAd, MediationInterstitialAdCallback\u003e\n mediationAdLoadCallback;\n\n /** Constructor. */\n public SampleInterstitialCustomEventLoader(\n @NonNull MediationInterstitialAdConfiguration mediationInterstitialAdConfiguration,\n @NonNull MediationAdLoadCallback\u003cMediationInterstitialAd, MediationInterstitialAdCallback\u003e\n mediationAdLoadCallback) {\n this.mediationInterstitialAdConfiguration = mediationInterstitialAdConfiguration;\n this.mediationAdLoadCallback = mediationAdLoadCallback;\n }\n\n /** Loads the interstitial ad from the third-party ad network. */\n public void loadAd() {\n // All custom events have a server parameter named \"parameter\" that returns\n // back the parameter entered into the UI when defining the custom event.\n Log.i(\"InterstitialCustomEvent\", \"Begin loading interstitial ad.\");\n String serverParameter = mediationInterstitialAdConfiguration.getServerParameters().getString(\n MediationConfiguration.CUSTOM_EVENT_SERVER_PARAMETER_FIELD);\n Log.d(\"InterstitialCustomEvent\", \"Received server parameter.\");\n\n sampleInterstitialAd =\n new SampleInterstitial(mediationInterstitialAdConfiguration.getContext());\n sampleInterstitialAd.setAdUnit(serverParameter);\n\n // Implement a SampleAdListener and forward callbacks to mediation.\n sampleInterstitialAd.setAdListener(this);\n\n // Make an ad request.\n Log.i(\"InterstitialCustomEvent\", \"start fetching interstitial ad.\");\n sampleInterstitialAd.fetchAd(\n SampleCustomEvent.createSampleRequest(mediationInterstitialAdConfiguration));\n }\n\npublic SampleAdRequest createSampleRequest(\n MediationAdConfiguration mediationAdConfiguration) {\n SampleAdRequest request = new SampleAdRequest();\n request.setTestMode(mediationAdConfiguration.isTestRequest());\n request.setKeywords(mediationAdConfiguration.getMediationExtras().keySet());\n return request;\n }\n}\n```\n\nDepending on whether the ad is successfully fetched or encounters an error, you\nwould call either\n[`onSuccess()`](/admob/android/reference/com/google/android/gms/ads/mediation/MediationAdLoadCallback#onSuccess(MediationAdT))\nor\n[`onFailure()`](/admob/android/reference/com/google/android/gms/ads/mediation/MediationAdLoadCallback#onFailure(com.google.android.gms.ads.AdError)).\n`onSuccess()` is called by passing in an instance of the class that implements\n`MediationInterstitialAd`.\n\nTypically, these methods are implemented inside callbacks from the\nthird-party SDK your adapter implements. For this example, the Sample SDK\nhas a `SampleAdListener` with relevant callbacks: \n\n### Java\n\n```java\n@Override\npublic void onAdFetchSucceeded() {\n interstitialAdCallback = mediationAdLoadCallback.onSuccess(this);\n}\n\n@Override\npublic void onAdFetchFailed(SampleErrorCode errorCode) {\n mediationAdLoadCallback.onFailure(SampleCustomEventError.createSampleSdkError(errorCode));\n}\n```\n\n`MediationInterstitialAd` requires implementing a `showAd()` method to display\nthe ad: \n\n### Java\n\n```java\n@Override\npublic void showAd(@NonNull Context context) {\n sampleInterstitialAd.show();\n}\n```\n\nForward mediation events to\nGoogle Mobile Ads SDK\n-------------------------------------------------\n\nOnce `onSuccess()` is called, the returned `MediationInterstitialAdCallback`\nobject can then be used by the adapter to forward presentation events from the\nthird-party SDK to\n\nGoogle Mobile Ads SDK\n\n. The\n`SampleInterstitialCustomEventLoader` class extends the `SampleAdListener`\ninterface to forward callbacks from the sample ad network to the Google Mobile\nAds SDK.\n\nIt's important that your custom event forwards as many of these callbacks as\npossible, so that your app receives these equivalent events from the Google\nMobile Ads SDK. Here's an example of using callbacks: \n\n### Java\n\n```java\n@Override\npublic void onAdFullScreen() {\n interstitialAdCallback.reportAdImpression();\n interstitialAdCallback.onAdOpened();\n}\n\n@Override\npublic void onAdClosed() {\n interstitialAdCallback.onAdClosed();\n}\n```\n\nThis completes the custom events implementation for interstitial ads. The full\nexample is available on\n[GitHub](//github.com/googleads/googleads-mobile-android-mediation/tree/master/Example/customevent/src/main/java/com/google/ads/mediation/sample/customevent).\nYou can use it with an ad network that is already supported or modify it to\ndisplay custom event interstitial ads."]]