Android Tutorial FirebaseAdMob and User Consent

Status
Not open for further replies.
Use instead: AdsHelper extends FirebaseAdMob2 / Google Mobile Ads v20.0+

More information: https://developers.google.com/admob/android/eu-consent
By default, AdMob serves personalized ads.
With the new GDPR law, publishers (you) need to request consent before the private data can be used to serve personalized ads to EU users.

The main steps are:

1. Check the current consent state, usually in the starter service.
2. If the consent state is unknown and the user is from the EU (or his location is unknown) then show the consent form.

SS-2018-05-23_15.56.06.png


This step must be done from an Activity.

3. Show non-personalized or personalized ads based on the consent state and location.

The limit on 12 providers was removed. Make sure to use FirebaseAdMob v1.55+.

Note that the Google consent form will only work if there are 12 or less ad technology providers. There are 200 by default so you need to change it in your AdMob console:
Blocking Controls - EU USER CONSENT tab:


SS-2018-05-23_16.09.16.png



Testing

In order to test the behavior you can can add your device as a test device and set whether the location should be treated as coming from the EU:
B4X:
consent.AddTestDevice("D4AC977CA12D122C4563730CC762C302") 'check the unfiltered logs for the device id after calling RequestInfoUpdate
consent.SetDebugGeography(True) 'True = EU, False = Non-EU

You can set the consent state yourself and it will be saved automatically by setting the ConsentState property. This can be useful for debugging and to allow the user to change the previously selected option.

Starter service code:

B4X:
Sub Service_Create
   consent.Initialize("consent")
   consent.AddTestDevice("D4AC977CA12D122C4563730CC762C302")
   consent.SetDebugGeography(True) 'comment for regular operation
   consent.RequestInfoUpdate(Array("pub-144444444"))
   Wait For consent_InfoUpdated (Success As Boolean)
   If Success = False Then
       Log($"Error getting consent state: ${LastException}"$)
   End If
   Log($"Consent state: ${consent.ConsentState}"$)
   Log("EU: " & consent.IsRequestLocationInEeaOrUnknown)
   Do While IsPaused(Main)
       Sleep(100)
   Loop
   CallSubDelayed(Main, "ConsentStateAvailable")
End Sub

Main activity code:

B4X:
'show the consent form if needed
Sub ConsentStateAvailable
   Dim consent As ConsentManager = Starter.consent
   If consent.ConsentState = consent.STATE_UNKNOWN And consent.IsRequestLocationInEeaOrUnknown Then
       'Set last parameter to False if you don't want to show the "pay for ad-free" option.
       'Change privacy policy URL.
       consent.ShowConsentForm("https://www.mysite.com/privacy.html", True, True, True)
       Wait For Consent_FormResult (Success As Boolean, UserPrefersAdFreeOption As Boolean)
       If Success Then
           Log($"Consent form result: ${consent.ConsentState}, AdFree: ${UserPrefersAdFreeOption}"$)
       Else
           Log($"Error: ${LastException}"$)
       End If
   End If
   LoadAd
End Sub

'load ads:
Sub LoadAd
   Dim builder As AdRequestBuilder
   builder.Initialize
   Dim consent As ConsentManager = Starter.consent
   If consent.IsRequestLocationInEeaOrUnknown Then
       If consent.ConsentState = consent.STATE_NON_PERSONALIZED Then
           builder.NonPersonalizedAds
       Else if consent.ConsentState = consent.STATE_UNKNOWN Then
           Return
       End If
   End If
   builder.AddTestDevice("D4AC977CA12D122C4563730CC762C302")
   adview1.LoadAdWithBuilder(builder)
End Sub

Ad free option

If the user selects this option then the ConsentState will be kept UNKNOWN and UserPrefersAdFreeOption will be true (nothing else happens).


Dependencies: FirebaseAdmob v1.50+ (https://www.b4x.com/android/forum/threads/updates-to-internal-libraries.59340/#post-590564)
 
Last edited:

kisoft

Well-Known Member
Licensed User
I live in Poland, I have not implemented it yet. Nothing seems to be happening. The other ads are still displayed.
 

kisoft

Well-Known Member
Licensed User
I just installed 20 different applications on my phone. None of them asked for permission to display personalized ads. Other ads are displayed normally.
 

abhishek007p

Active Member
Licensed User
1. minimum supported B4A version, play service version needed for this?
2. does it also work with 'Lite' version of FireBase AdMob??, since most of my apps are using lite version.
3. what will happen to older apps not yet updated? ads will continue to displayed or ads will stop showing?
4. i only use AdMob without mediation, can i use the default google consent dialogs.
 
Last edited:

Erel

Administrator
Staff member
Licensed User
1. Use B4A Sdk Manager and make sure that everything is updated. B4A v7.8+ is supported (might work with older versions as well).
2. It works with FirebaseAdMob. It was not tested with any other implementation.
3. Better consult with Google about this. As I understand it everything will work but you will not meet the new law requirements.
4. Yes, but you need to first update the list of ad technology providers as explained in the tutorial.
 

Erel

Administrator
Staff member
Licensed User
Updated the LoadAd code to not load anything if the user is in EU and the consent state is unknown.

This is more correct based on Google documentation:
"Ads served by Google can be categorized as personalized or non-personalized, both requiring consent from users in the EEA."

B4X:
Sub LoadAd
   Dim builder As AdRequestBuilder
   builder.Initialize
   Dim consent As ConsentManager = Starter.consent
   If consent.IsRequestLocationInEeaOrUnknown Then
       If consent.ConsentState = consent.STATE_NON_PERSONALIZED Then
           builder.NonPersonalizedAds
       Else if consent.ConsentState = consent.STATE_UNKNOWN Then
           Return
       End If
   End If
   builder.AddTestDevice("D4AC977CA12D122C4563730CC762C302")
   adview1.LoadAdWithBuilder(builder)
End Sub
 

R D Boozer

Member
Licensed User
Is there away to make an app be unavailable in the app store in the EU whilst still selling it elsewhere? The app I'm about to release would primarily be of interest to North Americans anyway.
 

npsonic

Active Member
Licensed User
Is there away to make an app be unavailable in the app store in the EU whilst still selling it elsewhere? The app I'm about to release would primarily be of interest to North Americans anyway.
You select countries when you are publishing your app. Just select United States leave everything else empty.
 

R D Boozer

Member
Licensed User
You select countries when you are publishing your app. Just select United States leave everything else empty.
Thanks! Obviously, this will be my first Android app, though I started my software engineer career about 40 years ago. Again, your quick and candid response is much appreciated!
 

sorex

Expert
Licensed User
thanks for supporting us with this, Erel.

I can see the 'pop-up' so I guess it works fine. (still need to test both modes)

What I can see is that an anywhere software icon also appears on my own test app.

Can this be replaced with our own app icon?
 

sorex

Expert
Licensed User
you're right, my bad.

I copied a B4A file to a new folder to work from and I didn't copy the /objects/res/drawable/icon.png image so it placed what I saw in your screenshot.
 

chris ash

Member
Licensed User
I am going to ask what is probably a really daft question...

But I'm not sure so I will ask anyway... How does this apply to interstitial and rewarded ads??

And how would you set them to fire or not with the same restraints as above? (A sample code snippet would be useful)

Thanks in advance
CAsh
 

sorex

Expert
Licensed User
you can see a modded loadAd sub in the first post.

just change the ad load to interstitial like this

B4X:
adInterstitial.LoadAdWithBuilder(builder)
 

sorex

Expert
Licensed User

tufanv

Expert
Licensed User
if we are also using interstitial ads , we need to add
iad.LoadAdWithBuilder(builder) under
adview1.LoadAdWithBuilder(builder)

right ?
 

Filippo

Expert
Licensed User
B4X:
Sub ConsentStateAvailable
   Dim consent As ConsentManager = Starter.consent
   If consent.ConsentState = consent.STATE_UNKNOWN And consent.IsRequestLocationInEeaOrUnknown Then
       'Set last parameter to False if you don't want to show the "pay for ad-free" option.
       'Change privacy policy URL.
       consent.ShowConsentForm("https://www.mysite.com/privacy.html", True, True, True)
       Wait For Consent_FormResult (Success As Boolean, UserPrefersAdFreeOption As Boolean)
       If Success Then
           Log($"Consent form result: ${consent.ConsentState}, AdFree: ${UserPrefersAdFreeOption}"$)
       Else
           Log($"Error: ${LastException}"$)
       End If
   End If
   LoadAd
End Sub

At the App-start I get this message:
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Consent state: UNKNOWN
EU: true
Error: java.lang.Exception: Error: consent form can be used with custom provider selection only.
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
This means that if you do not set the Admob settings to "Custom set of ad technology providers", this dialog will not be displayed and no further step is necessary?

Or did I not understand something again? :(
 
Status
Not open for further replies.
Top