Skip to content
This repository has been archived by the owner on Apr 27, 2022. It is now read-only.

Commit

Permalink
feat: add targets in requestOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
magrinj committed Nov 4, 2021
1 parent 106ff45 commit ea5a9a3
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
10 changes: 10 additions & 0 deletions android/src/main/java/com/rnadmob/admob/RNAdMobCommon.java
Expand Up @@ -120,6 +120,16 @@ static public AdManagerAdRequest buildAdRequest(ReadableMap requestOptions) {
builder.setLocation(location);
}

if (requestOptions.hasKey("targets")) {
Map<String, Object> networkExtras = Objects.requireNonNull(requestOptions.getMap("targets")).toHashMap();

for (Map.Entry<String, Object> entry : networkExtras.entrySet()) {
String key = entry.getKey();
String value = (String) entry.getValue();
builder.addCustomTargeting(key, value);
}
}

return builder.build();
}
}
10 changes: 9 additions & 1 deletion docs/docs/api/RequestOptions.md
Expand Up @@ -54,4 +54,12 @@ Content URL for targeting purposes. Max length of 512.

| Type |
| :----- |
| string |
| string |

### `targets`

Custom key-value pairs to target Google Ad Manager campaigns.

| Type |
| :------------------------ |
| { [key: string]: string } |
10 changes: 10 additions & 0 deletions ios/RNAdMobCommon.m
Expand Up @@ -81,6 +81,7 @@ + (NSArray *)stringsToValues:(NSArray *)strings {
+ (GAMRequest *)buildAdRequest:(NSDictionary *)requestOptions {
GAMRequest *request = [GAMRequest request];
NSMutableDictionary *extras = [@{} mutableCopy];
NSMutableDictionary *targets = [@{} mutableCopy];

if (requestOptions[@"requestNonPersonalizedAdsOnly"] && [requestOptions[@"requestNonPersonalizedAdsOnly"] boolValue]) {
extras[@"npa"] = @"1";
Expand Down Expand Up @@ -110,6 +111,15 @@ + (GAMRequest *)buildAdRequest:(NSDictionary *)requestOptions {
request.contentURL = requestOptions[@"contentUrl"];
}

if (requestOptions[@"targets"]) {
for (NSString *key in requestOptions[@"targets"]) {
NSString *value = requestOptions[@"targets"][key];
targets[key] = value;
}
}

request.customTargeting = targets;

return request;
}

Expand Down
18 changes: 17 additions & 1 deletion src/types.ts
Expand Up @@ -102,7 +102,23 @@ export type RequestOptions = {
* @ios
*/
locationAccuracy?: number;
};

/**
* Custom key-value pairs to target Google Ad Manager campaigns.
*
* Takes an object.
*
* #### Example
*
* ```js
* await interstitialAd.requestAd({
* targets: {
* age: '25',
* },
* });
*/
targets?: { [key: string]: string };
}

export type InitializationStatus = {
name: string;
Expand Down

0 comments on commit ea5a9a3

Please sign in to comment.