Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Targeting #266

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export default class Example extends Component {
<BannerExample title="DFP - Multiple Ad Sizes">
<PublisherBanner
adSize="banner"
targeting="key:valeur|key2:valeur2"
validAdSizes={['banner', 'largeBanner', 'mediumRectangle']}
adUnitID="/6499/example/APIDemo/AdSizes"
ref={el => (this._adSizesExample = el)}
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import {
<PublisherBanner
adSize="fullBanner"
adUnitID="your-admob-unit-id"
targeting="key:valeur|key1:valeur2|key2:[valeur3, valeur4]"
testDevices={[PublisherBanner.simulatorId]}
onAdFailedToLoad={error => console.error(error)}
onAppEvent={event => console.log(event.name, event.info)}
Expand Down
5 changes: 5 additions & 0 deletions RNPublisherBanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ PublisherBanner.propTypes = {
*/
adUnitID: string,

/**
* DFP targeting
*/
targeting: string,

/**
* Array of test devices. Use PublisherBanner.simulatorId for the simulator
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class ReactPublisherAdView extends ReactViewGroup implements AppEventListener {
protected PublisherAdView adView;

String[] testDevices;
String targetString = null;
AdSize[] validAdSizes;
String adUnitID;
AdSize adSize;
Expand Down Expand Up @@ -146,6 +147,18 @@ public void loadBanner() {
this.adView.setAdSizes(adSizesArray);

PublisherAdRequest.Builder adRequestBuilder = new PublisherAdRequest.Builder();
if (targetString != null) {
String[] parts = targetString.split("\\|");
for (String s : parts) {
if (s.contains(":")) {
if (s.split(":").length > 1) {
if (s.split(":")[0] != "" && s.split(":")[1] != "") {
adRequestBuilder.addCustomTargeting(s.split(":")[0], s.split(":")[1]);
}
}
}
}
}
if (testDevices != null) {
for (int i = 0; i < testDevices.length; i++) {
adRequestBuilder.addTestDevice(testDevices[i]);
Expand Down Expand Up @@ -173,6 +186,10 @@ public void setAdSize(AdSize adSize) {
this.adSize = adSize;
}

public void setTargeting(String targeting) {
this.targetString = targeting;
}

public void setValidAdSizes(AdSize[] adSizes) {
this.validAdSizes = adSizes;
}
Expand All @@ -193,6 +210,7 @@ public class RNPublisherBannerViewManager extends ViewGroupManager<ReactPublishe
public static final String PROP_AD_SIZE = "adSize";
public static final String PROP_VALID_AD_SIZES = "validAdSizes";
public static final String PROP_AD_UNIT_ID = "adUnitID";
public static final String PROP_BANNER_TARGETING = "targeting";
public static final String PROP_TEST_DEVICES = "testDevices";

public static final String EVENT_SIZE_CHANGE = "onSizeChange";
Expand Down Expand Up @@ -246,6 +264,11 @@ public void setPropAdSize(final ReactPublisherAdView view, final String sizeStri
view.setAdSize(adSize);
}

@ReactProp(name = PROP_BANNER_TARGETING)
public void setTargeting(final ReactPublisherAdView view, final String targetingString) {
view.setTargeting(targetingString);
}

@ReactProp(name = PROP_VALID_AD_SIZES)
public void setPropValidAdSizes(final ReactPublisherAdView view, final ReadableArray adSizeStrings) {
ReadableNativeArray nativeArray = (ReadableNativeArray)adSizeStrings;
Expand Down Expand Up @@ -290,6 +313,8 @@ private AdSize getAdSizeFromString(String adSize) {
return AdSize.SMART_BANNER;
case "smartBanner":
return AdSize.SMART_BANNER;
case "fluid":
return AdSize.FLUID;
default:
return AdSize.BANNER;
}
Expand Down
1 change: 1 addition & 0 deletions ios/RNDFPBannerView.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
@interface RNDFPBannerView : RCTView <GADBannerViewDelegate, GADAdSizeDelegate, GADAppEventDelegate>

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

@property (nonatomic, copy) RCTBubblingEventBlock onSizeChange;
Expand Down
22 changes: 21 additions & 1 deletion ios/RNDFPBannerView.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,27 @@ - (void)insertReactSubview:(UIView *)subview atIndex:(NSInteger)atIndex
#pragma clang diagnostic pop

- (void)loadBanner {
GADRequest *request = [GADRequest request];
DFPRequest *request = [DFPRequest request];
if ([_targeting length] > 0) {
NSArray *array = [_targeting componentsSeparatedByString:@"|"];
NSMutableDictionary *customtargeting = [[NSMutableDictionary alloc] initWithCapacity:[array count]];

for (int i = 0 ; i < [array count] ; i++) {
id objet = [array objectAtIndex:i];
if ([objet length] > 0 && objet) {
NSArray *array2 = [objet componentsSeparatedByString:@":"];
if ([array2 count]>1) {
NSString *key = [array2 objectAtIndex:0];
NSString *valeur = [array2 objectAtIndex:1];
if (valeur && ![valeur isEqual:@"[]"]) {
[customtargeting setObject:valeur forKey: key];
}
}
}
}

request.customTargeting = customtargeting;
}
request.testDevices = _testDevices;
[_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 @@ -40,6 +40,7 @@ - (dispatch_queue_t)methodQueue
RCT_REMAP_VIEW_PROPERTY(adSize, _bannerView.adSize, GADAdSize)
RCT_REMAP_VIEW_PROPERTY(adUnitID, _bannerView.adUnitID, NSString)
RCT_EXPORT_VIEW_PROPERTY(validAdSizes, NSArray)
RCT_EXPORT_VIEW_PROPERTY(targeting, NSString)
RCT_EXPORT_VIEW_PROPERTY(testDevices, NSArray)

RCT_EXPORT_VIEW_PROPERTY(onSizeChange, RCTBubblingEventBlock)
Expand Down