This guide is intended for publishers looking to use AdMob Mediation to do the following:
- Show ads from a network that is not directly supported in the AdMob user interface.
- Show a custom view instead of an ad.
Custom events allow you to write a custom mediation adapter to place any view into your ad space. You can find the full source for this project on GitHub.
Prerequisites
- The Google Mobile Ads SDK must be integrated into your app to use AdMob to display ads.
- You may also want to read about requesting a banner ad and how Mediation works before proceeding.
Banner custom event
In the following example, you'll first create a banner custom event within AdMob
Mediation.
This requires defining a custom event
that points to that specific class in your app
through the AdMob interface,
then implementing a
CustomEventBanner
to serve a view.
This example defines a custom event to display ads from the
sample ad network,
as in the Mediation guide.
Step 1: Define a custom event
The custom event must be defined in the AdMob interface. Follow these instructions to create a custom event.
Here is a screenshot showing some sample custom event settings:
Step 2: Request a banner
Define a class that implements CustomEventBanner
and call it SampleCustomEventBanner
.
When the custom event is chosen from the ad mediation flow,
the requestBannerAd()
method is called
on the class name you provided in the settings.
You can use the parameters provided in this method
to make a banner request to your desired network.
Your custom event must notify AdMob Mediation via the
CustomEventBannerListener
interface when it loads or fails to load an ad. Otherwise, the custom event
times out, and ad mediation moves on to the next network.
Implement lifecycle methods as appropriate.
AdMob Mediation forwards the adapter of
onPause()
and
onResume()
activity events if the user calls the
AdView.pause()
and
AdView.resume()
methods. The sample ad network doesn't include a pause or resume call, so it
provides an empty implementation. Mediation makes its best attempt to call
onDestroy()
when the adapter is about to be destroyed. Perform any necessary cleanup here.
Here's an example SampleCustomEventBanner.java
implementation:
public class SampleCustomEventBanner implements CustomEventBanner {
/** The {@link SampleAdView} representing a banner ad. */
private SampleAdView sampleAdView;
/** The event is being destroyed. Perform any necessary cleanup here. */
@Override
public void onDestroy() {
if (sampleAdView != null) {
sampleAdView.destroy();
}
}
/**
* The app is being paused. This call is only forwarded to the adapter if the developer
* notifies AdMob Mediation that the app is being paused.
*/
@Override
public void onPause() {
// The sample ad network doesn't have an onPause method, so it does nothing.
}
/**
* The app is being resumed. This call is only forwarded to the
* adapter if the developer notifies AdMob Mediation that the app is
* being resumed.
*/
@Override
public void onResume() {
// The sample ad network doesn't have an onResume method, so it does nothing.
}
@Override
public void requestBannerAd(Context context,
CustomEventBannerListener listener,
String serverParameter,
AdSize size,
MediationAdRequest mediationAdRequest,
Bundle customEventExtras) {
sampleAdView = new SampleAdView(context);
// Assumes that the serverParameter is the AdUnit for the Sample Network.
sampleAdView.setAdUnit(serverParameter);
sampleAdView.setSize(new SampleAdSize(size.getWidth(), size.getHeight()));
// Implement a SampleAdListener and forward callbacks to AdMob Mediation.
// The callback forwarding is handled by SampleBannerEventFowarder.
sampleAdView.setAdListener(new SampleCustomBannerEventForwarder(listener, sampleAdView));
// Make an ad request.
sampleAdView.fetchAd(createSampleRequest(mediationAdRequest));
}
private SampleAdRequest createSampleRequest(MediationAdRequest mediationAdRequest) {
SampleAdRequest request = new SampleAdRequest();
request.setTestMode(mediationAdRequest.isTesting());
request.setKeywords(mediationAdRequest.getKeywords());
return request;
}
}
Step 3: Notify AdMob Mediation
Implement the ad listener for your network and invoke the relevant callbacks on
CustomEventBannerListener
to send messages back to AdMob Mediation.
We've created the
SampleCustomBannerEventForwarder
class, implementing the SampleAdListener
interface,
to forward callbacks from the sample ad network.
AdMob Mediation supports the following callbacks:
Method | When to call |
---|---|
onAdLoaded() |
The banner request succeeded. |
onAdFailedToLoad() |
The banner request failed. |
onAdClicked() |
The banner was clicked. |
onAdOpened() |
The banner is rendering a full-screen view. |
onAdClosed() |
The user returns to the app after clicking on a banner. |
onAdLeftApplication() |
The banner caused the user to leave the app. |
You are required to notify AdMob Mediation of all callbacks.
Here's an example SampleCustomBannerEventForwarder.java
implementation:
public class SampleCustomBannerEventForwarder extends SampleAdListener { private CustomEventBannerListener mBannerListener; private SampleAdView mAdView; /** * Creates a new {@code SampleBannerEventForwarder}. * @param listener An AdMob Mediation {@link CustomEventBannerListener} that should receive * forwarded events. * @param adView A {@link SampleAdView}. */ public SampleCustomBannerEventForwarder( CustomEventBannerListener listener, SampleAdView adView) { this.mBannerListener = listener; this.mAdView = adView; } @Override public void onAdFetchSucceeded() { mBannerListener.onAdLoaded(mAdView); } @Override public void onAdFetchFailed(SampleErrorCode errorCode) { switch (errorCode) { case UNKNOWN: mBannerListener.onAdFailedToLoad(AdRequest.ERROR_CODE_INTERNAL_ERROR); break; case BAD_REQUEST: mBannerListener.onAdFailedToLoad(AdRequest.ERROR_CODE_INVALID_REQUEST); break; case NETWORK_ERROR: mBannerListener.onAdFailedToLoad(AdRequest.ERROR_CODE_NETWORK_ERROR); break; case NO_INVENTORY: mBannerListener.onAdFailedToLoad(AdRequest.ERROR_CODE_NO_FILL); break; } } @Override public void onAdFullScreen() { mBannerListener.onAdClicked(); mBannerListener.onAdOpened(); // Only call onAdLeftApplication if your ad network actually exits the developer's app. mBannerListener.onAdLeftApplication(); } @Override public void onAdClosed() { mBannerListener.onAdClosed(); } }
Interstitial custom event
To implement an interstitial custom event, you first create an interstitial
custom event within AdMob Mediation, much like a banner custom event. Then
implement a
CustomEventInterstitial
to provide notification. This example uses the sample ad network as before.
Step 1: Define a custom event
You can define an interstitial custom event through the AdMob Interface. Make sure the value you give for the Class Name has a full path. Parameter should contain any information needed to make an ad request to the network you're implementing in the custom event.
Step 2: Request an interstitial
Define a class that implements CustomEventInterstitial
; we'll call this class
SampleCustomEventInterstitial
. When the custom event is chosen from the
mediation flow, mediation calls the
requestInterstitialAd()
method on the class name you provided in the settings. You can use the
parameters provided in this method to make an interstitial request to your
desired network. The following example shows how to request an interstitial from
the sample ad network via a custom event:
Here's an example SampleCustomEventInterstitial.java
implementation:
public class SampleCustomEventInterstitial implements CustomEventInterstitial {
/** Represents a {@link SampleInterstitial}. */
private SampleInterstitial sampleInterstitial;
@Override
public void requestInterstitialAd(Context context,
CustomEventInterstitialListener listener,
String serverParameter,
MediationAdRequest mediationAdRequest,
Bundle customEventExtras) {
/**
* In this method, you should:
* 1. Create your interstitial ad.
* 2. Set your ad network's listener.
* 3. Make an ad request.
*/
sampleInterstitial = new SampleInterstitial(context);
// Here we're assuming the serverParameter is the ad unit for the sample ad network.
sampleInterstitial.setAdUnit(serverParameter);
// Implement a SampleAdListener and forward callbacks to AdMob Mediation.
sampleInterstitial.setAdListener(new SampleCustomInterstitialEventForwarder(listener));
// Make an ad request.
sampleInterstitial.fetchAd(createSampleRequest(mediationAdRequest));
}
/**
* Helper method to create a {@link SampleAdRequest}.
* @param mediationAdRequest The mediation request with targeting information.
* @return The created {@link SampleAdRequest}.
*/
private SampleAdRequest createSampleRequest(MediationAdRequest mediationAdRequest) {
SampleAdRequest request = new SampleAdRequest();
request.setTestMode(mediationAdRequest.isTesting());
request.setKeywords(mediationAdRequest.getKeywords());
return request;
}
@Override
public void showInterstitial() {
// Show your interstitial ad.
sampleInterstitial.show();
}
}
The interstitial custom event interface requires you to implement the
showInterstitial()
method. Mediation invokes this method when you tell the
Google Mobile Ads SDK to show the interstitial ad.
Step 3: Notify AdMob Mediation
Just as with the banner custom event example, implement your network's ad listener to send messages back to AdMob Mediation.
This example SampleCustomInterstitialEventForwarder
class
implements the SampleAdListener
interface to forward the callbacks
from the sample ad network:
public class SampleCustomInterstitialEventForwarder extends SampleAdListener { private CustomEventInterstitialListener mInterstitialListener; /** * Creates a new {@code SampleInterstitialEventForwarder}. * @param listener An AdMob Mediation {@link CustomEventInterstitialListener} that should * receive forwarded events. */ public SampleCustomInterstitialEventForwarder(CustomEventInterstitialListener listener) { this.mInterstitialListener = listener; } @Override public void onAdFetchSucceeded() { mInterstitialListener.onAdLoaded(); } @Override public void onAdFetchFailed(SampleErrorCode errorCode) { switch (errorCode) { case UNKNOWN: mInterstitialListener.onAdFailedToLoad(AdRequest.ERROR_CODE_INTERNAL_ERROR); break; case BAD_REQUEST: mInterstitialListener.onAdFailedToLoad(AdRequest.ERROR_CODE_INVALID_REQUEST); break; case NETWORK_ERROR: mInterstitialListener.onAdFailedToLoad(AdRequest.ERROR_CODE_NETWORK_ERROR); break; case NO_INVENTORY: mInterstitialListener.onAdFailedToLoad(AdRequest.ERROR_CODE_NO_FILL); break; } } @Override public void onAdFullScreen() { mInterstitialListener.onAdOpened(); // Only call onAdLeftApplication if your ad network actually exits the developer's app. mInterstitialListener.onAdLeftApplication(); } @Override public void onAdClosed() { mInterstitialListener.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 interstitial ads.