-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
openxOrtbBidAdapter.js
349 lines (314 loc) · 10.1 KB
/
openxOrtbBidAdapter.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import {config} from '../src/config.js';
import {registerBidder} from '../src/adapters/bidderFactory.js';
import * as utils from '../src/utils.js';
import {BANNER, VIDEO} from '../src/mediaTypes.js';
import {includes} from '../src/polyfill.js';
const bidderConfig = 'hb_pb_ortb';
const bidderVersion = '1.0';
const VIDEO_TARGETING = ['startdelay', 'mimes', 'minduration', 'maxduration', 'delivery',
'startdelay', 'skip', 'playbackmethod', 'api', 'protocol', 'boxingallowed', 'maxextended',
'linearity', 'delivery', 'protocols', 'placement', 'minbitrate', 'maxbitrate', 'battr', 'ext'];
export const REQUEST_URL = 'https://rtb.openx.net/openrtbb/prebidjs';
export const SYNC_URL = 'https://u.openx.net/w/1.0/pd';
export const DEFAULT_PH = '2d1251ae-7f3a-47cf-bd2a-2f288854a0ba';
export const spec = {
code: 'openx',
supportedMediaTypes: [BANNER, VIDEO],
isBidRequestValid,
buildRequests,
interpretResponse,
getUserSyncs,
transformBidParams
};
registerBidder(spec);
function transformBidParams(params, isOpenRtb) {
return utils.convertTypes({
'unit': 'string',
'customFloor': 'number'
}, params);
}
function isBidRequestValid(bidRequest) {
const hasDelDomainOrPlatform = bidRequest.params.delDomain ||
bidRequest.params.platform;
if (utils.deepAccess(bidRequest, 'mediaTypes.banner') &&
hasDelDomainOrPlatform) {
return !!bidRequest.params.unit ||
utils.deepAccess(bidRequest, 'mediaTypes.banner.sizes.length') > 0;
}
return !!(bidRequest.params.unit && hasDelDomainOrPlatform);
}
function buildRequests(bids, bidderRequest) {
let videoBids = bids.filter(bid => isVideoBid(bid));
let bannerBids = bids.filter(bid => isBannerBid(bid));
let requests = bannerBids.length ? [createBannerRequest(bannerBids, bidderRequest)] : [];
videoBids.forEach(bid => {
requests.push(createVideoRequest(bid, bidderRequest));
});
return requests;
}
function createBannerRequest(bids, bidderRequest) {
let data = getBaseRequest(bids[0], bidderRequest);
data.imp = bids.map(bid => {
const floor = getFloor(bid, BANNER);
let imp = {
id: bid.bidId,
tagid: bid.params.unit,
banner: {
format: toFormat(bid.mediaTypes.banner.sizes),
topframe: utils.inIframe() ? 0 : 1
},
ext: {divid: bid.adUnitCode}
};
enrichImp(imp, bid, floor);
return imp;
});
return {
method: 'POST',
url: REQUEST_URL,
data: data
}
}
function toFormat(sizes) {
return sizes.map((s) => {
return { w: s[0], h: s[1] };
});
}
function enrichImp(imp, bid, floor) {
if (bid.params.customParams) {
utils.deepSetValue(imp, 'ext.customParams', bid.params.customParams);
}
if (floor > 0) {
imp.bidfloor = floor;
imp.bidfloorcur = 'USD';
} else if (bid.params.customFloor) {
imp.bidfloor = bid.params.customFloor;
}
if (bid.ortb2Imp && bid.ortb2Imp.ext && bid.ortb2Imp.ext.data) {
imp.ext.data = bid.ortb2Imp.ext.data;
}
}
function createVideoRequest(bid, bidderRequest) {
let width;
let height;
const videoMediaType = utils.deepAccess(bid, `mediaTypes.video`);
const playerSize = utils.deepAccess(bid, 'mediaTypes.video.playerSize');
const context = utils.deepAccess(bid, 'mediaTypes.video.context');
const floor = getFloor(bid, VIDEO);
// normalize config for video size
if (utils.isArray(bid.sizes) && bid.sizes.length === 2 && !utils.isArray(bid.sizes[0])) {
width = parseInt(bid.sizes[0], 10);
height = parseInt(bid.sizes[1], 10);
} else if (utils.isArray(bid.sizes) && utils.isArray(bid.sizes[0]) && bid.sizes[0].length === 2) {
width = parseInt(bid.sizes[0][0], 10);
height = parseInt(bid.sizes[0][1], 10);
} else if (utils.isArray(playerSize) && playerSize.length === 2) {
width = parseInt(playerSize[0], 10);
height = parseInt(playerSize[1], 10);
}
let data = getBaseRequest(bid, bidderRequest);
data.imp = [{
id: bid.bidId,
tagid: bid.params.unit,
video: {
w: width,
h: height,
topframe: utils.inIframe() ? 0 : 1
},
ext: {divid: bid.adUnitCode}
}];
enrichImp(data.imp[0], bid, floor);
if (context) {
if (context === 'instream') {
data.imp[0].video.placement = 1;
} else if (context === 'outstream') {
data.imp[0].video.placement = 4;
}
}
// backward compatability for video params
let videoParams = bid.params.video || bid.params.openrtb || {};
if (utils.isArray(videoParams.imp)) {
videoParams = videoParams[0].video;
}
Object.keys(videoParams)
.filter(param => includes(VIDEO_TARGETING, param))
.forEach(param => data.imp[0].video[param] = videoParams[param]);
Object.keys(videoMediaType)
.filter(param => includes(VIDEO_TARGETING, param))
.forEach(param => data.imp[0].video[param] = videoMediaType[param]);
return {
method: 'POST',
url: REQUEST_URL,
data: data
}
}
function getBaseRequest(bid, bidderRequest) {
let req = {
id: bidderRequest.auctionId,
cur: [config.getConfig('currency.adServerCurrency') || 'USD'],
at: 1,
tmax: config.getConfig('bidderTimeout'),
site: {
page: config.getConfig('pageUrl') || bidderRequest.refererInfo.referer
},
regs: {
coppa: (config.getConfig('coppa') === true || bid.params.coppa) ? 1 : 0,
},
device: {
dnt: (utils.getDNT() || bid.params.doNotTrack) ? 1 : 0,
h: screen.height,
w: screen.width,
ua: window.navigator.userAgent,
language: window.navigator.language.split('-').shift()
},
ext: {
bc: `${bidderConfig}_${bidderVersion}`
}
};
if (bid.params.platform) {
utils.deepSetValue(req, 'ext.platform', bid.params.platform);
}
if (bid.params.delDomain) {
utils.deepSetValue(req, 'ext.delDomain', bid.params.delDomain);
}
if (bid.params.test) {
req.test = 1
}
if (bidderRequest.gdprConsent) {
if (bidderRequest.gdprConsent.gdprApplies !== undefined) {
utils.deepSetValue(req, 'regs.ext.gdpr', bidderRequest.gdprConsent.gdprApplies === true ? 1 : 0);
}
if (bidderRequest.gdprConsent.consentString !== undefined) {
utils.deepSetValue(req, 'user.ext.consent', bidderRequest.gdprConsent.consentString);
}
if (bidderRequest.gdprConsent.addtlConsent !== undefined) {
utils.deepSetValue(req, 'user.ext.ConsentedProvidersSettings.consented_providers', bidderRequest.gdprConsent.addtlConsent);
}
}
if (bidderRequest.uspConsent) {
utils.deepSetValue(req, 'regs.ext.us_privacy', bidderRequest.uspConsent);
}
if (bid.schain) {
utils.deepSetValue(req, 'source.ext.schain', bid.schain);
}
if (bid.userIdAsEids) {
utils.deepSetValue(req, 'user.ext.eids', bid.userIdAsEids);
}
const commonFpd = bidderRequest.ortb2 || {};
if (commonFpd.site) {
utils.mergeDeep(req, {site: commonFpd.site});
}
if (commonFpd.user) {
utils.mergeDeep(req, {user: commonFpd.user});
}
return req;
}
function isVideoBid(bid) {
return utils.deepAccess(bid, 'mediaTypes.video');
}
function isBannerBid(bid) {
return utils.deepAccess(bid, 'mediaTypes.banner') || !isVideoBid(bid);
}
function getFloor(bid, mediaType) {
let floor = 0;
if (typeof bid.getFloor === 'function') {
const floorInfo = bid.getFloor({
currency: 'USD',
mediaType: mediaType,
size: '*'
});
if (typeof floorInfo === 'object' &&
floorInfo.currency === 'USD' &&
!isNaN(parseFloat(floorInfo.floor))) {
floor = Math.max(floor, parseFloat(floorInfo.floor));
}
}
return floor;
}
function interpretResponse(resp, req) {
// pass these from request to the responses for use in userSync
if (req.data.ext) {
if (req.data.ext.delDomain) {
utils.deepSetValue(resp, 'body.ext.delDomain', req.data.ext.delDomain);
}
if (req.data.ext.platform) {
utils.deepSetValue(resp, 'body.ext.platform', req.data.ext.platform);
}
}
const respBody = resp.body;
if ('nbr' in respBody) {
return [];
}
let bids = [];
respBody.seatbid.forEach(seatbid => {
bids = [...bids, ...seatbid.bid.map(bid => {
let response = {
requestId: bid.impid,
cpm: bid.price,
width: bid.w,
height: bid.h,
creativeId: bid.crid,
dealId: bid.dealid,
currency: respBody.cur || 'USD',
netRevenue: true,
ttl: 300,
mediaType: 'banner' in req.data.imp[0] ? BANNER : VIDEO,
meta: { advertiserDomains: bid.adomain }
};
if (response.mediaType === VIDEO) {
if (bid.nurl) {
response.vastUrl = bid.nurl;
} else {
response.vastXml = bid.adm;
}
} else {
response.ad = bid.adm;
}
if (bid.ext) {
response.meta.networkId = bid.ext.dsp_id;
response.meta.advertiserId = bid.ext.buyer_id;
response.meta.brandId = bid.ext.brand_id;
}
if (respBody.ext && respBody.ext.paf) {
response.meta.paf = respBody.ext.paf;
response.meta.paf.content_id = utils.deepAccess(bid, 'ext.paf.content_id');
}
return response
})];
});
return bids;
}
/**
* @param syncOptions
* @param responses
* @param gdprConsent
* @param uspConsent
* @return {{type: (string), url: (*|string)}[]}
*/
function getUserSyncs(syncOptions, responses, gdprConsent, uspConsent) {
if (syncOptions.iframeEnabled || syncOptions.pixelEnabled) {
let pixelType = syncOptions.iframeEnabled ? 'iframe' : 'image';
let queryParamStrings = [];
let syncUrl = SYNC_URL;
if (gdprConsent) {
queryParamStrings.push('gdpr=' + (gdprConsent.gdprApplies ? 1 : 0));
queryParamStrings.push('gdpr_consent=' + encodeURIComponent(gdprConsent.consentString || ''));
}
if (uspConsent) {
queryParamStrings.push('us_privacy=' + encodeURIComponent(uspConsent));
}
if (responses.length > 0 && responses[0].body && responses[0].body.ext) {
const ext = responses[0].body.ext;
if (ext.delDomain) {
syncUrl = `https://${ext.delDomain}/w/1.0/pd`
} else if (ext.platform) {
queryParamStrings.push('ph=' + ext.platform)
}
} else {
queryParamStrings.push('ph=' + DEFAULT_PH)
}
return [{
type: pixelType,
url: `${syncUrl}${queryParamStrings.length > 0 ? '?' + queryParamStrings.join('&') : ''}`
}];
}
}