In this tutorial I'm going to show you how you can set Admob GDPR setup in admob form and in android stuido coding.
Follow these steps:
1. Login to your admob account and click on Create a GDPR message
2. Click on Create GDPR message
3. Include the Do not Consent button by selecting it and select option ON.
4. Click on Select Apps
- Android: https://developers.google.com/admob/ump/android/quick-start
- iOS: https://developers.google.com/admob/ump/ios/quick-start
8. If you are using admob ads sdk version less than 19.8.0, you have to add following dependency in your app level build.gradle.
implementation 'com.google.android.ump:user-messaging-platform:2.0.0'
If you are using the latest sdk of admob, there is no need to add this dependency11. Now you have to past the following piece of code in onCreate method. Remember you have to add hashed device id in code. If you are using emulator, then it is not needed. If you are running app on physical device, you should know the hashed device id. To know this id, you have to run any app containing admob ads and in logcat search for device id. You will find it.
ConsentDebugSettings debugSettings = new ConsentDebugSettings.Builder(this)consentInformation = UserMessagingPlatform.getConsentInformation(this);
.setDebugGeography(ConsentDebugSettings.DebugGeography.DEBUG_GEOGRAPHY_EEA)
.addTestDeviceHashedId("TEST-DEVICE-HASHED-ID")
.build();
ConsentRequestParameters params = new ConsentRequestParameters
.Builder()
.setTagForUnderAgeOfConsent(false)
.setConsentDebugSettings(debugSettings)
.build();
consentInformation.requestConsentInfoUpdate(
this,
params,
new ConsentInformation.OnConsentInfoUpdateSuccessListener() {
@Override
public void onConsentInfoUpdateSuccess() {
// The consent information state was updated.
// You are now ready to check if a form is available.
if (consentInformation.isConsentFormAvailable()) {
loadForm();
}
}
},
new ConsentInformation.OnConsentInfoUpdateFailureListener() {
@Override
public void onConsentInfoUpdateFailure(FormError formError) {
// Handle the error.
}
});
consentInformation = UserMessagingPlatform.getConsentInformation(this);
consentInformation.requestConsentInfoUpdate(this,
params,
new ConsentInformation.OnConsentInfoUpdateSuccessListener() {
@Override
public void onConsentInfoUpdateSuccess() {
// The consent information state was updated.
// You are now ready to check if a form is available.
}
},
new ConsentInformation.OnConsentInfoUpdateFailureListener() {
@Override
public void onConsentInfoUpdateFailure(FormError formError) {
// Handle the error.
}
});
consentInformation.reset();
12. Now run your app on your device and check whether the consent form in being loaded or not. As you have implemented testing code also as you are in EEA or UK region, so consent form should be shown to you. Once consent form is loaded, you can click on Consent button. If you click on consent button, google admob ads could run on your app. If you click on Do not Consent button, then google admob ads will not be shown in your app.
13. Please note that we have also included test location of EEA or UK region. Once tested the form you have to remove that code. Now create a method of loadForm() as following outside of onCreate method.
public void loadForm() {
// Loads a consent form. Must be called on the main thread.
UserMessagingPlatform.loadConsentForm(
this,
new UserMessagingPlatform.OnConsentFormLoadSuccessListener() {
@Override
public void onConsentFormLoadSuccess(ConsentForm consentForm) {
MainActivity.this.consentForm = consentForm;
if (consentInformation.getConsentStatus() == ConsentInformation.ConsentStatus.REQUIRED) {
consentForm.show(
MainActivity.this,
new ConsentForm.OnConsentFormDismissedListener() {
@Override
public void onConsentFormDismissed(@Nullable FormError formError) {
if (consentInformation.getConsentStatus() == ConsentInformation.ConsentStatus.OBTAINED) {
// App can start requesting ads.
}
// Handle dismissal by reloading form.
loadForm();
}
});
}
}
},
new UserMessagingPlatform.OnConsentFormLoadFailureListener() {
@Override
public void onConsentFormLoadFailure(FormError formError) {
// Handle Error.
}
}
);
}
14. Finally run your app on device and consent form should be shown on your app. If consent form is not being shown, the internet connection may be slow or app is not published on playstore. Now finally you have to initialize admob ads as usual after consent form code and do coding for banner and interstitial ads as you do earlier. First write code of consent form and then write code of ads initialize sdk, then banner interstitail ads etc.
15. Now if the user clicks on Consent, then admob ads will be shown to the user, if user clicks on Do not Consent, admob ads will not be shown to the user.
16. After testing the app, you have to remove following lines of code from your project.
ConsentDebugSettings debugSettings = new ConsentDebugSettings.Builder(this)Also comment or remove these two lines
.setDebugGeography(ConsentDebugSettings.DebugGeography.DEBUG_GEOGRAPHY_EEA)
.addTestDeviceHashedId("TEST-DEVICE-HASHED-ID")
.build();
.setConsentDebugSettings(debugSettings)
consentInformation.reset();
This time if you will be in EEA or UK region, the consent form will be shown otherwise it will not be loaded.
The complete MainActivity code after removing the testing code is here
public class MainActivity extends AppCompatActivity {private InterstitialAd adMobIntAd;private ConsentInformation consentInformation;
private ConsentForm consentForm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ConsentDebugSettings debugSettings = new ConsentDebugSettings.Builder(this)
// .setDebugGeography(ConsentDebugSettings.DebugGeography.DEBUG_GEOGRAPHY_EEA)
// .addTestDeviceHashedId("TEST-DEVICE-HASHED-ID")
// .build();
ConsentRequestParameters params = new ConsentRequestParameters
.Builder()
.setTagForUnderAgeOfConsent(false)
// .setConsentDebugSettings(debugSettings)
.build();
consentInformation = UserMessagingPlatform.getConsentInformation(this);
consentInformation.requestConsentInfoUpdate(
this,
params,
new ConsentInformation.OnConsentInfoUpdateSuccessListener() {
@Override
public void onConsentInfoUpdateSuccess() {
// The consent information state was updated.
// You are now ready to check if a form is available.
if (consentInformation.isConsentFormAvailable()) {
loadForm();
}
}
},
new ConsentInformation.OnConsentInfoUpdateFailureListener() {
@Override
public void onConsentInfoUpdateFailure(FormError formError) {
// Handle the error.
}
});
consentInformation = UserMessagingPlatform.getConsentInformation(this);
consentInformation.requestConsentInfoUpdate(this,
params,
new ConsentInformation.OnConsentInfoUpdateSuccessListener() {
@Override
public void onConsentInfoUpdateSuccess() {
// The consent information state was updated.
// You are now ready to check if a form is available.
}
},
new ConsentInformation.OnConsentInfoUpdateFailureListener() {
@Override
public void onConsentInfoUpdateFailure(FormError formError) {
// Handle the error.
}
});
//consentInformation.reset();
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
Map<String, AdapterStatus> statusMap = initializationStatus.getAdapterStatusMap();
for (String adapterClass : statusMap.keySet()) {
AdapterStatus status = statusMap.get(adapterClass);
Log.d("MyApp", String.format(
"Adapter name: %s, Description: %s, Latency: %d",
adapterClass, status.getDescription(), status.getLatency()));
}
LoadAdmobInterstitialAds();
//MediationTestSuite.launch(MainActivity.this);
}
});
ShowAllInterstitialAds();
}
private void LoadAdmobInterstitialAds() {
AdRequest adRequest = new AdRequest.Builder().build();
InterstitialAd.load(this, getString(R.string.admobinterstitial), adRequest, new InterstitialAdLoadCallback() {
@Override
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
adMobIntAd = interstitialAd;
}
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
adMobIntAd = null;
}
});
}
public void ShowAllInterstitialAds() {
if (adMobIntAd != null) {
adMobIntAd.show(MainActivity.this);
adMobIntAd.setFullScreenContentCallback(new FullScreenContentCallback() {
});
}
}
public void loadForm() {
// Loads a consent form. Must be called on the main thread.
UserMessagingPlatform.loadConsentForm(
this,
new UserMessagingPlatform.OnConsentFormLoadSuccessListener() {
@Override
public void onConsentFormLoadSuccess(ConsentForm consentForm) {
MainActivity.this.consentForm = consentForm;
if (consentInformation.getConsentStatus() == ConsentInformation.ConsentStatus.REQUIRED) {
consentForm.show(
MainActivity.this,
new ConsentForm.OnConsentFormDismissedListener() {
@Override
public void onConsentFormDismissed(@Nullable FormError formError) {
if (consentInformation.getConsentStatus() == ConsentInformation.ConsentStatus.OBTAINED) {
// App can start requesting ads.
}
// Handle dismissal by reloading form.
loadForm();
}
});
}
}
},
new UserMessagingPlatform.OnConsentFormLoadFailureListener() {
@Override
public void onConsentFormLoadFailure(FormError formError) {
// Handle Error.
}
}
);
}
}
You can also watch the video tutorial about how to do GDPR Form submission in admob
Having any problem or suggestion, you can comment.