Unfilled Recovery for inApp [Beta]

Last edited: 2024/01/18


<aside> <img src="/icons/new-alert_red.svg" alt="/icons/new-alert_red.svg" width="40px" /> Attention: This is a beta version of the tool. It is available only for Android using Kotlin.

</aside>

Functions:

Methods:

Script explanation and implementation method:

  1. In order to implement UR in app, a listener function needs to be applied - AdListener(). The listener function is used to monitor the response from the ad server. If the response is empty, the UR is activated.

    adView.adListener = object: AdListener() {
    override fun onAdFailedToLoad(adError : LoadAdError) {
    removeAdFromLayout(adView)
    adView.destroy()
    createSecondAd()
    }
    }
    adView.loadAd(adRequest)
    
  2. When the response from the ad server is detected empty, Unfilled Recovery will create an ad slot that will be used for the second monetization attempt. This is handled by a method onAdFailedToLoad(), which will replace the unfilled ad with a new ad slot that will call a second ad request.

    private fun createSecondAd() {
    val adView = AdManagerAdView(this)
    adView.setAdSize(AdSize.BANNER)
    adView.adUnitId = "AD_UNIT_ID_2"
    addAdToLayout(adView)
    val adRequest: AdManagerAdRequest = AdManagerAdRequest.Builder()
    .addCustomTargeting("yb_revive", "true")
    .build()
    adView.loadAd(adRequest)
    }
    
  3. Simultaneously, method removeAdFromLayout will delete the original ad slot from the ad layout. This method will have to be handled on the publisher’s side.

  4. Later, createSecondAd creates a new object in AdManagerAdView, and a new AdManagerAdRequest is created with a new key value yb_revive.

  5. Finally, the method addAdToLayout will add the new ad slot to the AdManagerAdView, meaning to the publisher’s app.

Example of implementation

private fun createFirstAd() {
val adView = AdManagerAdView(this)
adView.setAdSize(AdSize.BANNER)
adView.adUnitId = "AD_UNIT_ID_1"
addAdToLayout(adView)
val adRequest = AdManagerAdRequest.Builder().build()
adView.adListener = object: AdListener() {
override fun onAdFailedToLoad(adError : LoadAdError) {
removeAdFromLayout(adView)
adView.destroy()
createSecondAd()
}
}
adView.loadAd(adRequest)
}
private fun createSecondAd() {
val adView = AdManagerAdView(this)
adView.setAdSize(AdSize.BANNER)
adView.adUnitId = "AD_UNIT_ID_2"
addAdToLayout(adView)
val adRequest: AdManagerAdRequest = AdManagerAdRequest.Builder()
.addCustomTargeting("yb_revive", "true")
.build()
adView.loadAd(adRequest)
}
private fun addAdToLayout(adView: AdManagerAdView) {
adView.id = View.generateViewId()
val constraintLayout: ConstraintLayout = findViewById(R.id.main_layout)
constraintLayout.addView(adView)
val set = ConstraintSet()
set.clone(constraintLayout)
set.connect(adView.id, ConstraintSet.RIGHT, ConstraintSet.PARENT_ID,
ConstraintSet.RIGHT)
set.connect(adView.id, ConstraintSet.LEFT, ConstraintSet.PARENT_ID,
ConstraintSet.LEFT)
set.applyTo(constraintLayout)
}
private fun removeAdFromLayout(adView: AdManagerAdView) {
val constraintLayout: ConstraintLayout = findViewById(R.id.main_layout)
constraintLayout.removeViewInLayout(adView)
}

On this page: