插頁式廣告自訂事件
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
必要條件
完成自訂事件設定。
請求插頁式廣告
當刊登序列中介服務鏈執行到自訂事件委刊項,系統會以您在建立自訂事件時指定的類別名稱,呼叫 loadInterstitialAd()
方法。在本例中,該方法位於 SampleCustomEvent
,會接著呼叫 SampleInterstitialCustomEventLoader
中的 loadInterstitialAd()
方法。
如要請求插頁式廣告,請建立或修改 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
負責處理以下工作:
在 AdMob UI 中定義的選用參數會包含在廣告設定,使用 adConfiguration.getServerParameters().getString(MediationConfiguration.CUSTOM_EVENT_SERVER_PARAMETER_FIELD)
即可存取。這項參數通常是廣告單元 ID,廣告聯播網 SDK 在例項化廣告物件時會用到。
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()
。傳遞導入 MediationInterstitialAd
的類別執行個體,即可呼叫 onSuccess()
。
這些方法通常是在轉接程式導入的第三方 SDK 回呼中執行。以本例來說,範例 SDK 的 SampleAdListener
含有相關回呼:
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 上有完整的範例可參考。範例可直接用於支援的廣告聯播網,您也可修改現有範例,顯示自訂事件插頁式廣告。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-09-02 (世界標準時間)。
[null,null,["上次更新時間:2025-09-02 (世界標準時間)。"],[[["\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,["Prerequisites\n\nComplete the [custom events setup](/admob/android/custom-events/setup).\n\nRequest an interstitial ad\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\nJava \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 Google Mobile Ads SDK.\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\nJava \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\nJava \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\nJava \n\n```java\n@Override\npublic void showAd(@NonNull Context context) {\n sampleInterstitialAd.show();\n}\n```\n\nForward mediation events to Google Mobile Ads SDK\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 Google Mobile Ads SDK. 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\nJava \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."]]