Custom Events
Pages 17
- Home
- Banner Integration
- Conversion Tracking
- Custom Events
- Data Passing
- Getting Started
- Integrating Native Third Party Ad Networks
- Integrating Third Party Ad Networks
- Integrating with Google Play Services
- Interstitial Integration
- Manual Integration of Native Ads
- Native Ads Integration
- Native Ads with Recycler View
- Native Custom Event Development
- Native Video Integration
- Proguard
- Rewarded Video Integration
- Show 2 more pages…
Integrating the MoPub SDK
- Integrating Banner Ads
- Integrating Interstitial Ads
- Integrating Rewarded Video Ads
- Integrating Native Ads
- Integrating Native Video Ads
- Native Ads with Recycler View
- Manual Integration of Native Ads
- Conversion Tracking
Integrating with Third Party Networks
- Integrating Banner and Interstitial Third Party Ad Networks
- Integrating Native Third Party Ad Networks
- Integrating with Google Play Services
- Writing Custom Events
- Writing Native Custom Events
Other
Clone this wiki locally
Custom events allow you to support native ad networks not bundled with the MoPub SDK, or to execute any of your application code from the MoPub web interface.
Quick Start for Banners
Create a subclass of
CustomEventBannerin thecom.mopub.mobileadspackage of your application.Override the
loadBanner(Context, CustomEventBannerListener, Map<String, Object>, Map<String, String>)method and implement code that requests an ad.When your ad loads successfully, you must notify MoPub of the event and display your ad by calling the
onBannerLoaded(View)method on theCustomEventBannerListenerobject (passed inloadAd).Similarly, you must notify the
CustomEventBannerListenerobject when your ad fails to load by calling theonBannerFailed(MoPubErrorCode)method on the listener object. You should pass a suitable MoPubErrorCode constant (seeMoPubErrorCode.java).-
(Optional) Notify the listener object of other ad lifecycle events via the corresponding methods on
CustomEventBannerListener.public void onBannerClicked();public void onLeaveApplication(); (Optional) Override the
onInvalidate()method ofCustomEventBannerif your custom event requires any sort of cleanup.-
Finally, on the MoPub web interface, create a network with the "Custom Native Network" type. Place the fully-qualified class name of your custom event (e.g.
com.mopub.mobileads.YourCustomEventBanner) in the "Custom Class" column.Once you've completed these steps, the MoPub SDK will be able to cause your
CustomEventBannersubclass to be instantiated at the proper time while your application is running. You do not need to instantiate any of these subclasses in your application code. Note: the MoPub SDK will cause a newCustomEventBannerobject to be instantiated on every ad call, so you can safely make changes to the custom event object's internal state between calls.
Quick Start for Interstitials
Create a subclass of
CustomEventInterstitialin thecom.mopub.mobileadspackage of your application.Override the
loadInterstitial(Context, CustomEventInterstitialListener, Map<String, Object>, Map<String, String>)method and implement code that requests an interstitial.Override the
showInterstitial()method and implement code that displays your interstitial.When your ad loads successfully, you must notify MoPub of the event and display your ad by calling the
onInterstitialLoaded()method on theCustomEventInterstitialListenerobject (passed inloadAd).Similarly, you must notify the
CustomEventInterstitialListenerobject when your ad fails to load by calling theonInterstitialFailed(MoPubErrorCode)method on the listener object.-
(Optional) Notify the listener object of other ad lifecycle events via the corresponding methods on
CustomEventInterstitialListener.public void onInterstitialShown();public void onInterstitialDismissed();public void onInterstitialClicked();public void onLeaveApplication(); (Optional) Override the
onInvalidate()method ofCustomEventInterstitialif your custom event requires any sort of cleanup.-
Finally, on the MoPub web interface, create a network with the "Custom Native Network" type. Place the fully-qualified class name of your custom event (e.g.
com.mopub.mobileads.YourCustomEventInterstitial) in the "Custom Class" column.Once you've completed these steps, the MoPub SDK will be able to cause your
CustomEventInterstitialsubclass to be instantiated at the proper time while your application is running. You do not need to instantiate any of these subclasses in your application code. Note: the MoPub SDK will cause a newCustomEventInterstitialobject to be instantiated on every ad call, so you can safely make changes to the custom event object's internal state between calls.
Quick Start for Native Ads
Note Advanced instructions available: Native Custom Event Development
Create a subclass of
com.mopub.nativeads.StaticNativeAdin thecom.mopub.nativeadspackage. This class will implement a MoPub Native Ad using a native ad from another network.-
After loading the native ad from the network, initialize your object using the following setter methods:
-
setTitlefor a title string -
setTextfor a text string -
setIconImageUrlfor an icon-size image -
setMainImageUrlfor a larger image -
setCallToActionfor a call to action string -
setStarRatingfor a star rating between 0 and 5 (optional) -
setClickDestinationUrlfor a click destination URL -
addExtrafor any additional fields. A string key is required to store and retrieve them. -
addImpressionTrackerfor any additional impression tracking urls you want fired -
setImpressionMinTimeViewedfor the minimum amount of time the view is required to be on the screen before recording an impression. This value should be set according to how the network records their impression. The default value is 500ms.
-
-
Implement lifecycle methods for handling events with your native ad:
-
prepareis called before an ad is displayed in the app. You should set up impression tracking in this method. Seecom.mopub.nativeads.MoPubCustomEventNative.MoPubStaticNativeAd#preparefor an example of setting up impression tracking using thecom.mopub.nativeads.ImpressionTrackerclass. -
handleClickshould be implemented if the network requires you to explicitly handle click events of views rendered to screen. -
clearis called when an ad is no longer displayed to a user. After this, theViewused for the ad may be deleted or recycled. You should implement this to clear an viewability tracking code you are using. If using anImpressionTrackerinstance, you should remove the view here. -
destroyis called when the ad will never be displayed again. You should implement it if the network requires you to destroy or cleanup their native ad when you are finished with it.
-
Impression Tracking Details: All Native Custom Events must implement their own impression tracking and call
#notifyAdImpressedwhen the impression is recorded. MoPub provides utility classes to provide automatic impression tracking.StaticNativeAdimplementsImpressionInterface, so you can instantiate anImpressionTrackerin your object's construction. You should add your ad'sViewto anImpressionTrackerinstance inprepareand remove it from theImpressionTrackerinclear. In destroy you should callImpressionTracker#destroy. Seecom.mopub.nativeads.MoPubCustomEventNative.MoPubStaticNativeAdfor an example following this pattern.Click Tracking Details: All Native Custom Events are responsible for firing click trackers when sending the user to the destination URL. You can easily do this by calling
#notifyAdClickedimmediately before opening the destination URL. You can also delegate Click URL handling to the MoPub SDK. ImplementClickInterfacein your BaseNativeAd subclass and create an instance ofcom.mopub.nativeads.NativeClickHandlerin your class construction. Inprepare, add your ad'sViewto theNativeClickHandlerand remove it from the click handler inclear. Seecom.mopub.nativeads.MoPubCustomEventNative.MoPubStaticNativeAdfor an example following this pattern.-
If the network exposes impression or click callbacks, have them call the following methods to notify the SDK to record an impression or click. This is an alternative to using the MoPub
ImpressionTracker,ImpressionInterface,NativeClickHandlerandClickInterfaceclasses.-
notifyAdImpressedshould be called from the network's impression callback to notify the SDK to record an impression. -
notifyAdClickedshould be called from the network's click callback to notify the SDK to record a click event.
-
Create a subclass of
CustomEventNativein thecom.mopub.nativeadspackage. This class should override theloadNativeAdmethod and requests a native ad from the network. TheserverExtrasmap will contain any values that were entered into the "Custom Event Class Data" field when youcreated your Custom Native Network in the MoPub web UI, such as the placement id.Once the ad is loaded, you need to create an instance of the
StaticNativeAdorBaseNativeAdclass as described above. If the ad fails to load, you should callcustomEventNativeListener.onNativeAdFailedwith aNativeErrorCode.Before calling
customEventNativeListener.onAdLoaded, call the superclass'spreCacheImagesmethod to ensure that images are displayed immediately when your native ad renders. If images fail to download, you should callcustomEventNativeListener.onNativeAdFailed.
Passing Extra Data To Custom Events
The loadAd() / loadInterstitial() method of your custom event subclass exposes two Map objects that you may use to provide your custom event with any additional data it needs: Map<String, Object> localExtras and Map<String, String> serverExtras.
The localExtras Map can be set anywhere in your application code by calling MoPubView.setLocalExtras(Map<String, Object>) or MoPubInterstitial.setLocalExtras(Map<String, Object>).
The serverExtras Map is populated with data entered in the MoPub web interface. Specifically, after navigating to the Custom Native Network edit page, you can select the "Custom Class" column and enter a JSON object with String keys and values in the Data field (e.g. {"id": "12345", "foo": "bar"}). This is particularly useful for passing down third-party Network IDs without making changes to your application code.
Existing Custom Event Classes
We have implemented support for several third-party networks using the Custom Event framework. They can be found in the extras/src/com/mopub/mobileads directory and can be used as a starting point for your own Custom Event implementations.