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

NonPersonalizedAds - Missing network extra to toggle npa parameter #131

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class AdaptiveBannerAdView extends ReactViewGroup implements AppEventListener, L
String publisherProvidedID;
Location location;
String correlator;
Boolean servePersonalizedAds = true;

int top;
int left;
Expand Down Expand Up @@ -207,6 +208,10 @@ public void loadBanner() {
Bundle bundle = new Bundle();
bundle.putString("correlator", correlator);

if (!servePersonalizedAds) {
bundle.putInt("npa", 1);
}

adRequestBuilder.addNetworkExtrasBundle(AdMobAdapter.class, bundle);

// Targeting
Expand Down Expand Up @@ -308,6 +313,10 @@ public void setCorrelator(String correlator) {
this.correlator = correlator;
}

public void setServePersonalizedAds(Boolean servePersonalizedAds) {
this.servePersonalizedAds = servePersonalizedAds;
}

@Override
public void onAppEvent(String name, String info) {
WritableMap event = Arguments.createMap();
Expand Down
14 changes: 14 additions & 0 deletions android/src/main/java/com/matejdr/admanager/BannerAdView.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class BannerAdView extends ReactViewGroup implements AppEventListener, Lifecycle
Location location;
String correlator;

Boolean servePersonalizedAds = true;

int top;
int left;
int width;
Expand Down Expand Up @@ -283,6 +285,10 @@ public void loadBanner() {
Bundle bundle = new Bundle();
bundle.putString("correlator", correlator);

if (!servePersonalizedAds) {
bundle.putInt("npa", 1);
}

adRequestBuilder.addNetworkExtrasBundle(AdMobAdapter.class, bundle);

// Targeting
Expand Down Expand Up @@ -447,6 +453,14 @@ public void setCorrelator(String correlator) {
}
}

public void setServePersonalizedAds(Boolean servePersonalizedAds) {
try {
this.servePersonalizedAds = servePersonalizedAds;
} catch (Exception e) {
sendErrorEvent("Error found at ad manager when setServePersonalizedAds(): " + e.getMessage() + "!");
}
}

@Override
public void onAppEvent(String name, String info) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public class NativeAdViewContainer extends ReactViewGroup implements AppEventLis
String publisherProvidedID;
Location location;
String correlator;
Boolean servePersonalizedAds = true;
List<String> customClickTemplateIds;

/**
Expand Down Expand Up @@ -267,6 +268,10 @@ public void run() {
Bundle bundle = new Bundle();
bundle.putString("correlator", correlator);

if (!servePersonalizedAds) {
bundle.putInt("npa", 1);
}

adRequestBuilder.addNetworkExtrasBundle(AdMobAdapter.class, bundle);

Bundle fbExtras = new FacebookExtras()
Expand Down Expand Up @@ -313,7 +318,7 @@ public void run() {
}

// setLocation() became obsolete since GMA SDK version 21.0.0, link reference below:
// https://developers.google.com/admob/android/rel-notes
// https://developers.google.com/admob/android/rel-notes
//if (location != null) {
// adRequestBuilder.setLocation(location);
//}
Expand Down Expand Up @@ -639,6 +644,10 @@ public void setCorrelator(String correlator) {
this.correlator = correlator;
}

public void setServePersonalizedAds(Boolean servePersonalizedAds) {
this.servePersonalizedAds = servePersonalizedAds;
}

public void setCustomClickTemplateIds(String[] customClickTemplateIds) {
this.customClickTemplateIds = Arrays.asList(customClickTemplateIds);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class RNAdManagerAdaptiveBannerViewManager extends ViewGroupManager<Adapt
public static final String PROP_TEST_DEVICES = "testDevices";
public static final String PROP_TARGETING = "targeting";
public static final String PROP_CORRELATOR = "correlator";
public static final String PROP_SERVE_PERSONALIZED_ADS = "servePersonalizedAds";

public static final String EVENT_SIZE_CHANGE = "onSizeChange";
public static final String EVENT_AD_LOADED = "onAdLoaded";
Expand Down Expand Up @@ -178,6 +179,11 @@ public void setCorrelator(final AdaptiveBannerAdView view, final String correlat
view.setCorrelator(correlator);
}

@ReactProp(name = PROP_SERVE_PERSONALIZED_ADS)
public void setServePersonalizedAds(final AdaptiveBannerAdView view, final Boolean servePersonalizedAds) {
view.setServePersonalizedAds(servePersonalizedAds);
}

@Nullable
@Override
public Map<String, Integer> getCommandsMap() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class RNAdManagerBannerViewManager extends ViewGroupManager<BannerAdView>
public static final String PROP_TEST_DEVICES = "testDevices";
public static final String PROP_TARGETING = "targeting";
public static final String PROP_CORRELATOR = "correlator";
public static final String PROP_SERVE_PERSONALIZED_ADS = "servePersonalizedAds";

public static final String EVENT_SIZE_CHANGE = "onSizeChange";
public static final String EVENT_AD_LOADED = "onAdLoaded";
Expand Down Expand Up @@ -190,6 +191,11 @@ public void setCorrelator(final BannerAdView view, final String correlator) {
view.setCorrelator(correlator);
}

@ReactProp(name = PROP_SERVE_PERSONALIZED_ADS)
public void setServePersonalizedAds(final BannerAdView view, final Boolean servePersonalizedAds) {
view.setServePersonalizedAds(servePersonalizedAds);
}

@Nullable
@Override
public Map<String, Integer> getCommandsMap() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;

Expand All @@ -19,6 +20,7 @@
import com.facebook.react.bridge.ReadableNativeArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.google.ads.mediation.admob.AdMobAdapter;
import com.google.android.gms.ads.AdError;
import com.google.android.gms.ads.FullScreenContentCallback;
import com.google.android.gms.ads.LoadAdError;
Expand Down Expand Up @@ -60,6 +62,7 @@ public class RNAdManagerInterstitial extends ReactContextBaseJavaModule {
ReactApplicationContext reactContext;

private Promise mRequestAdPromise;
private Boolean servePersonalizedAds = true;

public RNAdManagerInterstitial(ReactApplicationContext reactContext) {
super(reactContext);
Expand Down Expand Up @@ -142,6 +145,11 @@ public void setTargeting(ReadableMap targetingObjects) {
}
}

@ReactMethod
public void setServePersonalizedAds(Boolean servePersonalizedAds) {
this.servePersonalizedAds = servePersonalizedAds;
}

private AdManagerAdRequest buildAdRequest() {
AdManagerAdRequest.Builder adRequestBuilder = new AdManagerAdRequest.Builder();

Expand Down Expand Up @@ -203,6 +211,13 @@ private AdManagerAdRequest buildAdRequest() {
// adRequestBuilder.setLocation(location);
//}

Bundle bundle = new Bundle();
if (!servePersonalizedAds) {
bundle.putInt("npa", 1);
}

adRequestBuilder.addNetworkExtrasBundle(AdMobAdapter.class, bundle);

adRequest = adRequestBuilder.build();

this.adRequest = adRequest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public class RNAdManagerNativeViewManager extends ViewGroupManager<NativeAdViewC
public static final String PROP_VALID_AD_TYPES = "validAdTypes";
public static final String PROP_TARGETING = "targeting";
public static final String PROP_CORRELATOR = "correlator";
public static final String PROP_SERVE_PERSONALIZED_ADS = "servePersonalizedAds";


public static final String EVENT_AD_LOADED = "onAdLoaded";
public static final String EVENT_SIZE_CHANGE = "onSizeChange";
Expand Down Expand Up @@ -222,6 +224,11 @@ public void setPropCustomClickTemplateIds(final NativeAdViewContainer view, fina
view.setCustomClickTemplateIds(customTemplateClickIdsStringArray);
}

@ReactProp(name = PROP_SERVE_PERSONALIZED_ADS)
public void setServePersonalizedAds(final NativeAdViewContainer view, final Boolean servePersonalizedAds) {
view.setServePersonalizedAds(servePersonalizedAds);
}

@Override
public void addView(NativeAdViewContainer parent, View child, int index) {
parent.addView(child, index);
Expand Down
1 change: 1 addition & 0 deletions ios/RNAdManageNativeManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ - (UIView *)view
RCT_EXPORT_VIEW_PROPERTY(validAdSizes, NSArray)
RCT_EXPORT_VIEW_PROPERTY(targeting, NSDictionary)
RCT_EXPORT_VIEW_PROPERTY(correlator, NSString)
RCT_EXPORT_VIEW_PROPERTY(servePersonalizedAds, BOOL)

RCT_EXPORT_VIEW_PROPERTY(onSizeChange, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onAppEvent, RCTBubblingEventBlock)
Expand Down
1 change: 1 addition & 0 deletions ios/RNAdManageNativeView.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
@property (nonatomic, copy) NSArray *validAdSizes;
@property (nonatomic, copy) NSDictionary *targeting;
@property (nonatomic, copy) NSString *correlator;
@property (nonatomic) BOOL servePersonalizedAds;

@property (nonatomic, copy) RCTBubblingEventBlock onSizeChange;
@property (nonatomic, copy) RCTBubblingEventBlock onAppEvent;
Expand Down
21 changes: 17 additions & 4 deletions ios/RNAdManageNativeView.m
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,17 @@ - (void)reloadAd {
if (_correlator == nil) {
_correlator = getCorrelator(_adUnitID);
}
extras.additionalParameters = [[NSDictionary alloc] initWithObjectsAndKeys:
_correlator, @"correlator",
nil];


NSMutableDictionary *additionalParams = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
_correlator, @"correlator",
nil];
if (_servePersonalizedAds == NO) {
[additionalParams setObject:[NSNumber numberWithInt:1] forKey:@"npa"];
}

// Set the dictionary to extras.additionalParameters
extras.additionalParameters = [NSDictionary dictionaryWithDictionary:additionalParams];

[request registerAdNetworkExtras:extras];

if (_targeting != nil) {
Expand Down Expand Up @@ -237,6 +244,12 @@ - (void)setCorrelator:(NSString *)correlator
_correlator = correlator;
}

- (void)setServePersonalizedAds:(BOOL)servePersonalizedAds
{
_servePersonalizedAds = servePersonalizedAds;
}


#pragma mark GADAdLoaderDelegate implementation

/// Tells the delegate an ad request failed.UnifiedNativeAdView
Expand Down
1 change: 1 addition & 0 deletions ios/RNAdManagerAdaptiveBannerView.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
@property (nonatomic, copy) NSNumber *maxHeight;
@property (nonatomic, strong) NSString *adUnitID;
@property (nonatomic, copy) NSString *correlator;
@property (nonatomic) BOOL servePersonalizedAds;

@property (nonatomic, copy) RCTBubblingEventBlock onSizeChange;
@property (nonatomic, copy) RCTBubblingEventBlock onAppEvent;
Expand Down
20 changes: 17 additions & 3 deletions ios/RNAdManagerAdaptiveBannerView.m
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ - (void)setCorrelator:(NSString *)correlator
_correlator = correlator;
}

- (void)setServePersonalizedAds:(BOOL)servePersonalizedAds
{
_servePersonalizedAds = servePersonalizedAds;
}


// Initialise BannerAdView as soon as all the props are set
- (void)createViewIfCan
{
Expand Down Expand Up @@ -110,9 +116,17 @@ - (void)createViewIfCan
if (_correlator == nil) {
_correlator = getCorrelator(_adUnitID);
}
extras.additionalParameters = [[NSDictionary alloc] initWithObjectsAndKeys:
_correlator, @"correlator",
nil];

NSMutableDictionary *additionalParams = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
_correlator, @"correlator",
nil];
if (_servePersonalizedAds == NO) {
[additionalParams setObject:[NSNumber numberWithInt:1] forKey:@"npa"];
}

// Set the dictionary to extras.additionalParameters
extras.additionalParameters = [NSDictionary dictionaryWithDictionary:additionalParams];

[request registerAdNetworkExtras:extras];

if (_targeting != nil) {
Expand Down
1 change: 1 addition & 0 deletions ios/RNAdManagerAdaptiveBannerViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ - (UIView *)view
RCT_EXPORT_VIEW_PROPERTY(correlator, NSString)
RCT_EXPORT_VIEW_PROPERTY(testDevices, NSArray)
RCT_EXPORT_VIEW_PROPERTY(targeting, NSDictionary)
RCT_EXPORT_VIEW_PROPERTY(servePersonalizedAds, BOOL)

RCT_EXPORT_VIEW_PROPERTY(onSizeChange, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onAppEvent, RCTBubblingEventBlock)
Expand Down
1 change: 1 addition & 0 deletions ios/RNAdManagerBannerView.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
@property (nonatomic, copy) NSString *adSize;
@property (nonatomic, strong) NSString *adUnitID;
@property (nonatomic, copy) NSString *correlator;
@property (nonatomic) BOOL servePersonalizedAds;

@property (nonatomic, copy) RCTBubblingEventBlock onSizeChange;
@property (nonatomic, copy) RCTBubblingEventBlock onAppEvent;
Expand Down
19 changes: 16 additions & 3 deletions ios/RNAdManagerBannerView.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ - (void)setCorrelator:(NSString *)correlator
_correlator = correlator;
}

- (void)setServePersonalizedAds:(BOOL)servePersonalizedAds
{
_servePersonalizedAds = servePersonalizedAds;
}

// Initialise BannerAdView as soon as all the props are set
- (void)createViewIfCan
{
Expand Down Expand Up @@ -97,9 +102,17 @@ - (void)createViewIfCan
if (_correlator == nil) {
_correlator = getCorrelator(_adUnitID);
}
extras.additionalParameters = [[NSDictionary alloc] initWithObjectsAndKeys:
_correlator, @"correlator",
nil];

NSMutableDictionary *additionalParams = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
_correlator, @"correlator",
nil];
if (_servePersonalizedAds == NO) {
[additionalParams setObject:[NSNumber numberWithInt:1] forKey:@"npa"];
}

// Set the dictionary to extras.additionalParameters
extras.additionalParameters = [NSDictionary dictionaryWithDictionary:additionalParams];

[request registerAdNetworkExtras:extras];

if (_targeting != nil) {
Expand Down
1 change: 1 addition & 0 deletions ios/RNAdManagerBannerViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ - (UIView *)view
RCT_EXPORT_VIEW_PROPERTY(validAdSizes, NSArray)
RCT_EXPORT_VIEW_PROPERTY(testDevices, NSArray)
RCT_EXPORT_VIEW_PROPERTY(targeting, NSDictionary)
RCT_EXPORT_VIEW_PROPERTY(servePersonalizedAds, BOOL)

RCT_EXPORT_VIEW_PROPERTY(onSizeChange, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onAppEvent, RCTBubblingEventBlock)
Expand Down
14 changes: 14 additions & 0 deletions ios/RNAdManagerInterstitial.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ @implementation RNAdManagerInterstitial
NSString *_adUnitID;
NSArray *_testDevices;
NSDictionary *_targeting;
BOOL _servePersonalizedAds;

RCTPromiseResolveBlock _requestAdResolve;
RCTPromiseRejectBlock _requestAdReject;
Expand Down Expand Up @@ -60,6 +61,11 @@ + (BOOL)requiresMainQueueSetup
_targeting = targeting;
}

RCT_EXPORT_METHOD(setServePersonalizedAds:(BOOL) servePersonalizedAds)
{
_servePersonalizedAds = servePersonalizedAds;
}

RCT_EXPORT_METHOD(requestAd:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
_requestAdResolve = nil;
Expand All @@ -72,6 +78,14 @@ + (BOOL)requiresMainQueueSetup

GADMobileAds.sharedInstance.requestConfiguration.testDeviceIdentifiers = _testDevices;
GAMRequest *request = [GAMRequest request];

if (_servePersonalizedAds == NO) {
GADExtras *extras = [[GADExtras alloc] init];
extras.additionalParameters = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithInt:1], @"npa",
nil];
[request registerAdNetworkExtras:extras];
}

if (_targeting != nil) {
NSDictionary *customTargeting = [_targeting objectForKey:@"customTargeting"];
Expand Down
Loading