Unfilled Recovery for inApp
Last edited: 2024/01/18
Note: This is a beta version of the tool. It is available only for Android using Kotlin.
Functions:
AdListener
- used for monitoring the response from the ad server
Methods:
onAdFailedToLoad()
- replacing the unfilled ad with the new ad slotremoveAdFromLayout()
- removing the unfilled ad from the layoutcreateSecondAd()
- creating new ad slotaddAdToLayout()
- adding the new ad slot to the layoutadView.setAdSize(AdSize.BANNER)
- this is the method that is used to define the ad slot you wish to replace
Script explanation and implementation method:
- 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)
Example of how AdListener
should be implemented
- 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)
}
Example of how createSecondAd
should be implemented
- 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. - Later,
createSecondAd
creates a new object inAdManagerAdView
, and a newAdManagerAdRequest
is created with a new parameter yb_revive, which is a key-value Yieldbird implements on our side - Finally, the method
addAdToLayout
will add the new ad slot to theAdManagerAdView
, 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)
}