Native Ads

This guide shows how to integrate native ads of RTBSDK into your app.

Create RTBNativeAdLoader

First, create an instance of RTBNativeAdLoader , passing context instance and implementation of RTBNativeAdLoadDelegate

val loader = RTBNativeAdLoader(context, loadListener)

Request Ad

To load an ad, you will need to pass an instance of RTBNativeAdRequestConfiguration with placementID and the bundleId. Please contact our support to get the needed IDs.

val request = RTBNativeAdRequestConfiguration(placementId, bundleId)
request.sellerId = "<SELLERID>" // Optional
loader.load(request)

Retrieve Native Ad Assets

Once the ad is loaded and the method nativeAdDidReceiveAd is called, you can get the needed native ad assets and assemble your view.

override fun nativeAdDidReceiveAd(nativeAd: RTBNativeAd, bidInfo: RTBBidInfo, networkName: String) {
    // Prepare your layout for native ad
    val adView: View = findViewById(R.id.native_ad)
    val iconView = adView.findViewById<ImageView>(R.id.iconImage)
    val mainImageView = adView.findViewById<ImageView>(R.id.mainImage)
    val titleView = adView.findViewById<TextView>(R.id.title)
    val descriptionView = adView.findViewById<TextView>(R.id.description)
    val callToActionView = adView.findViewById<TextView>(R.id.cta)

    // Fill the views with native ad assets
    nativeAd.iconUrl?.let {
        loadBitmapForView(it, iconView)
    }
    nativeAd.imageUrl?.let {
        loadBitmapForView(it, mainImageView)
    }
    titleView.text = nativeAd.title
    descriptionView.text = nativeAd.body
    callToActionView.text = nativeAd.callToAction
}

(Optional) Use InteractionDelegate

If you are interested in native ad interaction events like clicks, you can pass optional RTBNativeAdInteractionDelegate to native ad instance

nativeAd.listener = yourListener

Track Native Ad

To make sure impression reporting and click handling is handled, call trackImpression method on native ad instance, passing the view used for rendering it.

nativeAd.trackImpression(adView)

Complete Code Example

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Prepare native ad loader
        val loader = RTBNativeAdLoader(this, createLoadListener())

        // Prepare request configurations
        val request = RTBNativeAdRequestConfiguration(placementId, bundleId)
        request.sellerId = "<SELLERID>" // Optional
        
        // Request an ad
        loader.load(request)
    }
    
    private fun createLoadListener(): RTBNativeAdLoadDelegate {
        return object : RTBNativeAdLoadDelegate {
            override fun nativeAdDidReceiveAd(nativeAd: RTBNativeAd, bidInfo: RTBBidInfo, networkName: String) {
                // Ad had been loaded
                // Optionally - examine bid details:
                val priceCPM = bidInfo.priceCPM
                
                // Optionally - set interaction listener
                native.listener = createInteractionListener()
                
                // Render the native ad and start tracking
                renderNativeAd(nativeAd)
            }

            override fun nativeAdDidFailToReceiveAd(errorMessage: String, networkName: String) {
                // Failed to load an ad
            }
        }
    }
    
    private fun renderNativeAd(ad: RTBNativeAd) {
        // Prepare your layout for native ad
        val adView: View = findViewById(R.id.native_ad)

        val iconView = adView.findViewById<ImageView>(R.id.iconImage)
        val mainImageView = adView.findViewById<ImageView>(R.id.mainImage)
        val titleView = adView.findViewById<TextView>(R.id.title)
        val descriptionView = adView.findViewById<TextView>(R.id.description)
        val callToActionView = adView.findViewById<TextView>(R.id.cta)

        // Fill the views with native ad assets
        ad.iconUrl?.let {
            loadBitmapForView(it, iconView)
        }
        ad.imageUrl?.let {
            loadBitmapForView(it, mainImageView)
        }
        titleView.text = ad.title
        descriptionView.text = ad.body
        callToActionView.text = ad.callToAction

        // Track impression
        ad.trackImpression(adView)
    }    
    
    private fun createInteractionListener(): RTBNativeAdInteractionDelegate {
        return object : RTBNativeAdInteractionDelegate {
            override fun nativeAdDidRecordClick(nativeAd: RTBNativeAd, networkName: String) {
                // Native ad recorded click
            }

            override fun nativeAdDidPauseForAd(nativeAd: RTBNativeAd, networkName: String) {
                // The app paused after the user clicked on the ad
            }

            override fun nativeAdDidResumeAfterAd(nativeAd: RTBNativeAd, networkName: String) {
                // The app resumed after returning from ad
            }
        }
    }
    
}

Last updated