Mars SDK - Native Ad Unit

Prepare the Native Ad Request

The ad request for the Native ad assumes the list of assets that are part of the native ad layout. Only publishers know the exact ad layout and its components. Hence, the app developer should provide a description of the assets by creating the respective objects.

private fun createNativeConfig(): MediafyNativeAdConfig {
    val ctaText = NativeDataAsset(NativeDataAsset.DataType.CTATEXT)
    ctaText.len = 15

    val title = NativeTitleAsset(90)
    title.isRequired = true

    val icon = NativeImageAsset()
    icon.wMin = 50
    icon.hMin = 50
    icon.imageType = NativeImageAsset.ImageType.ICON

    val image = NativeImageAsset()
    image.w = 1200
    image.h = 627
    image.imageType = NativeImageAsset.ImageType.MAIN
    image.isRequired = true

    val rating = NativeDataAsset(NativeDataAsset.DataType.RATING)

    val description = NativeDataAsset(NativeDataAsset.DataType.DESC)
    description.isRequired = true
    description.len = 150

    val assets = listOf(ctaText, title, icon, image, rating, description)

    val methods: ArrayList<EventTrackingMethod> = ArrayList()
    methods.add(EventTrackingMethod.IMAGE)
    methods.add(EventTrackingMethod.JS)
    val tracker = NativeEventTracker(NativeEventTracker.EventType.IMPRESSION, methods)


    val config = MediafyNativeAdConfig()
    config.setContextType(NativeContextType.SOCIAL_CENTRIC)
    config.setPlacementType(NativePlacementType.CONTENT_FEED)
    config.setContextSubType(NativeContextSubtype.GENERAL_SOCIAL)
    config.setNativeEventTrackers(listOf(tracker))
    config.setNativeAssets(assets)

    return config
}

Add Native Ad Unit

To load a native ad, you should initialize and configure MediafyNativeAdUnit object and call the loadAd() method.

private fun createAd() {
    // 1. Create MediafyNativeAdUnit
    adUnit = MediafyNativeAdUnit()
    
    // 2. Configure ad unit with native config
    adUnit?.setAdUnitId(AD_UNIT_ID)
    adUnit?.setNativeAdConfig(createNativeConfig())
    
    // 3. Load ad
    adUnit?.loadAd { result: MediafyNativeResult ->
        val nativeAd = result.nativeAd
        if (nativeAd == null) {
            Log.e("AdExample", "Native ad is null: " + result.status)
            return@loadAd
        }
        Log.d(TAG, "Native ad loaded successfully")
        
        // 4. Create native view
        createNativeView(nativeAd)
    }
}

Apply the received assets to the ad layout

Once the ad is loaded, the SDK provides you with a MediafyNativeAd object in the callback of the loadAd() method. This object contains ad assets you should apply to the native ad layout in your app.

private fun createNativeView(ad: MediafyNativeAd) {
    val nativeContainer = View.inflate(this, R.layout.layout_native, null)
   
    val icon = nativeContainer.findViewById<ImageView>(R.id.imgIcon)
    ImageUtils.download(ad.iconUrl, icon)
    
    val title = nativeContainer.findViewById<TextView>(R.id.tvTitle)
    title.text = ad.title
    
    val image = nativeContainer.findViewById<ImageView>(R.id.imgImage)
    ImageUtils.download(ad.imageUrl, image)
    val description = nativeContainer.findViewById<TextView>(R.id.tvDesc)
    description.text = ad.description
    
    val cta = nativeContainer.findViewById<Button>(R.id.btnCta)
    cta.text = ad.callToAction
    
    containerForAd.addView(nativeContainer)
    
    ad.registerView(nativeContainer, Lists.newArrayList(icon, title, image, description, cta), createListener())
}

It's important to call the registerView() method. In this case, the SDK will track the impression event properly.

Manage the Ad States

If you need to manage stages of the ad lifecycle you should implement the MediafyNativeAdUnitEventListener interface.

private fun createListener(): MediafyNativeAdUnitEventListener {
    return object : MediafyNativeAdUnitEventListener {
        override fun onAdImpression() {
            // Called when ad displayed
            Log.d(TAG, "Ad displayed on the screen")
        }
        override fun onAdClicked() {
            // Called when ad clicked
            Log.d(TAG, "Ad clicked")
        }
        override fun onAdExpired() {
            // Called when ad expired
            Log.d(TAG, "Ad expired")
        }
    }
}

Contact us today

to enjoy endless opportunities!

Contact Us
+

Contact Us