Getting GDPR ready for AdMob | Implementing Google Consent SDK

According to GDPR any businesses and organizations who are established in Europe or who serve users in Europe and collect their personal data require the consent of subjects for data processing.

As AdMob by default collects personal information of users, developers who use AdMob for monetization are required to pass on consent to AdMob SDK.

In this tutorial, we will learn how to get GDPR ready using Google Consent SDK for Admob.

1) Setting up AdMob for Consent SDK

  • Visit AdMob website
  • Select Blocking control from the left navigation bar and then click on EU User Consent
    EU User Consent - AdMob
  • Copy your publisher ID present at the bottom of the page. It should be in the following format: pub-xxxxxxxxxxxxxxxx
    Copy Publisher ID - EU User Consent
  • Now click on “SELECT PROVIDER” under “Custom set of ad technology providers”  and make sure that Google is selected as Ad technology provider
    Ad technology providers - EU User Consent
  • Now click on Save changes

2) Code for Requesting and Passing Consent to AdMob SDK

Now add following libraries to your apps build.gradle  file

implementation 'com.google.android.gms:play-services-ads:17.0.0'
implementation 'com.android.support:customtabs:28.0.0'
implementation 'com.google.android.ads.consent:consent-library:1.0.6'

Then, go to your strings.xml file and add the following string resources – Publisher ID you copied from AdMob, AdMob app ID, Ad unit ID for the banner and interstitial.

<!-- Replace them with your own -->
<string name="admobPublisherId">pub-0123456789012345</string>
<string name="admob_app_id">ca-app-pub-3940256099942544~3347511713</string>
<string name="bannerAdUnitId">ca-app-pub-3940256099942544/6300978111</string>
<string name="interstitialAdUnitId">ca-app-pub-3940256099942544/1033173712</string>
<string name="rewardedVideoAdUnitId">ca-app-pub-3940256099942544/5224354917</string>

Add the following code inside the application tag in the AndroidManifest.xml file

<meta-data
    android:name="com.google.android.gms.ads.APPLICATION_ID"
    android:value="@string/admob_app_id"/>

Open your activity_main.xml file and add the following code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/start_interstitial_ad"
android:text="Interstitial Ad"
android:layout_width="match_parent"
android:layout_height="60dp"
/>
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="@string/bannerAdUnitId"
/>
</LinearLayout>

Add following code in your MainActivity.java file. Replace the things mentioned in Todo with your own data.

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.google.ads.consent.ConsentForm;
import com.google.ads.consent.ConsentFormListener;
import com.google.ads.consent.ConsentInfoUpdateListener;
import com.google.ads.consent.ConsentInformation;
import com.google.ads.consent.ConsentStatus;
import com.google.ads.mediation.admob.AdMobAdapter;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.MobileAds;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

private int PERSONALIZED_ADS = 0;
private int NON_PERSONALIZED_ADS = 1;

private InterstitialAd mInterstitialAd;
private AdView adView;
private ConsentForm form;

private TextView startInterstitial;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

adView = findViewById(R.id.adView);
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId(getString(R.string.interstitialAdUnitId));

MobileAds.initialize(this, getString(R.string.admob_app_id));

startInterstitial = findViewById(R.id.start_interstitial_ad);
startInterstitial.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
}
});
checkForConsent();
}

private void checkForConsent() {
ConsentInformation consentInformation = ConsentInformation.getInstance(MainActivity.this);
// Todo: Replace with your admob publisherId
String[] publisherIds = {getString(R.string.admobPublisherId )};
consentInformation.requestConsentInfoUpdate(publisherIds, new ConsentInfoUpdateListener() {
@Override
public void onConsentInfoUpdated(ConsentStatus consentStatus) {
// User's consent status successfully updated.
switch (consentStatus) {
case PERSONALIZED:
showPersonalizedAds();
break;
case NON_PERSONALIZED:
showNonPersonalizedAds();
break;
case UNKNOWN:
if (ConsentInformation.getInstance(getBaseContext())
.isRequestLocationInEeaOrUnknown()) {
requestConsent();
} else {
showPersonalizedAds();
}
break;
default:
showNonPersonalizedAds();
break;
}
}

@Override
public void onFailedToUpdateConsentInfo(String errorDescription) {
// User's consent status failed to update.
}
});
}

private void requestConsent() {
URL privacyUrl = null;
try {
// TODO: Replace with your app's privacy policy URL.
privacyUrl = new URL("https://apps-privacy.policy/");
} catch (MalformedURLException e) {
e.printStackTrace();
}
form = new ConsentForm.Builder(MainActivity.this, privacyUrl)
.withListener(new ConsentFormListener() {
@Override
public void onConsentFormLoaded() {
// Consent form loaded successfully.
showForm();
}

@Override
public void onConsentFormOpened() {
// Consent form was displayed.
}

@Override
public void onConsentFormClosed(
ConsentStatus consentStatus, Boolean userPrefersAdFree) {
if (userPrefersAdFree) {
// Todo: User prefers ad-free option, Buy or Subscribe
} else {
switch (consentStatus) {
case PERSONALIZED:
showPersonalizedAds();
break;
case NON_PERSONALIZED:
showNonPersonalizedAds();
break;
case UNKNOWN:
showNonPersonalizedAds();
break;
}
}
// Consent form was closed.
}

@Override
public void onConsentFormError(String errorDescription) {
// Consent form error.
showPersonalizedAds();
}
})
.withPersonalizedAdsOption()
.withNonPersonalizedAdsOption()
.withAdFreeOption()
.build();
form.load();
}

private void showForm() {
form.show();
}

private void showNonPersonalizedAds() {
showBanner(NON_PERSONALIZED_ADS);
showInterstitial(NON_PERSONALIZED_ADS);
}

private void showPersonalizedAds() {
showBanner(NON_PERSONALIZED_ADS);
showInterstitial(PERSONALIZED_ADS);
}

private static Bundle getNonPersonalizedAdsBundle() {
Bundle extras = new Bundle();
extras.putString("npa", "1");
return extras;
}

private void showBanner(int adsType) {
AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
if (adsType == NON_PERSONALIZED_ADS) {
adRequestBuilder
.addNetworkExtrasBundle(
AdMobAdapter.class,
getNonPersonalizedAdsBundle()
);
}
adView.loadAd(adRequestBuilder.build());
}

private void showInterstitial(int adsType) {
AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
if (adsType == NON_PERSONALIZED_ADS) {
adRequestBuilder.addNetworkExtrasBundle(
AdMobAdapter.class,
getNonPersonalizedAdsBundle()
);
}

final AdRequest adRequest = adRequestBuilder.build();
mInterstitialAd.loadAd(adRequest);
mInterstitialAd.setAdListener( new AdListener() {
@Override
public void onAdClosed() {
mInterstitialAd.loadAd(adRequest);
}
});
}
}

Here is the brief summary of how MainActivity.java works

  • In the checkForConsent() function we first check if a user has already given permission for showing personalized or non-personalized ads.
  • If permission has been granted for showing ads we will show ads according to users choice.
  • If permission has not been granted we will check if user is in EU country or unknown country.
  • If user is not in EU country or unknown country we will show personalized ads without users permission.
  • If user is in EU country or unknown we will request consent from the user using the function requestConsent(). This function will show a form to the user requesting their consent.
  • User will have to choose between personalized, non-personalized ads and Ad-Free version.
  • If user chooses personalized or non-personalized ads, show them ads according to their choice.
  • If user chooses ad-free version, offer them IAP (In App Purchase) for removing ads or another version of your app which do not have ads.

The above code should make your task of asking consent from user easy. If you face any problem try debugging this code with Log statements. If you face any more problems you can comment down below.

Be the first to comment

Leave a Reply