To discuss and provide feedback on our products, join the official AdMob Discord channel in the Google Advertising and Measurement Community server.

Interstitial ads custom events

  • To request an interstitial ad with a custom event, create a class extending Adapter and implement the loadInterstitialAd() method, delegating the loading process to a separate loader class.

  • The loader class is responsible for loading the ad, implementing the MediationInterstitialAd interface, and handling ad event callbacks to the Google Mobile Ads SDK.

  • Upon successful ad fetch, the loader invokes onSuccess() on the MediationAdLoadCallback, passing an instance of the MediationInterstitialAd implementation.

  • The MediationInterstitialAdCallback 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.

  • A full example demonstrating custom event implementation for interstitial ads is available on GitHub for reference and adaptation.

Select platform: Android iOS

Prerequisites

Complete the custom events setup.

Request an interstitial ad

When the custom event line item is reached in the waterfall mediation chain, the loadInterstitialAd() method is called on the class name you provided when creating a custom event. In this case, that method is in SampleCustomEvent, which then calls the loadInterstitialAd() method in SampleInterstitialCustomEventLoader.

To request an interstitial ad, create or modify a class that extends Adapter to implement loadInterstitialAd(). Additionally, create a new class to implement MediationInterstitialAd.

In our custom event example, SampleCustomEvent extends the Adapter class and then delegates to SampleInterstitialCustomEventLoader.

Java

packagecom.google.ads.mediation.sample.customevent;
importcom.google.android.gms.ads.mediation.Adapter;
importcom.google.android.gms.ads.mediation.MediationAdConfiguration;
importcom.google.android.gms.ads.mediation.MediationAdLoadCallback;
importcom.google.android.gms.ads.mediation.MediationInterstitialAd;
importcom.google.android.gms.ads.mediation.MediationInterstitialAdCallback;
...
publicclass SampleCustomEventextendsAdapter{
privateSampleInterstitialCustomEventLoaderinterstitialLoader;
@Override
publicvoidloadInterstitialAd(
@NonNullMediationInterstitialAdConfigurationadConfiguration,
@NonNull
MediationAdLoadCallback<MediationInterstitialAd,MediationInterstitialAdCallback>
callback){
interstitialLoader=newSampleInterstitialCustomEventLoader(adConfiguration,callback);
interstitialLoader.loadAd();
}
}

SampleInterstitialCustomEventLoader is responsible for the following tasks:

  • Loading the interstitial ad and invoking a MediationAdLoadCallback method once loading completes.

  • Implementing the MediationInterstitialAd interface.

  • Receiving and reporting ad event callbacks to Google Mobile Ads SDK.

The optional parameter defined in the AdMob UI is included in the ad configuration. The parameter can be accessed through adConfiguration.getServerParameters().getString(MediationConfiguration.CUSTOM_EVENT_SERVER_PARAMETER_FIELD). This parameter is typically an ad unit identifier that an ad network SDK requires when instantiating an ad object.

Java

packagecom.google.ads.mediation.sample.customevent;
importcom.google.android.gms.ads.mediation.Adapter;
importcom.google.android.gms.ads.mediation.MediationInterstitialAdConfiguration;
importcom.google.android.gms.ads.mediation.MediationAdLoadCallback;
importcom.google.android.gms.ads.mediation.MediationInterstitialAd;
importcom.google.android.gms.ads.mediation.MediationInterstitialAdCallback;
...
publicclass SampleInterstitialCustomEventLoaderextendsSampleAdListener
implementsMediationInterstitialAd{
/** A sample third-party SDK interstitial ad. */
privateSampleInterstitialsampleInterstitialAd;
/** Configuration for requesting the interstitial ad from the third-party network. */
privatefinalMediationInterstitialAdConfigurationmediationInterstitialAdConfiguration;
/** Callback for interstitial ad events. */
privateMediationInterstitialAdCallbackinterstitialAdCallback;
/** Callback that fires on loading success or failure. */
privatefinalMediationAdLoadCallback<MediationInterstitialAd,MediationInterstitialAdCallback>
mediationAdLoadCallback;
/** Constructor. */
publicSampleInterstitialCustomEventLoader(
@NonNullMediationInterstitialAdConfigurationmediationInterstitialAdConfiguration,
@NonNullMediationAdLoadCallback<MediationInterstitialAd,MediationInterstitialAdCallback>
mediationAdLoadCallback){
this.mediationInterstitialAdConfiguration=mediationInterstitialAdConfiguration;
this.mediationAdLoadCallback=mediationAdLoadCallback;
}
/** Loads the interstitial ad from the third-party ad network. */
publicvoidloadAd(){
// 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.");
StringserverParameter=mediationInterstitialAdConfiguration.getServerParameters().getString(
MediationConfiguration.CUSTOM_EVENT_SERVER_PARAMETER_FIELD);
Log.d("InterstitialCustomEvent","Received server parameter.");
sampleInterstitialAd=
newSampleInterstitial(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));
}
publicSampleAdRequestcreateSampleRequest(
MediationAdConfigurationmediationAdConfiguration){
SampleAdRequestrequest=newSampleAdRequest();
request.setTestMode(mediationAdConfiguration.isTestRequest());
request.setKeywords(mediationAdConfiguration.getMediationExtras().keySet());
returnrequest;
}
}

Depending on whether the ad is successfully fetched or encounters an error, you would call either onSuccess() or onFailure(). onSuccess() is called by passing in an instance of the class that implements MediationInterstitialAd.

Typically, these methods are implemented inside callbacks from the third-party SDK your adapter implements. For this example, the Sample SDK has a SampleAdListener with relevant callbacks:

Java

@Override
publicvoidonAdFetchSucceeded(){
interstitialAdCallback=mediationAdLoadCallback.onSuccess(this);
}
@Override
publicvoidonAdFetchFailed(SampleErrorCodeerrorCode){
mediationAdLoadCallback.onFailure(SampleCustomEventError.createSampleSdkError(errorCode));
}

MediationInterstitialAd requires implementing a showAd() method to display the ad:

Java

@Override
publicvoidshowAd(@NonNullContextcontext){
sampleInterstitialAd.show();
}

Forward mediation events to Google Mobile Ads SDK

Once onSuccess() is called, the returned MediationInterstitialAdCallback object can then be used by the adapter to forward presentation events from the third-party SDK to Google Mobile Ads SDK. The SampleInterstitialCustomEventLoader class extends the SampleAdListener interface to forward callbacks from the sample ad network to the Google Mobile Ads SDK.

It's important that your custom event forwards as many of these callbacks as possible, so that your app receives these equivalent events from the Google Mobile Ads SDK. Here's an example of using callbacks:

Java

@Override
publicvoidonAdFullScreen(){
interstitialAdCallback.reportAdImpression();
interstitialAdCallback.onAdOpened();
}
@Override
publicvoidonAdClosed(){
interstitialAdCallback.onAdClosed();
}

This completes the custom events implementation for interstitial ads. The full example is available on GitHub. You can use it with an ad network that is already supported or modify it to display custom event interstitial ads.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025年10月15日 UTC.