Skip to content

Commit

Permalink
Proposal: Targeting support
Browse files Browse the repository at this point in the history
I'm not sure if using nested prop is the right approach, so if you have any suggestions, please let me know!
  • Loading branch information
koenpunt committed Sep 22, 2019
1 parent 2d1432a commit 6c5baa2
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 2 deletions.
9 changes: 9 additions & 0 deletions Example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@ export default class Example extends Component {
validAdSizes={['banner', 'largeBanner', 'mediumRectangle']}
adUnitID="/6499/example/APIDemo/AdSizes"
ref={el => (this._adSizesExample = el)}
targeting={{
customTargeting: { group: "users" },
categoryExclusions: ['media'],
gender: 'male',
birthday: new Date(1989, 6, 9),
childDirectedTreatment: true,
contentURL: 'admob://',
publisherProvidedID: 'abc123',
}}
/>
<Button
title="Reload"
Expand Down
68 changes: 67 additions & 1 deletion RNPublisherBanner.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import { arrayOf, func, string } from 'prop-types';
import {
arrayOf,
bool,
func,
instanceOf,
number,
object,
oneOf,
shape,
string,
} from 'prop-types';
import React, { Component } from 'react';
import {
findNodeHandle,
Expand Down Expand Up @@ -114,6 +124,62 @@ PublisherBanner.propTypes = {
onAdClosed: func,
onAdLeftApplication: func,
onAppEvent: func,

targeting: shape({
/**
* Arbitrary object of custom targeting information.
*/
customTargeting: object,

/**
* Array of exclusion labels.
*/
categoryExclusions: arrayOf(string),

/**
* Array of keyword strings.
*/
keywords: arrayOf(string),

/**
* When using backfill or an SDK mediation creative, gender can be supplied
* in the ad request for targeting purposes.
*/
gender: oneOf(['unknown', 'male', 'female']),

/**
* When using backfill or an SDK mediation creative, birthday can be supplied
* in the ad request for targeting purposes.
*/
birthday: instanceOf(Date),

/**
* Indicate that you want Google to treat your content as child-directed.
*/
childDirectedTreatment: bool,

/**
* Applications that monetize content matching a webpage's content may pass
* a content URL for keyword targeting.
*/
contentURL: string,

/**
* 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.
*/
publisherProvidedID: string,

/**
* The user’s current location may be used to deliver more relevant ads.
*/
location: shape({
latitude: number,
longitude: number,
accuracy: number,
}),
}),
};

const RNDFPBannerView = requireNativeComponent(
Expand Down
8 changes: 8 additions & 0 deletions ios/RCTConvert+GADGender.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#import <React/RCTConvert.h>
@import GoogleMobileAds;

@interface RCTConvert (GoogleMobileAds)

+ (GADGender)GADGender:(id)json;

@end
17 changes: 17 additions & 0 deletions ios/RCTConvert+GADGender.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#import "RCTConvert+GADGender.h"

@implementation RCTConvert (GoogleMobileAds)

+ (GADGender)GADGender:(id)json
{
NSString *gender = [self NSString:json];
if ([gender isEqualToString:@"male"]) {
return kGADGenderMale;
} else if ([gender isEqualToString:@"female"]) {
return kGADGenderFemale;
} else {
return kGADGenderUnknown;;
}
}

@end
6 changes: 6 additions & 0 deletions ios/RNAdMobManager.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
0CB8C04E1D9143A6002BC3EF /* RNDFPBannerViewManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 0CB8C04D1D9143A6002BC3EF /* RNDFPBannerViewManager.m */; };
5E2419841FE11E0F00C6B738 /* RNAdMobUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = 5E2419831FE11E0F00C6B738 /* RNAdMobUtils.m */; };
5E86A6471F126FCE008013EB /* RCTConvert+GADAdSize.m in Sources */ = {isa = PBXBuildFile; fileRef = 5E86A6461F126FCE008013EB /* RCTConvert+GADAdSize.m */; };
5EE0EED01F327DFD0045CC2C /* RCTConvert+GADGender.m in Sources */ = {isa = PBXBuildFile; fileRef = 5EE0EECF1F327DFD0045CC2C /* RCTConvert+GADGender.m */; };
A90F2F8C1D50AEF200F2A2E3 /* RNAdMobRewarded.m in Sources */ = {isa = PBXBuildFile; fileRef = A90F2F8B1D50AEF200F2A2E3 /* RNAdMobRewarded.m */; };
A962C2531CB27DBD00E508A1 /* RNAdMobInterstitial.m in Sources */ = {isa = PBXBuildFile; fileRef = A962C2521CB27DBD00E508A1 /* RNAdMobInterstitial.m */; };
A96DA7841C146DA600FC639B /* RNGADBannerView.m in Sources */ = {isa = PBXBuildFile; fileRef = A96DA7811C146DA600FC639B /* RNGADBannerView.m */; };
Expand Down Expand Up @@ -38,6 +39,8 @@
5E2419851FE11F1200C6B738 /* RNAdMobUtils.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNAdMobUtils.h; sourceTree = SOURCE_ROOT; };
5E86A6451F126FCE008013EB /* RCTConvert+GADAdSize.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "RCTConvert+GADAdSize.h"; sourceTree = SOURCE_ROOT; };
5E86A6461F126FCE008013EB /* RCTConvert+GADAdSize.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "RCTConvert+GADAdSize.m"; sourceTree = SOURCE_ROOT; };
5EE0EECE1F327DFD0045CC2C /* RCTConvert+GADGender.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "RCTConvert+GADGender.h"; sourceTree = SOURCE_ROOT; };
5EE0EECF1F327DFD0045CC2C /* RCTConvert+GADGender.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "RCTConvert+GADGender.m"; sourceTree = SOURCE_ROOT; };
A90F2F8A1D50AEF200F2A2E3 /* RNAdMobRewarded.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RNAdMobRewarded.h; sourceTree = SOURCE_ROOT; };
A90F2F8B1D50AEF200F2A2E3 /* RNAdMobRewarded.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNAdMobRewarded.m; sourceTree = SOURCE_ROOT; };
A962C2511CB27DBD00E508A1 /* RNAdMobInterstitial.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RNAdMobInterstitial.h; sourceTree = SOURCE_ROOT; };
Expand Down Expand Up @@ -95,6 +98,8 @@
A90F2F8B1D50AEF200F2A2E3 /* RNAdMobRewarded.m */,
5E86A6451F126FCE008013EB /* RCTConvert+GADAdSize.h */,
5E86A6461F126FCE008013EB /* RCTConvert+GADAdSize.m */,
5EE0EECE1F327DFD0045CC2C /* RCTConvert+GADGender.h */,
5EE0EECF1F327DFD0045CC2C /* RCTConvert+GADGender.m */,
);
path = RNAdMobManager;
sourceTree = "<group>";
Expand Down Expand Up @@ -164,6 +169,7 @@
5E2419841FE11E0F00C6B738 /* RNAdMobUtils.m in Sources */,
5E86A6471F126FCE008013EB /* RCTConvert+GADAdSize.m in Sources */,
0CB8C04E1D9143A6002BC3EF /* RNDFPBannerViewManager.m in Sources */,
5EE0EED01F327DFD0045CC2C /* RCTConvert+GADGender.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
1 change: 1 addition & 0 deletions ios/RNDFPBannerView.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

@property (nonatomic, copy) NSArray *validAdSizes;
@property (nonatomic, copy) NSArray *testDevices;
@property (nonatomic, copy) NSDictionary *targeting;

@property (nonatomic, copy) RCTBubblingEventBlock onSizeChange;
@property (nonatomic, copy) RCTBubblingEventBlock onAppEvent;
Expand Down
46 changes: 45 additions & 1 deletion ios/RNDFPBannerView.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#endif

#include "RCTConvert+GADAdSize.h"
#include "RCTConvert+GADGender.h"

@implementation RNDFPBannerView
{
Expand Down Expand Up @@ -53,8 +54,51 @@ - (void)insertReactSubview:(UIView *)subview atIndex:(NSInteger)atIndex
#pragma clang diagnostic pop

- (void)loadBanner {
GADRequest *request = [GADRequest request];
DFPRequest *request = [DFPRequest request];
request.testDevices = _testDevices;

if (_targeting != nil) {
NSDictionary *customTargeting = [_targeting objectForKey:@"customTargeting"];
if (customTargeting != nil) {
request.customTargeting = customTargeting;
}
NSArray *categoryExclusions = [_targeting objectForKey:@"categoryExclusions"];
if (categoryExclusions != nil) {
request.categoryExclusions = categoryExclusions;
}
NSArray *keywords = [_targeting objectForKey:@"keywords"];
if (keywords != nil) {
request.keywords = keywords;
}
NSString *gender = [_targeting objectForKey:@"gender"];
if (gender != nil) {
request.gender = [RCTConvert GADGender:gender];
}
NSDate *birthday = [_targeting objectForKey:@"birthday"];
if (birthday != nil) {
request.birthday = [RCTConvert NSDate:birthday];
}
id childDirectedTreatment = [_targeting objectForKey:@"childDirectedTreatment"];
if (childDirectedTreatment != nil) {
[request tagForChildDirectedTreatment:childDirectedTreatment];
}
NSString *contentURL = [_targeting objectForKey:@"contentURL"];
if (contentURL != nil) {
request.contentURL = contentURL;
}
NSString *publisherProvidedID = [_targeting objectForKey:@"publisherProvidedID"];
if (publisherProvidedID != nil) {
request.publisherProvidedID = publisherProvidedID;
}
NSDictionary *location = [_targeting objectForKey:@"location"];
if (location != nil) {
CGFloat latitude = [[location objectForKey:@"latitude"] doubleValue];
CGFloat longitude = [[location objectForKey:@"longitude"] doubleValue];
CGFloat accuracy = [[location objectForKey:@"accuracy"] doubleValue];
[request setLocationWithLatitude:latitude longitude:longitude accuracy:accuracy];
}
}

[_bannerView loadRequest:request];
}

Expand Down
1 change: 1 addition & 0 deletions ios/RNDFPBannerViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ - (UIView *)view
RCT_REMAP_VIEW_PROPERTY(adUnitID, _bannerView.adUnitID, NSString)
RCT_EXPORT_VIEW_PROPERTY(validAdSizes, NSArray)
RCT_EXPORT_VIEW_PROPERTY(testDevices, NSArray)
RCT_EXPORT_VIEW_PROPERTY(targeting, NSDictionary)

RCT_EXPORT_VIEW_PROPERTY(onSizeChange, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onAppEvent, RCTBubblingEventBlock)
Expand Down

0 comments on commit 6c5baa2

Please sign in to comment.