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
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.Builder.addTestDevice:
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 PublisherAdRequest.Builder.addCustomTargeting:
// 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();
This setting is separate from the Children's Online Privacy Protection Act (COPPA) "tag for child directed treatment" setting. For COPPA-related compliance, see the child-directed setting.
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.
tagForChildDirectedTreatmentprovides no indication of how you would like your content treated with respect to COPPA.
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.
It may take some time for this designation to take effect in applicable Google services.
We recommend that third-party libraries that reference the Mobile Ads SDK call setRequestAgent 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
PublisherAdRequest adRequest = new PublisherAdRequest.Builder() .setGender(AdRequest.GENDER_FEMALE) .setBirthday(new GregorianCalendar(1985, 1, 1).getTime()) .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();