diff --git a/android/src/main/java/com/matejdr/admanager/AdaptiveBannerAdView.java b/android/src/main/java/com/matejdr/admanager/AdaptiveBannerAdView.java index 3929753..8ebbffa 100644 --- a/android/src/main/java/com/matejdr/admanager/AdaptiveBannerAdView.java +++ b/android/src/main/java/com/matejdr/admanager/AdaptiveBannerAdView.java @@ -48,6 +48,7 @@ class AdaptiveBannerAdView extends ReactViewGroup implements AppEventListener, L String publisherProvidedID; Location location; String correlator; + Boolean servePersonalizedAds = true; int top; int left; @@ -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 @@ -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(); diff --git a/android/src/main/java/com/matejdr/admanager/BannerAdView.java b/android/src/main/java/com/matejdr/admanager/BannerAdView.java index 348a7b6..4467dc9 100644 --- a/android/src/main/java/com/matejdr/admanager/BannerAdView.java +++ b/android/src/main/java/com/matejdr/admanager/BannerAdView.java @@ -50,6 +50,8 @@ class BannerAdView extends ReactViewGroup implements AppEventListener, Lifecycle Location location; String correlator; + Boolean servePersonalizedAds = true; + int top; int left; int width; @@ -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 @@ -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 { diff --git a/android/src/main/java/com/matejdr/admanager/NativeAdViewContainer.java b/android/src/main/java/com/matejdr/admanager/NativeAdViewContainer.java index fc99289..50e532d 100644 --- a/android/src/main/java/com/matejdr/admanager/NativeAdViewContainer.java +++ b/android/src/main/java/com/matejdr/admanager/NativeAdViewContainer.java @@ -76,6 +76,7 @@ public class NativeAdViewContainer extends ReactViewGroup implements AppEventLis String publisherProvidedID; Location location; String correlator; + Boolean servePersonalizedAds = true; List customClickTemplateIds; /** @@ -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() @@ -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); //} @@ -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); } diff --git a/android/src/main/java/com/matejdr/admanager/RNAdManagerAdaptiveBannerViewManager.java b/android/src/main/java/com/matejdr/admanager/RNAdManagerAdaptiveBannerViewManager.java index 3bbf879..2750c06 100644 --- a/android/src/main/java/com/matejdr/admanager/RNAdManagerAdaptiveBannerViewManager.java +++ b/android/src/main/java/com/matejdr/admanager/RNAdManagerAdaptiveBannerViewManager.java @@ -32,6 +32,7 @@ public class RNAdManagerAdaptiveBannerViewManager extends ViewGroupManager getCommandsMap() { diff --git a/android/src/main/java/com/matejdr/admanager/RNAdManagerBannerViewManager.java b/android/src/main/java/com/matejdr/admanager/RNAdManagerBannerViewManager.java index ebe4d31..7722a93 100644 --- a/android/src/main/java/com/matejdr/admanager/RNAdManagerBannerViewManager.java +++ b/android/src/main/java/com/matejdr/admanager/RNAdManagerBannerViewManager.java @@ -34,6 +34,7 @@ public class RNAdManagerBannerViewManager extends ViewGroupManager 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"; @@ -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 getCommandsMap() { diff --git a/android/src/main/java/com/matejdr/admanager/RNAdManagerInterstitial.java b/android/src/main/java/com/matejdr/admanager/RNAdManagerInterstitial.java index 5cbf131..ee3553b 100644 --- a/android/src/main/java/com/matejdr/admanager/RNAdManagerInterstitial.java +++ b/android/src/main/java/com/matejdr/admanager/RNAdManagerInterstitial.java @@ -2,6 +2,7 @@ import android.app.Activity; import android.location.Location; +import android.os.Bundle; import android.os.Handler; import android.os.Looper; @@ -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; @@ -60,6 +62,7 @@ public class RNAdManagerInterstitial extends ReactContextBaseJavaModule { ReactApplicationContext reactContext; private Promise mRequestAdPromise; + private Boolean servePersonalizedAds = true; public RNAdManagerInterstitial(ReactApplicationContext reactContext) { super(reactContext); @@ -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(); @@ -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; diff --git a/android/src/main/java/com/matejdr/admanager/RNAdManagerNativeViewManager.java b/android/src/main/java/com/matejdr/admanager/RNAdManagerNativeViewManager.java index 334d36c..ea91528 100644 --- a/android/src/main/java/com/matejdr/admanager/RNAdManagerNativeViewManager.java +++ b/android/src/main/java/com/matejdr/admanager/RNAdManagerNativeViewManager.java @@ -36,6 +36,8 @@ public class RNAdManagerNativeViewManager extends ViewGroupManager void; /** @@ -92,7 +94,9 @@ interface IAdManagerBannerNativeProps extends IAdManagerBannerPropsBase { onAppEvent?: (event: NativeSyntheticEvent) => void; onAdOpened?: (event: NativeSyntheticEvent) => void; onAdClosed?: (event: NativeSyntheticEvent) => void; - onAdRecordImpression?: (event: NativeSyntheticEvent) => void; + onAdRecordImpression?: ( + event: NativeSyntheticEvent + ) => void; } const ComponentName = 'CTKBannerView'; @@ -113,7 +117,10 @@ export class Banner extends React.Component< constructor(props: IAdManagerBannerProps) { super(props); - this.hasOnErrorFromParent = Object.prototype.hasOwnProperty.call(props, 'onError'); + this.hasOnErrorFromParent = Object.prototype.hasOwnProperty.call( + props, + 'onError' + ); this.handleSizeChange = this.handleSizeChange.bind(this); this.state = { style: {}, @@ -137,21 +144,24 @@ export class Banner extends React.Component< } componentDidMount() { - this.customListener= DeviceEventEmitter.addListener('onError',eventData=>{ - this.setState({ error: eventData }); - if (this.hasOnErrorFromParent && this.props.onError) { - this.props.onError(eventData); + this.customListener = DeviceEventEmitter.addListener( + 'onError', + (eventData) => { + this.setState({ error: eventData }); + if (this.hasOnErrorFromParent && this.props.onError) { + this.props.onError(eventData); + } } - }); + ); this.loadBanner(); } - + componentWillUnmount() { if (this.customListener) { this.customListener.remove(); } } - + loadBanner() { UIManager.dispatchViewManagerCommand( findNodeHandle(this), @@ -191,7 +201,8 @@ export class Banner extends React.Component< this.props.onAdClosed && this.props.onAdClosed(event.nativeEvent) } onAdRecordImpression={(event) => - this.props.onAdRecordImpression && this.props.onAdRecordImpression(event.nativeEvent) + this.props.onAdRecordImpression && + this.props.onAdRecordImpression(event.nativeEvent) } /> ); diff --git a/src/CTKAdManagerInterstitial.ts b/src/CTKAdManagerInterstitial.ts index b2ab9ac..ec4490a 100644 --- a/src/CTKAdManagerInterstitial.ts +++ b/src/CTKAdManagerInterstitial.ts @@ -101,17 +101,21 @@ const setTargeting = (targeting: IAdManagerTargeting) => { CTKInterstitial.setTargeting(targeting); }; +const setServePersonalizedAds = (servePersonalizedAds: boolean) => { + CTKInterstitial.setServePersonalizedAds(servePersonalizedAds); +}; + const requestAd = (): Promise => { return CTKInterstitial.requestAd(); -} +}; const showAd = (): Promise => { return CTKInterstitial.showAd(); -} +}; const isReady = (callback: (isReady: boolean) => void): Promise => { return CTKInterstitial.isReady(callback); -} +}; export default { addEventListener, @@ -123,5 +127,6 @@ export default { setTargeting, requestAd, showAd, - isReady -} + setServePersonalizedAds, + isReady, +}; diff --git a/src/native-ads/withNativeAd.tsx b/src/native-ads/withNativeAd.tsx index fef346b..544f701 100644 --- a/src/native-ads/withNativeAd.tsx +++ b/src/native-ads/withNativeAd.tsx @@ -37,6 +37,7 @@ interface INativeAdPropsBase extends ViewProps { validAdTypes?: ('banner' | 'native' | 'template')[]; customClickTemplateIds?: string[]; targeting?: IAdManagerTargeting; + servePersonalizedAds?: boolean; } interface INativeAdNativeProps extends INativeAdPropsBase { @@ -60,7 +61,9 @@ interface INativeAdNativeProps extends INativeAdPropsBase { onAdCustomClick?: ( event: NativeSyntheticEvent ) => void; - onAdRecordImpression?: (event: NativeSyntheticEvent) => void; + onAdRecordImpression?: ( + event: NativeSyntheticEvent + ) => void; } interface INativeAdProps extends INativeAdPropsBase { @@ -276,11 +279,13 @@ export default (Component: JSXElementConstructor) => this.props.onAdCustomClick(event.nativeEvent) } onAdRecordImpression={(event) => - this.props.onAdRecordImpression && this.props.onAdRecordImpression(event.nativeEvent) + this.props.onAdRecordImpression && + this.props.onAdRecordImpression(event.nativeEvent) } targeting={this.props.targeting} customClickTemplateIds={this.props.customClickTemplateIds} adsManager={adsManager.toJSON()} + servePersonalizedAds={this.props.servePersonalizedAds} > {this.renderAdComponent(rest)}