Skip to content

Commit fd391d2

Browse files
committed
Proposal: Targeting support
I'm not sure if using nested prop is the right approach, so if you have any suggestions, please let me know!
1 parent 45f1106 commit fd391d2

File tree

8 files changed

+144
-2
lines changed

8 files changed

+144
-2
lines changed

Example/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,15 @@ export default class Example extends Component {
139139
validAdSizes={['banner', 'largeBanner', 'mediumRectangle']}
140140
adUnitID="/6499/example/APIDemo/AdSizes"
141141
ref={el => (this._adSizesExample = el)}
142+
targeting={{
143+
customTargeting: { group: "users" },
144+
categoryExclusions: ['media'],
145+
gender: 'male',
146+
birthday: new Date(1989, 6, 9),
147+
childDirectedTreatment: true,
148+
contentURL: 'admob://',
149+
publisherProvidedID: 'abc123',
150+
}}
142151
/>
143152
<Button
144153
title="Reload"

RNPublisherBanner.js

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
findNodeHandle,
77
ViewPropTypes,
88
} from 'react-native';
9-
import { string, func, arrayOf } from 'prop-types';
9+
import { string, func, arrayOf, bool, object, shape, instanceOf, oneOf, number } from 'prop-types';
1010

1111
import { createErrorFromErrorData } from './utils';
1212

@@ -119,6 +119,62 @@ PublisherBanner.propTypes = {
119119
onAdViewDidDismissScreen: func,
120120
onAdViewWillLeaveApplication: func,
121121
onAdmobDispatchAppEvent: func,
122+
123+
targeting: shape({
124+
/**
125+
* Arbitrary object of custom targeting information.
126+
*/
127+
customTargeting: object,
128+
129+
/**
130+
* Array of exclusion labels.
131+
*/
132+
categoryExclusions: arrayOf(string),
133+
134+
/**
135+
* Array of keyword strings.
136+
*/
137+
keywords: arrayOf(string),
138+
139+
/**
140+
* When using backfill or an SDK mediation creative, gender can be supplied
141+
* in the ad request for targeting purposes.
142+
*/
143+
gender: oneOf(['unknown', 'male', 'female']),
144+
145+
/**
146+
* When using backfill or an SDK mediation creative, birthday can be supplied
147+
* in the ad request for targeting purposes.
148+
*/
149+
birthday: instanceOf(Date),
150+
151+
/**
152+
* Indicate that you want Google to treat your content as child-directed.
153+
*/
154+
childDirectedTreatment: bool,
155+
156+
/**
157+
* Applications that monetize content matching a webpage's content may pass
158+
* a content URL for keyword targeting.
159+
*/
160+
contentURL: string,
161+
162+
/**
163+
* You can set a publisher provided identifier (PPID) for use in frequency
164+
* capping, audience segmentation and targeting, sequential ad rotation, and
165+
* other audience-based ad delivery controls across devices.
166+
*/
167+
publisherProvidedID: string,
168+
169+
/**
170+
* The user’s current location may be used to deliver more relevant ads.
171+
*/
172+
location: shape({
173+
latitude: number,
174+
longitude: number,
175+
accuracy: number,
176+
}),
177+
}),
122178
};
123179

124180
const RNDFPBannerView = requireNativeComponent('RNDFPBannerView', PublisherBanner);

ios/RCTConvert+GADGender.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#import <React/RCTConvert.h>
2+
@import GoogleMobileAds;
3+
4+
@interface RCTConvert (GoogleMobileAds)
5+
6+
+ (GADGender)GADGender:(id)json;
7+
8+
@end

ios/RCTConvert+GADGender.m

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#import "RCTConvert+GADGender.h"
2+
3+
@implementation RCTConvert (GoogleMobileAds)
4+
5+
+ (GADGender)GADGender:(id)json
6+
{
7+
NSString *gender = [self NSString:json];
8+
if ([gender isEqualToString:@"male"]) {
9+
return kGADGenderMale;
10+
} else if ([gender isEqualToString:@"female"]) {
11+
return kGADGenderFemale;
12+
} else {
13+
return kGADGenderUnknown;;
14+
}
15+
}
16+
17+
@end

ios/RNAdMobManager.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
0CB8C04B1D913E00002BC3EF /* RNDFPBannerView.m in Sources */ = {isa = PBXBuildFile; fileRef = 0CB8C04A1D913E00002BC3EF /* RNDFPBannerView.m */; };
1111
0CB8C04E1D9143A6002BC3EF /* RNDFPBannerViewManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 0CB8C04D1D9143A6002BC3EF /* RNDFPBannerViewManager.m */; };
1212
5E86A6471F126FCE008013EB /* RCTConvert+GADAdSize.m in Sources */ = {isa = PBXBuildFile; fileRef = 5E86A6461F126FCE008013EB /* RCTConvert+GADAdSize.m */; };
13+
5EE0EED01F327DFD0045CC2C /* RCTConvert+GADGender.m in Sources */ = {isa = PBXBuildFile; fileRef = 5EE0EECF1F327DFD0045CC2C /* RCTConvert+GADGender.m */; };
1314
A90F2F8C1D50AEF200F2A2E3 /* RNAdMobRewarded.m in Sources */ = {isa = PBXBuildFile; fileRef = A90F2F8B1D50AEF200F2A2E3 /* RNAdMobRewarded.m */; };
1415
A962C2531CB27DBD00E508A1 /* RNAdMobInterstitial.m in Sources */ = {isa = PBXBuildFile; fileRef = A962C2521CB27DBD00E508A1 /* RNAdMobInterstitial.m */; };
1516
A96DA7841C146DA600FC639B /* RNGADBannerView.m in Sources */ = {isa = PBXBuildFile; fileRef = A96DA7811C146DA600FC639B /* RNGADBannerView.m */; };
@@ -35,6 +36,8 @@
3536
0CB8C04D1D9143A6002BC3EF /* RNDFPBannerViewManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNDFPBannerViewManager.m; sourceTree = SOURCE_ROOT; };
3637
5E86A6451F126FCE008013EB /* RCTConvert+GADAdSize.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "RCTConvert+GADAdSize.h"; sourceTree = SOURCE_ROOT; };
3738
5E86A6461F126FCE008013EB /* RCTConvert+GADAdSize.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "RCTConvert+GADAdSize.m"; sourceTree = SOURCE_ROOT; };
39+
5EE0EECE1F327DFD0045CC2C /* RCTConvert+GADGender.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "RCTConvert+GADGender.h"; sourceTree = SOURCE_ROOT; };
40+
5EE0EECF1F327DFD0045CC2C /* RCTConvert+GADGender.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "RCTConvert+GADGender.m"; sourceTree = SOURCE_ROOT; };
3841
A90F2F8A1D50AEF200F2A2E3 /* RNAdMobRewarded.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RNAdMobRewarded.h; sourceTree = SOURCE_ROOT; };
3942
A90F2F8B1D50AEF200F2A2E3 /* RNAdMobRewarded.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNAdMobRewarded.m; sourceTree = SOURCE_ROOT; };
4043
A962C2511CB27DBD00E508A1 /* RNAdMobInterstitial.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RNAdMobInterstitial.h; sourceTree = SOURCE_ROOT; };
@@ -90,6 +93,8 @@
9093
A90F2F8B1D50AEF200F2A2E3 /* RNAdMobRewarded.m */,
9194
5E86A6451F126FCE008013EB /* RCTConvert+GADAdSize.h */,
9295
5E86A6461F126FCE008013EB /* RCTConvert+GADAdSize.m */,
96+
5EE0EECE1F327DFD0045CC2C /* RCTConvert+GADGender.h */,
97+
5EE0EECF1F327DFD0045CC2C /* RCTConvert+GADGender.m */,
9398
);
9499
path = RNAdMobManager;
95100
sourceTree = "<group>";
@@ -158,6 +163,7 @@
158163
A96DA7851C146DA600FC639B /* RNGADBannerViewManager.m in Sources */,
159164
5E86A6471F126FCE008013EB /* RCTConvert+GADAdSize.m in Sources */,
160165
0CB8C04E1D9143A6002BC3EF /* RNDFPBannerViewManager.m in Sources */,
166+
5EE0EED01F327DFD0045CC2C /* RCTConvert+GADGender.m in Sources */,
161167
);
162168
runOnlyForDeploymentPostprocessing = 0;
163169
};

ios/RNDFPBannerView.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
@property (nonatomic, copy) NSArray *validAdSizes;
1414
@property (nonatomic, copy) NSArray *testDevices;
15+
@property (nonatomic, copy) NSDictionary *targeting;
1516

1617
@property (nonatomic, copy) RCTBubblingEventBlock onSizeChange;
1718
@property (nonatomic, copy) RCTBubblingEventBlock onAdmobDispatchAppEvent;

ios/RNDFPBannerView.m

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#endif
1010

1111
#include "RCTConvert+GADAdSize.h"
12+
#include "RCTConvert+GADGender.h"
1213

1314
@implementation RNDFPBannerView
1415
{
@@ -42,8 +43,51 @@ - (instancetype)initWithFrame:(CGRect)frame
4243
}
4344

4445
- (void)loadBanner {
45-
GADRequest *request = [GADRequest request];
46+
DFPRequest *request = [DFPRequest request];
4647
request.testDevices = _testDevices;
48+
49+
if (_targeting != nil) {
50+
NSDictionary *customTargeting = [_targeting objectForKey:@"customTargeting"];
51+
if (customTargeting != nil) {
52+
request.customTargeting = customTargeting;
53+
}
54+
NSArray *categoryExclusions = [_targeting objectForKey:@"categoryExclusions"];
55+
if (categoryExclusions != nil) {
56+
request.categoryExclusions = categoryExclusions;
57+
}
58+
NSArray *keywords = [_targeting objectForKey:@"keywords"];
59+
if (keywords != nil) {
60+
request.keywords = keywords;
61+
}
62+
NSString *gender = [_targeting objectForKey:@"gender"];
63+
if (gender != nil) {
64+
request.gender = [RCTConvert GADGender:gender];
65+
}
66+
NSDate *birthday = [_targeting objectForKey:@"birthday"];
67+
if (birthday != nil) {
68+
request.birthday = [RCTConvert NSDate:birthday];
69+
}
70+
id childDirectedTreatment = [_targeting objectForKey:@"childDirectedTreatment"];
71+
if (childDirectedTreatment != nil) {
72+
[request tagForChildDirectedTreatment:childDirectedTreatment];
73+
}
74+
NSString *contentURL = [_targeting objectForKey:@"contentURL"];
75+
if (contentURL != nil) {
76+
request.contentURL = contentURL;
77+
}
78+
NSString *publisherProvidedID = [_targeting objectForKey:@"publisherProvidedID"];
79+
if (publisherProvidedID != nil) {
80+
request.publisherProvidedID = publisherProvidedID;
81+
}
82+
NSDictionary *location = [_targeting objectForKey:@"location"];
83+
if (location != nil) {
84+
CGFloat latitude = [[location objectForKey:@"latitude"] doubleValue];
85+
CGFloat longitude = [[location objectForKey:@"longitude"] doubleValue];
86+
CGFloat accuracy = [[location objectForKey:@"accuracy"] doubleValue];
87+
[request setLocationWithLatitude:latitude longitude:longitude accuracy:accuracy];
88+
}
89+
}
90+
4791
[_bannerView loadRequest:request];
4892
}
4993

ios/RNDFPBannerViewManager.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ - (dispatch_queue_t)methodQueue
4141
RCT_REMAP_VIEW_PROPERTY(adUnitID, _bannerView.adUnitID, NSString)
4242
RCT_EXPORT_VIEW_PROPERTY(validAdSizes, NSArray)
4343
RCT_EXPORT_VIEW_PROPERTY(testDevices, NSArray)
44+
RCT_EXPORT_VIEW_PROPERTY(targeting, NSDictionary)
4445

4546
RCT_EXPORT_VIEW_PROPERTY(onSizeChange, RCTBubblingEventBlock)
4647
RCT_EXPORT_VIEW_PROPERTY(onAdmobDispatchAppEvent, RCTBubblingEventBlock)

0 commit comments

Comments
 (0)