Faruk Arnaut
Faruk Arnaut

Reputation: 59

How to request consent for personalized ads while using admob with various ad networks through mediation?

I want to use mediation in my Android application by using AdMob. So I connected with different ad networks(Vungle, AdColony, MoPub, etc..) and integrated them inside "Mediation" for my AdMob account. I noticed that I have to comply with GDPR and that I have to request consent from my users to show personalized ads. I found out about the Consent SDK from Google which can do a lot of functionality. One of the functionality is the dialog that is shown (https://developers.google.com/admob/images/android_eu_consent_form.png). But I read that I can't use this dialog if I am using mediation. So how do make this work while using mediation?

Thanks!!

Upvotes: 1

Views: 1730

Answers (1)

Faruk Arnaut
Faruk Arnaut

Reputation: 59

I figured this out by using the combination of Consent SDK and my own code. Every time the user enters the app(don't know if I should do this, this often), I would initialize Consent SDK.

private fun getConsentStatus() {
    val consentInformation = ConsentInformation.getInstance(requireContext())
    consentInformation.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
    consentInformation.debugGeography = DebugGeography.DEBUG_GEOGRAPHY_EEA
    val ids = arrayOf("pub-77777777777")

    consentInformation.requestConsentInfoUpdate(ids, object : ConsentInfoUpdateListener {
        override fun onConsentInfoUpdated(consentStatus: ConsentStatus) {
            // User's consent status successfully updated.
            if (consentInformation.isRequestLocationInEeaOrUnknown) {
                when (consentStatus) {
                    ConsentStatus.UNKNOWN -> displayConsentForm()
                    ConsentStatus.PERSONALIZED -> proceed(true, true)
                    ConsentStatus.NON_PERSONALIZED -> proceed(false, true)
                }
            } else {
                // Not in EU, displaying personalized ads
                proceed(true, false)
            }
        }

        override fun onFailedToUpdateConsentInfo(errorDescription: String) {
            // User's consent status failed to update.
            AdvertisingInitialization(requireContext(), prefs, false)
            proceedWithoutUpdate()
        }
    })
}

private fun proceedWithoutUpdate(isInEu: Boolean = false){
    AdvertisingInitialization(requireContext(), prefs, isInEu)
    (activity as LandingActivity).navigateToContentActivity()
}

/**
 * Save the consent status in prefs and initialize ads. Then navigate to the other activity
 */
private fun proceed(isPersonalized: Boolean, isInEu: Boolean = false) {
    val consentInfo = ConsentInformation.getInstance(requireContext())
    consentInfo.consentStatus = if (isPersonalized) ConsentStatus.PERSONALIZED else ConsentStatus.NON_PERSONALIZED
    prefs.setCanShowPersonalizedAds(isPersonalized)
    AdvertisingInitialization(requireContext(), prefs, isInEu)
    (activity as LandingActivity).navigateToContentActivity()
}

If the user is in EU and never chose if he wants personalized or non-personalized ads. I would forward him to another fragment, which has: 1 TextView(description with spannable string directed to web page of ad networks), 2 Buttons with one being "Yes I agree" and the other "No, thank you"

Then I would save their pick

consentInfo.consentStatus = if (isPersonalized) ConsentStatus.PERSONALIZED else ConsentStatus.NON_PERSONALIZED
prefs.setCanShowPersonalizedAds(isPersonalized)

And every time that I would request an ad, I would do it like this:

fun getAdRequest(isPersonalized: Boolean): AdRequest {
        val adRequest: AdRequest
        adRequest = if (isPersonalized) {
            AdRequest.Builder().build()
        } else {
            val extras = Bundle()
            extras.putString("npa", "1")
            AdRequest.Builder()
                .addNetworkExtrasBundle(AdMobAdapter::class.java, extras)
                .build()
        }
        return adRequest
    }

You may have noticed a class AdvertisingInitialization. I use this class to forward the pick of user to ad networks with which I use mediation. One part of the code looks like this:

init {
    initAppLovin()
    initAdColony()
    initChartboost()
    initIronSource()
    initMoPub()
    initUnityAds()
    initVungle()
    initInMobi()
    MobileAds.initialize(context)
}

Info about every implementation is provided here: https://developers.google.com/admob/android/eu-consent

Upvotes: 0

Related Questions