-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathnative.js
282 lines (239 loc) · 8.63 KB
/
native.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import { deepAccess, getKeyByValue, insertHtmlIntoIframe, logError, triggerPixel } from './utils.js';
import {includes} from './polyfill.js';
import {auctionManager} from './auctionManager.js';
import CONSTANTS from './constants.json';
export const nativeAdapters = [];
export const NATIVE_TARGETING_KEYS = Object.keys(CONSTANTS.NATIVE_KEYS).map(
key => CONSTANTS.NATIVE_KEYS[key]
);
const IMAGE = {
image: { required: true },
title: { required: true },
sponsoredBy: { required: true },
clickUrl: { required: true },
body: { required: false },
icon: { required: false },
};
const SUPPORTED_TYPES = {
image: IMAGE
};
/**
* Recieves nativeParams from an adUnit. If the params were not of type 'type',
* passes them on directly. If they were of type 'type', translate
* them into the predefined specific asset requests for that type of native ad.
*/
export function processNativeAdUnitParams(params) {
if (params && params.type && typeIsSupported(params.type)) {
return SUPPORTED_TYPES[params.type];
}
return params;
}
export function decorateAdUnitsWithNativeParams(adUnits) {
adUnits.forEach(adUnit => {
const nativeParams =
adUnit.nativeParams || deepAccess(adUnit, 'mediaTypes.native');
if (nativeParams) {
adUnit.nativeParams = processNativeAdUnitParams(nativeParams);
}
});
}
/**
* Check if the native type specified in the adUnit is supported by Prebid.
*/
function typeIsSupported(type) {
if (!(type && includes(Object.keys(SUPPORTED_TYPES), type))) {
logError(`${type} nativeParam is not supported`);
return false;
}
return true;
}
/**
* Helper functions for working with native-enabled adUnits
* TODO: abstract this and the video helper functions into general
* adunit validation helper functions
*/
export const nativeAdUnit = adUnit => {
const mediaType = adUnit.mediaType === 'native';
const mediaTypes = deepAccess(adUnit, 'mediaTypes.native');
return mediaType || mediaTypes;
}
export const nativeBidder = bid => includes(nativeAdapters, bid.bidder);
export const hasNonNativeBidder = adUnit =>
adUnit.bids.filter(bid => !nativeBidder(bid)).length;
/**
* Validate that the native assets on this bid contain all assets that were
* marked as required in the adUnit configuration.
* @param {Bid} bid Native bid to validate
* @param {BidRequest[]} bidRequests All bid requests for an auction
* @return {Boolean} If object is valid
*/
export function nativeBidIsValid(bid, {index = auctionManager.index} = {}) {
// all native bid responses must define a landing page url
if (!deepAccess(bid, 'native.clickUrl')) {
return false;
}
const requestedAssets = index.getAdUnit(bid).nativeParams;
if (!requestedAssets) {
return true;
}
const requiredAssets = Object.keys(requestedAssets).filter(
key => requestedAssets[key].required
);
const returnedAssets = Object.keys(bid['native']).filter(
key => bid['native'][key]
);
return requiredAssets.every(asset => includes(returnedAssets, asset));
}
/*
* Native responses may have associated impression or click trackers.
* This retrieves the appropriate tracker urls for the given ad object and
* fires them. As a native creatives may be in a cross-origin frame, it may be
* necessary to invoke this function via postMessage. secureCreatives is
* configured to fire this function when it receives a `message` of 'Prebid Native'
* and an `adId` with the value of the `bid.adId`. When a message is posted with
* these parameters, impression trackers are fired. To fire click trackers, the
* message should contain an `action` set to 'click'.
*
* // Native creative template example usage
* <a href="%%CLICK_URL_UNESC%%%%PATTERN:hb_native_linkurl%%"
* target="_blank"
* onclick="fireTrackers('click')">
* %%PATTERN:hb_native_title%%
* </a>
*
* <script>
* function fireTrackers(action) {
* var message = {message: 'Prebid Native', adId: '%%PATTERN:hb_adid%%'};
* if (action === 'click') {message.action = 'click';} // fires click trackers
* window.parent.postMessage(JSON.stringify(message), '*');
* }
* fireTrackers(); // fires impressions when creative is loaded
* </script>
*/
export function fireNativeTrackers(message, adObject) {
let trackers;
if (message.action === 'click') {
trackers = adObject['native'] && adObject['native'].clickTrackers;
} else {
trackers = adObject['native'] && adObject['native'].impressionTrackers;
if (adObject['native'] && adObject['native'].javascriptTrackers) {
insertHtmlIntoIframe(adObject['native'].javascriptTrackers);
}
}
(trackers || []).forEach(triggerPixel);
return message.action;
}
/**
* Gets native targeting key-value pairs
* @param {Object} bid
* @return {Object} targeting
*/
export function getNativeTargeting(bid, {index = auctionManager.index} = {}) {
let keyValues = {};
const adUnit = index.getAdUnit(bid);
if (deepAccess(adUnit, 'nativeParams.rendererUrl')) {
bid['native']['rendererUrl'] = getAssetValue(adUnit.nativeParams['rendererUrl']);
} else if (deepAccess(adUnit, 'nativeParams.adTemplate')) {
bid['native']['adTemplate'] = getAssetValue(adUnit.nativeParams['adTemplate']);
}
const globalSendTargetingKeys = deepAccess(
adUnit,
`nativeParams.sendTargetingKeys`
) !== false;
const nativeKeys = getNativeKeys(adUnit);
const flatBidNativeKeys = { ...bid.native, ...bid.native.ext };
delete flatBidNativeKeys.ext;
Object.keys(flatBidNativeKeys).forEach(asset => {
const key = nativeKeys[asset];
let value = getAssetValue(bid.native[asset]) || getAssetValue(deepAccess(bid, `native.ext.${asset}`));
if (asset === 'adTemplate' || !key || !value) {
return;
}
let sendPlaceholder = deepAccess(adUnit, `nativeParams.${asset}.sendId`);
if (typeof sendPlaceholder !== 'boolean') {
sendPlaceholder = deepAccess(adUnit, `nativeParams.ext.${asset}.sendId`);
}
if (sendPlaceholder) {
const placeholder = `${key}:${bid.adId}`;
value = placeholder;
}
let assetSendTargetingKeys = deepAccess(adUnit, `nativeParams.${asset}.sendTargetingKeys`)
if (typeof assetSendTargetingKeys !== 'boolean') {
assetSendTargetingKeys = deepAccess(adUnit, `nativeParams.ext.${asset}.sendTargetingKeys`);
}
const sendTargeting = typeof assetSendTargetingKeys === 'boolean' ? assetSendTargetingKeys : globalSendTargetingKeys;
if (sendTargeting) {
keyValues[key] = value;
}
});
return keyValues;
}
/**
* Constructs a message object containing asset values for each of the
* requested data keys.
*/
export function getAssetMessage(data, adObject) {
const message = {
message: 'assetResponse',
adId: data.adId,
assets: [],
};
if (adObject.native.hasOwnProperty('adTemplate')) {
message.adTemplate = getAssetValue(adObject.native['adTemplate']);
} if (adObject.native.hasOwnProperty('rendererUrl')) {
message.rendererUrl = getAssetValue(adObject.native['rendererUrl']);
}
data.assets.forEach(asset => {
const key = getKeyByValue(CONSTANTS.NATIVE_KEYS, asset);
const value = getAssetValue(adObject.native[key]);
message.assets.push({ key, value });
});
return message;
}
export function getAllAssetsMessage(data, adObject) {
const message = {
message: 'assetResponse',
adId: data.adId,
assets: []
};
Object.keys(adObject.native).forEach(function(key, index) {
if (key === 'adTemplate' && adObject.native[key]) {
message.adTemplate = getAssetValue(adObject.native[key]);
} else if (key === 'rendererUrl' && adObject.native[key]) {
message.rendererUrl = getAssetValue(adObject.native[key]);
} else if (key === 'ext') {
Object.keys(adObject.native[key]).forEach(extKey => {
if (adObject.native[key][extKey]) {
const value = getAssetValue(adObject.native[key][extKey]);
message.assets.push({ key: extKey, value });
}
})
} else if (adObject.native[key] && CONSTANTS.NATIVE_KEYS.hasOwnProperty(key)) {
const value = getAssetValue(adObject.native[key]);
message.assets.push({ key, value });
}
});
return message;
}
/**
* Native assets can be a string or an object with a url prop. Returns the value
* appropriate for sending in adserver targeting or placeholder replacement.
*/
function getAssetValue(value) {
if (typeof value === 'object' && value.url) {
return value.url;
}
return value;
}
function getNativeKeys(adUnit) {
const extraNativeKeys = {}
if (deepAccess(adUnit, 'nativeParams.ext')) {
Object.keys(adUnit.nativeParams.ext).forEach(extKey => {
extraNativeKeys[extKey] = `hb_native_${extKey}`;
})
}
return {
...CONSTANTS.NATIVE_KEYS,
...extraNativeKeys
}
}