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:

Computersmith64

Well-Known Member
Licensed User
Longtime User
So how are we supposed to know which ad technology providers to choose? I can't find anything in my AdMob console that tells me which ones I am currently using - so if I just take a stab at it, I will most likely end up impacting my EU revenue (actually, if I am currently using say 100 & I have to choose only 12, then I guess that will happen anyway).

I think that no matter what we do, there will be a big hit on EU ad revenues - lucky for me it's "only" about 10% of my total revenue...

- Colin.
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
OK - on further investigation, I have found that if you go into the Ad Review Center in your AdMob console you can filter the list of ads by Ad Network. I guess you could use this to see how many ads are being shown from each ad technology provider, but there are 2 issues with this:

1) You can only apply 1 Ad Network filter at a time - so you'll have to go through them 1 by 1; and
2) You can't also filter by country/region - which means you still don't know if those ads are actually being shown in EU.

- Colin.
 

kisoft

Well-Known Member
Licensed User
Longtime User
HI
I understand that if the user does not agree to the display of advertisements, the ad will not be displayed. How to block an application in the absence of such consent.
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
HI
I understand that if the user does not agree to the display of advertisements, the ad will not be displayed. How to block an application in the absence of such consent.
My understanding is that every time the app starts you check the current consent state & if there is no consent, you present them with the dialog with 3 options:

1) Consent to personalized ads; or
2) Consent to non-personalized ads; or
3) Pay for an ad-free version (which can either be sending them to Play Store to download a paid version, or via an in-app purchase).

If they don't pick one of those options, then you have 3 choices:

1) Let them use the app with no ads; or
2) Let them use the app & where you would have shown an ad, instead show a "pay for the app you cheap bastard" message; or
3) Exit the app.

I think I will probably implement choice 3 - so if they don't consent to any form of ads & don't want to pay for the app, then they won't be able to use it.

- Colin.
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
I've been trying to test this today, however even when I set consent.SetDebugGeography(True), it still returns EU: false in the logs. The device id I'm using is the one that comes up when you first connect to a device:

Logger connected to: 360ceeb6

Or is this not the correct one? I can't find another device id in the unfiltered logs, so I'm hoping this is the correct one!

- Colin.
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
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
consent.SetDebugGeography(True) 'True = EU, False = Non-EU

Shouldn't this be the advertising id? According to the Google doco:
Whitelist your device to be a debug device using the advertising ID from logcat:

ConsentInformation.getInstance(context).addTestDevice("33BE2250B43518CCDA7DE426D04EE231");

Even so, I couldn't find the advertising id in the unfiltered logs of my test device, but I was able to find it in the Google Settings app on the device. Unfortunately, even using that id, I still can't get consent.SetDebugGeography(True) to work.

- Colin.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Shouldn't this be the advertising id?
No. It is a device id. I see it here in the unfiltered logs. It is the same id that you use to request test ads.

I understand that if the user does not agree to the display of advertisements, the ad will not be displayed. How to block an application in the absence of such consent.
The user is not asked to agree for ads, only to agree to personalized ads. See the code in LoadAd. If the user is from the EU and hasn't gave consent to personalization then you show him non-personalized ads.
 
Last edited:

Computersmith64

Well-Known Member
Licensed User
Longtime User
No. It is a device id. I see it here in the unfiltered logs. It is the same id that you use to request test ads.
Hmmm - I don't use a device id to get test ads. I just use the test ad unit ids - https://developers.google.com/admob/android/test-ads. I tried using the device id from the log (& using the GetDeviceId per Android Device Unique ID - Alternative to PhoneId) however it wouldn't set the location as EU.

The user is not asked to agree for ads, only to agree to personalized ads. See the code in LoadAd. If the user if from the EU and hasn't gave consent to personalization then you show him non-personalized ads.
From the fairly large amount of reading I've done on this over the past couple of weeks, I'm pretty sure that you still have to get consent for non-personalized ads too. This is from the AdMob EU User Consent Policy (https://support.google.com/admob/answer/7666366)

A Non-Personalized Ads solution (DFP Help Center, AdSense Help Center) allows you to present EEA users with a choice between personalized ads and non-personalized ads (or to choose to serve only non-personalized ads to all users in the EEA). Non-Personalized Ads only use contextual information, including coarse general (city-level) location.
  • Although these ads don’t use cookies for ads personalization, they do use cookies to allow for frequency capping, aggregated ad reporting, and to combat fraud and abuse. Consent is therefore required to use cookies for those purposes from users in countries to which the ePrivacy Directive’s cookie provisions apply.

I guess this is why their dialog has options for the user to give consent for personalized or non personalized ads. By tapping on the non personalized option, they are giving consent.

- Colin.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
This has nothing to do with the device id you need. It works here with the device id from the logs.

I guess this is why their dialog has options for the user to give consent for personalized or non personalized ads. By tapping on the non personalized option, they are giving consent.
You should read the terms and follow them as you or your lawyer understand them.
I'm not a lawyer. As I understand it, the quote you posted is not relevant for mobile apps as you are not storing cookies. It is relevant for web sites and it means that web sites should show the "accept cookies" message.
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
This has nothing to do with the device id you need. It works here with the device id from the logs.

The device id in the logs is the same as the device id returned by that function - & it doesn't work. I have copied the entire unfiltered log to Notepad++ & searched for a device id & found nothing.

- Colin.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
When you call consent.RequestInfoUpdate it must print in the unfiltered logs:

Use ConsentInformation.getInstance(context).addTestDevice("D4AC977CA12D122C4563730CC762C302") to get test ads on this device.

Or:

This request is sent from a test device.

If you don't see it then maybe you are unable to see the unfiltered logs. Make sure to use B4A v8+ with the latest version of B4A-Bridge. If not working then switch to USB debug mode.
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
When you call consent.RequestInfoUpdate it must print in the unfiltered logs:

Use ConsentInformation.getInstance(context).addTestDevice("D4AC977CA12D122C4563730CC762C302") to get test ads on this device.

Or:

This request is sent from a test device.

If you don't see it then maybe you are unable to see the unfiltered logs. Make sure to use B4A v8+ with the latest version of B4A-Bridge. If not working then switch to USB debug mode.

Thank you for providing that crucial piece of information Erel. I can sense your frustration with me seemingly not understanding where this device id was coming from, however I am equally frustrated because I've spent pretty much all day trying to make it work & the final piece of the puzzle ("When you call consent.RequestInfoUpdate it must print in the unfiltered logs:") is nowhere to be found in the tutorial. :)

Yes, the comment about looking in the unfiltered logs (which I was doing) is there - however it doesn't say that the device id will be printed after you call consent.RequestInfoUpdate. All it says is "'check the unfiltered logs for the device id". Perhaps I'm just dumber than the average developer... :)

- Colin.
 

kisoft

Well-Known Member
Licensed User
Longtime User
I've pasted the code but I can not compile. I'm getting a syntax error: unknown user ConsentState.
I've updated the FirebaseAdmob v1.50 library. I have b4a V8.0
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
I've pasted the code but I can not compile. I'm getting a syntax error: unknown user ConsentState.
I've updated the FirebaseAdmob v1.50 library. I have b4a V8.0
Make sure you either copied the v1.50 of the FirebaseAdmob library to the Libraries directory, or if you put it in the Additional Libraries directory, you deleted the older one our of the Libraries directory. I made that mistake when I copied it to my Additional Libraries directory. Can't remember if I got that error though...

- Colin.
 

kisoft

Well-Known Member
Licensed User
Longtime User
The library is visible as 1.50 and is in the directory C:\Program Files (x86)\Anywhere Software\Basic4android\Libraries

I'm getting a syntax error:
unknown user: ConsentState
Expected non code block
unknown user: ShowConsentForm
unknown user: ConsentState
unknown user: Consent
unregistered "CONSENT" variable it is used before assigning any value
unregistered "CONSENT" variable it is used before assigning any value
unregistered "CONSENT" variable it is used before assigning any value
 

tufanv

Expert
Licensed User
Longtime User
I am still not sure , if we have to implement this or not . Is there a specific google announcement that this HAS TO BE DONE ?
 
Status
Not open for further replies.
Top