To use PubMatic as a client-side demand source, publishers can use third-party mediation support in MoPub SDK to integrate PubMatic Android SDK for Banner, Interstitial & Native ads. PubMatic provides a client-side mediation adapter to integrate PubMatic SDK with MoPub SDK. Publishers can easily integrate PubMatic adapter and MoPub SDK into mobile applications with minimal code changes.
This document provides guidelines for writing PubMatic adapters for MoPub.
The following diagram illustrates the call flow of mediation ad serving.
Mediation Ad Serving Call Flow
Mediation Ad Serving call flow is as following:
- MoPub SDK integrated with application, requests an ad from MoPub ad server.
- MoPub ad server responds back with mediation response containing Custom Event Class name (Please refer section below) and server extra parameters containing PubMatic’s pubId, siteId & adId.
- On receiving mediation response, MoPub SDK initializes PubMatic adapter using class name, and pass all server extra parameters.
- PubMatic adapter initializes PubMatic SDK with server extra parameters.
- PubMatic SDK then makes call to PubMatic ad server requesting ad.
- PubMatic ad server responds with ad.
- PubMatic SDK delegates control to the adapter.
- Adapter then hands over rendered ad to MoPub SDK & informs the result with standard callbacks.
Configuring the PubMatic Network on MoPub Portal
Use the following steps to configure a Custom Event on the MoPub portal:
- Log in into your MoPub account.
- Navigate to the Networks tab and click Add a Network.
On the Add a network pop-up, click Custom Native Network.
NoteNative Network does not have any relation to Native Ad format.
Give a suitable name to PubMatic custom network.
In the Set Up Your Inventory section, add the custom event class and data only for the native ad unit for which you want to enable PubMatic network.
In the custom event class field add the fully qualified class name for the respective subclass of the MoPub CustomEvent class
Use the steps below to add the Custom Event Class Data field in JSON containing PubMatic placement details:
For all ad formats: Enter pubId, siteId & adId provided by your PubMatic account manager, in the following format.
Please enter the Custom Event Class name and data exactly in the same format mentioned above. Any changes in these may cause failure in initializing PubMatic adapter.
- Save the network. Please note that it may take from 15 to 30 minutes for MoPub ad server to reflect this change.
PubMatic Banner Adapter for MoPub SDK
- Create a subclass of CustomEventBanner in your application. (for example, PubMaticBannerAdapter)
CustomEventBanner::loadBannermethod and implement code that requests a PubMatic Banner Ad. Please refer to the PubMatic SDK Guide to create an instance of
PMBannerAdViewand sending an Ad request. Please refer code to the following code:
When your ad loads successfully, you must notify MoPub of the event by calling the
CustomEventBannerListener::onBannerLoaded method. Please refer to the following code:
Similarly, you must notify Mopub when PubMatic's ad fails to load by calling
ActivityListenerand register it to get the lifecycle callbacks from PubMatic. Notify Mopub of user interaction events via the corresponding callback methods:
- Finally, on the MoPub web interface, create a network with the "Custom Native Network" type. Place the fully classified class name of your custom adapter (for example,
PubMaticBannerAdapter) in the “Custom Event Class" column.
Once you've completed these steps, the MoPub SDK will be able to automatically instantiate your CustomEventBanner subclass when your application is running. You do not need to instantiate any of these sub classes in your application code.
PubMatic Interstitial Adapter for MoPub SDK
- Create a subclass of CustomEventInterstitial in your application; for exmaple,
CustomEventInterstitial::loadInterstitialmethod and implement code that requests a PubMatic interstitial Ad. See PubMatic SDK Guide to create instance of
PMInterstitialAdViewand sending Ad request. Please refer code below.
When the ad loads successfully you must notify Mopub of the event by calling the CustomEventInterstitial::onInterstitialLoaded method. See below.
CustomEventInterstitial::showInterstitialmethod in adapter as shown below, it will be called by Mopub SDK when required, also call
CustomEventInterstitial::onInterstitialShownafter showing Pubmatic’s interstitial Ad.
Similarly, you must notify Mopub when PubMatic's Ad fails to load by calling
ActivityListenerand register it to get the lifecycle callbacks from PubMatic. Notify Mopub of the user interaction events via the corresponding callback methods.
On the MoPub web interface, create a network with the "Custom Native Network" type. Place the fully classified class name of your custom adapter (for example,
PubMaticInterstitialAdapter), in the "Custom Class" column.
Once you've completed these steps, the MoPub SDK will be able to automatically instantiate your CustomEventInterstitial subclass when your application is running. You do not need to instantiate any of these sub classes in your application code.