This guide explains how to provide targeting information to an ad request.
To see ad targeting in action, download the Android API Demo app.
- Complete the Get Started guide.
PublisherAdRequest object collects targeting information to be sent with
an ad request.
During development, it is recommended to utilize test ads to avoid generating false impressions. Additionally, you can always count on a test ad being available.
Set up test ads by passing your hashed Device ID to
PublisherAdRequest adRequest = new PublisherAdRequest.Builder() .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) // All emulators .addTestDevice("AC98C820A50B4AD8A2106EDE96FB87D4") // My Galaxy Nexus test phone .build();
logcat output for a device's MD5-hashed ID. Remember to add a test
device ID for each device for which you want to request test ads.
You can pass custom key-value pairs to target DFP campaigns (line items) through
// Example: Pass custom targeting "age=25". PublisherAdRequest newRequest = new PublisherAdRequest.Builder() .addCustomTargeting("age", "25") .build();
You can pass multiple values for a key as a list of strings. For example, to target individuals in their mid-twenties rather than just 25 year olds.
.addCustomTargeting("age", Arrays.asList("24", "25", "26"))
See the DFP Custom Targeting example for an implementation of custom targeting in the Android API Demo app.
// Example: Exclude "automobile" and "boat" categories. PublisherAdRequest newRequest = new PublisherAdRequest.Builder() .addCategoryExclusion("automobile") .addCategoryExclusion("boat") .build();
See the DFP Category Exclusions example for an implementation of category exclusions in the Android API Demo app.
If a user has granted your app location permissions, DFP automatically passes this location data to the SDK. The SDK uses this data to improve ad targeting without requiring any code changes in your app. You can, of course, enable or disable location data for ads.
Autopopulated location information is not forwarded to mediation networks
and it may also be disabled entirely. Therefore, the SDK provides the
ability to set location manually. You can specify location-targeting
information in the
PublisherAdRequest as follows:
PublisherAdRequest request = new PublisherAdRequest.Builder() .setLocation(location) .build();
Out of respect for user privacy, Google asks that you specify location only if that information is already being used by your app.
Designed for Families setting
If you have opted your app in to Google Play's Designed for Families program and you show ads in your app, you need to ensure those ads comply with the Designed for Families program requirements and ad policies.
Ad requests can be tagged as designed for families by setting the
is_designed_for_families parameter to
true in the extras:
Bundle extras = new Bundle(); extras.putBoolean("is_designed_for_families", true); PublisherAdRequest request = new PublisherAdRequest.Builder() .addNetworkExtrasBundle(AdMobAdapter.class, extras) .build();
For purposes of the Children's Online Privacy Protection Act (COPPA), there is a setting called "tag for child directed treatment".
As an app developer, you can indicate whether you want Google to treat your
content as child-directed when you make an ad request. If you indicate that
you want Google to treat your content as child-directed, we take steps to
disable IBA and remarketing ads on that ad request. The setting can be used
with all versions of the Google Play services SDK, via
trueindicates that you want your content treated as child-directed for purposes of COPPA.
falseindicates that you don't want your content treated as child-directed for purposes of COPPA.
- Not setting
tagForChildDirectedTreatmentprovides no indication of how you would like your content treated with respect to COPPA.
Here's how to set
PublisherAdRequest request = new PublisherAdRequest.Builder() .tagForChildDirectedTreatment(true) .build();
By setting this tag, you certify that this notification is accurate and you are authorized to act on behalf of the owner of the app. You understand that abuse of this setting may result in termination of your Google account.
Users under the age of consent
You can mark your ad requests to receive treatment for users in the European Economic Area (EEA) under the age of consent. This feature is designed to help facilitate compliance with the General Data Protection Regulation (GDPR). Note that you may have other legal obligations under GDPR. Please review the European Union’s guidance and consult with your own legal counsel. Please remember that Google's tools are designed to facilitate compliance and do not relieve any particular publisher of its obligations under the law. Learn more about how the GDPR affects publishers.
When using this feature, a Tag For Users under the Age of Consent in Europe (TFUA) parameter will be included in the ad request. This parameter disables personalized advertising, including remarketing, for that specific ad request. It also disables requests to third-party ad vendors, such as ad measurement pixels and third-party ad servers.
The setting can be used with all versions of the Google Mobile Ads SDK by
tag_for_under_age_of_consent network extra.
trueto indicate that you want the ad request to be handled in a manner suitable for users under the age of consent.
- Not setting
tag_for_under_age_of_consentindicates that you don't want the ad request to be handled in a manner suitable for users under the age of consent.
The following example indicates that you want TFUA included in your ad request:
Bundle extras = new Bundle(); extras.putBoolean("tag_for_under_age_of_consent", true); PublisherAdRequest request = new PublisherAdRequest.Builder() .addNetworkExtrasBundle(AdMobAdapter.class, extras) .build();
val extras = Bundle() extras.putBoolean("tag_for_under_age_of_consent", true) val request = PublisherAdRequest.Builder() .addNetworkExtrasBundle(AdMobAdapter::class.java, extras) .build()
The tags to enable the Child-directed setting
tag_for_under_age_of_consent should not both simultaneously be set to true.
If they are, the child-directed setting takes precedence.
We recommend that third-party libraries that reference the Mobile Ads SDK call
to denote the platform from which the ad request originated. For example,
call this method with "CoolAds" if a third-party ad network called "CoolAds"
mediates requests to the Mobile Ads SDK:
PublisherAdRequest newRequest = new PublisherAdRequest.Builder() .setRequestAgent("CoolAds") .build();
Loading an ad with targeting
Once your request targeting information is set, call
loadAd on the
PublisherAdView with your
PublisherAdRequest adRequest = new PublisherAdRequest.Builder() .tagForChildDirectedTreatment(true) .build(); PublisherAdView.loadAd(adRequest);
Publisher provided identifiers
You can set a publisher provided identifier (PPID) for use in frequency capping, audience segmentation and targeting, sequential ad rotation, and other audience-based ad delivery controls across devices.
Here's an example of setting the PPID:
PublisherAdRequest adRequest = new PublisherAdRequest.Builder() .setPublisherProvidedId("AB123456789") .build();
See the DFP PPID example for an implementation of publisher provided identifiers (PPID) in the Android API Demo app.
To provide a URL for content related to your app, you can call
when building a
PublisherAdRequest requestWithContent = new PublisherAdRequest.Builder() .setContentUrl("http://googleadsdeveloper.blogspot.com/2014/03/monetizing-unity-mobile-apps-just-got.html") .build();