Targeting

This guide explains how to provide targeting information to an ad request.

To see ad targeting in action, download the iOS API Demo app in Objective-C or Swift.

Download API Demo

Prerequisites

DFPRequest

The DFPRequest object collects targeting information to be sent with an ad request.

Custom targeting

To set page level custom targeting, set the CustomTargeting property on a DFPRequest with a dictionary of custom targeting information. A key can be assigned multiple values, as in section here:

Swift

let request = DFPRequest()
request.customTargeting = ["gender": "male", "section": ["sports", "finance"]]

Objective-C

DFPRequest *request = [DFPRequest request];
request.customTargeting = @{@"gender" : @"male", @"section" : @[ @"sports", @"finance"]};

See the Google Ad Manager Custom Targeting example for an implementation of custom targeting in the iOS API Demo app.

Objective-C Swift

Category exclusions

To set page-level ad category exclusions, set the categoryExclusions property on a DFPRequest with an array of exclusion labels.

Swift

let request = DFPRequest()
request.categoryExclusions = ["cars", "pets"]

Objective-C

DFPRequest *request = [DFPRequest request];
request.categoryExclusions = @[@"cars", @"pets"];

See the Ad Manager Category Exclusions example for an implementation of category exclusions in the iOS API Demo app.

Objective-C Swift

Location

If a user has granted your app location permissions, location data is automatically passed 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.

After getting the user's location, you can specify location-targeting information in the DFPRequest as follows:

Swift

let request = DFPRequest()
if let currentLocation = locationManager.location {
  request.setLocationWithLatitude(CGFloat(currentLocation.coordinate.latitude),
      longitude: CGFloat(currentLocation.coordinate.longitude),
      accuracy: CGFloat(currentLocation.horizontalAccuracy))
}

Objective-C

DFPRequest *request = [DFPRequest request];
CLLocation *currentLocation = locationManager.location;
if (currentLocation) {
  [request setLocationWithLatitude:currentLocation.coordinate.latitude
                         longitude:currentLocation.coordinate.longitude
                          accuracy:currentLocation.horizontalAccuracy];
}

Out of respect for user privacy, Google asks that you only specify location if that information is already used by your app.

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 will take steps to disable IBA and remarketing ads on that ad request.

  • If you set tagForChildDirectedTreatment to YES or 'true', you will indicate that your content should be treated as child-directed for purposes of COPPA.
  • If you set tagForChildDirectedTreatment to NO or 'false', you will indicate that your content should not be treated as child-directed for purposes of COPPA.
  • If you do not set tagForChildDirectedTreatment, ad requests will include no indication of how you would like your content treated with respect to COPPA.

Swift

let request = DFPRequest()
request.tag(forChildDirectedTreatment: true)

Objective-C

DFPRequest *request = [DFPRequest request];
[request tagForChildDirectedTreatment:YES];

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.

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 using the tag_for_under_age_of_consent network extra.

  • Set tag_for_under_age_of_consent to @YES or true to 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_consent indicates 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:

Swift

let request = DFPRequest();
let extras = GADExtras();
extras.additionalParameters = ["tag_for_under_age_of_consent": true];
request.register(extras)

Objective-C

DFPRequest *request = [DFPRequest request];
GADExtras *extras = [[GADExtras alloc] init];
extras.additionalParameters = @{@"tag_for_under_age_of_consent": @YES};
[request registerAdNetworkExtras:extras];

The tags to enable the Child-directed setting and tag_for_under_age_of_consent should not both simultaneously be set to true. If they are, the child-directed setting takes precedence.

Content URL

Applications that monetize content matching a webpage's content may pass a content URL for keyword targeting.

For example, if your app is requesting an ad while showing content from https://www.example.com, you can pass the URL to target relevant keywords:

Swift

let request = DFPRequest()
request.contentURL = "https://www.example.com"

Objective-C

DFPRequest *request = [DFPRequest request];
request.contentURL = @"https://www.example.com";

Loading an ad with targeting

Once all of your request targeting information is set, call loadRequest on DFPBannerView with your DFPRequest instance.

Swift

let request = DFPRequest()
request.gender = .male
var components = DateComponents()
components.month = 3
components.day = 13
components.year = 1976
request.birthday = Calendar.current.date(from: components)
request.tag(forChildDirectedTreatment: true)
request.contentURL = "https://www.example.com"
adView.load(request)

Objective-C

DFPRequest *request = [DFPRequest request];
request.gender = kGADGenderMale;
NSDateComponents *components = [[NSDateComponents alloc] init];
components.month = 3;
components.day = 13;
components.year = 1976;
request.birthday = [[NSCalendar currentCalendar] dateFromComponents:components];
[request tagForChildDirectedTreatment:YES];
request.contentURL = @"https://www.example.com";
[self.adView loadRequest:request];

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.

Setting the PPID is done through DFPRequest:

Swift

let request = DFPRequest()
request.publisherProvidedID = "AB123456789"

Objective-C

DFPRequest *request = [DFPRequest request];
request.publisherProvidedID = @"AB123456789";

See the Ad Manager PPID example for an implementation of publisher provided identifiers (PPID) in the iOS API Demo app.

Objective-C Swift

FAQ

Can I release my app with request.testDevices?
Yes. Test ads are only shown on specific devices that you specify, so all of your users will still receive production ads.
What targeting gets used when an ad automatically refreshes?
On ad refresh, the previously specified DFPRequest object is used for targeting again. To set new targeting, explicitly call loadRequest on DFPBannerView with a new DFPRequest object.
How do I pass extra targeting parameters to mediation networks?
Specify additional request parameters describes how to send targeting to mediation networks.

发送以下问题的反馈:

此网页
Mobile Ads SDK for iOS
Mobile Ads SDK for iOS
需要帮助?请访问我们的支持页面