MoPub: Native Ads Integration

Native Ads

The integration of native ads into MoPub monetization is based on MoPub’s Mediation feature (using PrebidNativeAdapter). Here are the basic steps of integration:

fun initAd() {
    // Initialize AdapterHelper and MoPubNative.
    adapterHelper = AdapterHelper()
    mopubNative = MoPubNative(requireContext(), adUnitId, nativeNetworkListener)
    // Create viewBinder.
    val viewBinder = ViewBinder.Builder(R.layout.lyt_native_ad)
            // ...
    // Register ad renderers.
    // Initialize MoPubNativeAdUnit and provide necessary configuration.
    mopubNativeAdUnit = MoPubNativeAdUnit(requireContext(), configId, getNativeAdConfig())
    // Execute ad load.

fun loadAd() {
    // Initialize MoPub SDK and make ad request. 
    MoPub.initializeSdk(requireContext(), SdkConfiguration.Builder(adUnitId).build()) {
        mopubNativeAdUnit.fetchDemand(keywordsContainer, mopubNative) {
            val requestParameters = RequestParameters.Builder()
// Add view when receiving a successful NativeAd response from MoPub.
object : MoPubNative.MoPubNativeNetworkListener {
    override fun onNativeLoad(nativeAd: NativeAd?) {
        val view = adapterHelper.getAdView(null, viewContainer, nativeAd)
        // Add view to viewContainer

Step 1: Create Ad View

You have to create and place MoPub’s Ad View into the app page.

Step 2: Create Ad Unit

Create the MoPubBannerAdUnit object with parameters:

  • configId - an ID of Stored Impression on the Prebid server
  • size - the size of the ad unit which will be used in the bid request.

Step 3: Create and provide NativeAdConfiguration

NativeAdConfiguration creation example:

private fun createNativeAdConfiguration(): NativeAdConfiguration {
    val nativeAdConfiguration = NativeAdConfiguration()
    nativeAdConfiguration.contextType = NativeAdConfiguration.ContextType.SOCIAL_CENTRIC
    nativeAdConfiguration.placementType = NativeAdConfiguration.PlacementType.CONTENT_FEED
    nativeAdConfiguration.contextSubType = NativeAdConfiguration.ContextSubType.GENERAL_SOCIAL

    val methods = ArrayList<NativeEventTracker.EventTrackingMethod>()
    val eventTracker = NativeEventTracker(NativeEventTracker.EventType.IMPRESSION, methods)

    val assetTitle = NativeAssetTitle()
    assetTitle.len = 90
    assetTitle.isRequired = true

    val assetIcon = NativeAssetImage()
    assetIcon.type = NativeAssetImage.ImageType.ICON
    assetIcon.wMin = 20
    assetIcon.hMin = 20
    assetIcon.isRequired = true

    val assetImage = NativeAssetImage()
    assetImage.hMin = 20
    assetImage.wMin = 200
    assetImage.isRequired = true

    val assetData = NativeAssetData()
    assetData.len = 90
    assetData.type = NativeAssetData.DataType.SPONSORED
    assetData.isRequired = true

    val assetBody = NativeAssetData()
    assetBody.isRequired = true
    assetBody.type = NativeAssetData.DataType.DESC

    val assetCta = NativeAssetData()
    assetCta.isRequired = true
    assetCta.type = NativeAssetData.DataType.CTA_TEXT

    return nativeAdConfiguration

See more NativeAdConfiguration options here.

Step 4: Fetch Demand

To run an auction on Prebid run the fetchDemand() method which performs several actions:

  • Makes a bid request to Prebid
  • Sets up the targeting keywords to the MoPub’s ad unit
  • Passes the winning bid to the MoPub’s ad unit
  • Returns the result of bid request for future processing

Step 5: Load the Ad

When the bid request has completed, the responsibility of making the Ad Request is passed to the publisher. That is why you have to invoke loadAd() on the MoPub’s Ad View explicitly in the completion handler of fetchDemand().

Native Styles

See MoPub Integration page for more info about MoPub order setup and Adapter integration.

To display an ad you need to implement these easy steps:

private fun initBanner() {
    // 1. Create and initialize MoPubView instance
    bannerView = MoPubView(requireContext())
    bannerView?.bannerAdListener = this
    // 2. initialize MoPubBannerAdUnit
    if (bannerAdUnit == null) {
        bannerAdUnit = MoPubBannerAdUnit(requireContext(), configId, AdSize(width, height))        
    // 3. Provide NativeAdConfiguration
    val nativeAdConfiguration = createNativeAdConfiguration()

    // Add moPubView to your viewContainer

    val builder = SdkConfiguration.Builder(moPubAdUnit)
    MoPub.initializeSdk(requireContext(), {
        // 4. Run an Header Bidding auction on Prebid and provide MoPubView as parameter. It is important to execute this method after MoPub SDK initialization.
        bannerAdUnit?.fetchDemand(bannerView!!) {
            // 5. execute MoPubView `loadAd` when receiving a valid demand result