-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
adrelevantisBidAdapter.js
615 lines (546 loc) · 17.8 KB
/
adrelevantisBidAdapter.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
import {Renderer} from '../src/Renderer.js';
import {
chunk,
convertCamelToUnderscore,
convertTypes,
createTrackPixelHtml,
deepAccess,
deepClone,
getBidRequest,
isArray,
isArrayOfNums,
isEmpty,
isStr,
logError,
logMessage,
logWarn,
transformBidderParamKeywords
} from '../src/utils.js';
import {config} from '../src/config.js';
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {BANNER, NATIVE, VIDEO} from '../src/mediaTypes.js';
import {find, includes} from '../src/polyfill.js';
import {INSTREAM, OUTSTREAM} from '../src/video.js';
const BIDDER_CODE = 'adrelevantis';
const URL = 'https://ssp.adrelevantis.com/prebid';
const VIDEO_TARGETING = ['id', 'mimes', 'minduration', 'maxduration',
'startdelay', 'skippable', 'playback_method', 'frameworks'];
const USER_PARAMS = ['age', 'externalUid', 'segments', 'gender', 'dnt', 'language'];
const APP_DEVICE_PARAMS = ['geo', 'device_id']; // appid is collected separately
const SOURCE = 'pbjs';
const MAX_IMPS_PER_REQUEST = 15;
const NATIVE_MAPPING = {
body: 'description',
body2: 'desc2',
cta: 'ctatext',
image: {
serverName: 'main_image',
requiredParams: { required: true }
},
icon: {
serverName: 'icon',
requiredParams: { required: true }
},
sponsoredBy: 'sponsored_by',
privacyLink: 'privacy_link',
salePrice: 'saleprice',
displayUrl: 'displayurl'
};
export const spec = {
code: BIDDER_CODE,
aliases: ['adr', 'adsmart', 'compariola'],
supportedMediaTypes: [BANNER, VIDEO, NATIVE],
/**
* Determines whether or not the given bid request is valid.
*
* @param {object} bid The bid to validate.
* @return boolean True if this is a valid bid, and false otherwise.
*/
isBidRequestValid: function(bid) {
return !!(bid.params.placementId);
},
/**
* Make a server request from the list of BidRequests.
*
* @param {BidRequest[]} bidRequests A non-empty list of bid requests which should be sent to the Server.
* @return ServerRequest Info describing the request to the server.
*/
buildRequests: function(bidRequests, bidderRequest) {
const tags = bidRequests.map(bidToTag);
const userObjBid = find(bidRequests, hasUserInfo);
let userObj;
if (config.getConfig('coppa') === true) {
userObj = {'coppa': true};
}
if (userObjBid) {
userObj = {};
Object.keys(userObjBid.params.user)
.filter(param => includes(USER_PARAMS, param))
.forEach(param => userObj[param] = userObjBid.params.user[param]);
}
const appDeviceObjBid = find(bidRequests, hasAppDeviceInfo);
let appDeviceObj;
if (appDeviceObjBid && appDeviceObjBid.params && appDeviceObjBid.params.app) {
appDeviceObj = {};
Object.keys(appDeviceObjBid.params.app)
.filter(param => includes(APP_DEVICE_PARAMS, param))
.forEach(param => appDeviceObj[param] = appDeviceObjBid.params.app[param]);
}
const appIdObjBid = find(bidRequests, hasAppId);
let appIdObj;
if (appIdObjBid && appIdObjBid.params && appDeviceObjBid.params.app && appDeviceObjBid.params.app.id) {
appIdObj = {
appid: appIdObjBid.params.app.id
};
}
const payload = {
tags: [...tags],
user: userObj,
sdk: {
source: SOURCE,
version: '$prebid.version$'
}
};
if (appDeviceObjBid) {
payload.device = appDeviceObj
}
if (appIdObjBid) {
payload.app = appIdObj;
}
if (bidderRequest && bidderRequest.gdprConsent) {
// note - objects for impbus use underscore instead of camelCase
payload.gdpr_consent = {
consent_string: bidderRequest.gdprConsent.consentString,
consent_required: bidderRequest.gdprConsent.gdprApplies
};
}
if (bidderRequest && bidderRequest.refererInfo) {
let refererinfo = {
rd_ref: encodeURIComponent(bidderRequest.refererInfo.referer),
rd_top: bidderRequest.refererInfo.reachedTop,
rd_ifs: bidderRequest.refererInfo.numIframes,
rd_stk: bidderRequest.refererInfo.stack.map((url) => encodeURIComponent(url)).join(',')
}
payload.referrer_detection = refererinfo;
}
let fpdcfg = config.getLegacyFpd(config.getConfig('ortb2'));
if (fpdcfg && fpdcfg.context) {
let fdata = {
keywords: fpdcfg.context.keywords || '',
category: fpdcfg.context.data.category || ''
}
payload.fpd = fdata;
}
const request = formatRequest(payload, bidderRequest);
return request;
},
/**
* Unpack the response from the server into a list of bids.
*
* @param {*} serverResponse A successful response from the server.
* @return {Bid[]} An array of bids which were nested inside the server.
*/
interpretResponse: function(serverResponse, {bidderRequest}) {
serverResponse = serverResponse.body;
const bids = [];
if (!serverResponse || serverResponse.error) {
let errorMessage = `in response for ${bidderRequest.bidderCode} adapter`;
if (serverResponse && serverResponse.error) { errorMessage += `: ${serverResponse.error}`; }
logError(errorMessage);
return bids;
}
if (serverResponse.tags) {
serverResponse.tags.forEach(serverBid => {
const rtbBid = getRtbBid(serverBid);
if (rtbBid) {
if (rtbBid.cpm !== 0 && includes(this.supportedMediaTypes, rtbBid.ad_type)) {
const bid = newBid(serverBid, rtbBid, bidderRequest);
bid.mediaType = parseMediaType(rtbBid);
bids.push(bid);
}
}
});
}
return bids;
},
transformBidParams: function(params, isOpenRtb) {
params = convertTypes({
'placementId': 'number',
'keywords': transformBidderParamKeywords
}, params);
if (isOpenRtb) {
params.use_pmt_rule = (typeof params.usePaymentRule === 'boolean') ? params.usePaymentRule : false;
if (params.usePaymentRule) { delete params.usePaymentRule; }
if (isPopulatedArray(params.keywords)) {
params.keywords.forEach(deleteValues);
}
Object.keys(params).forEach(paramKey => {
let convertedKey = convertCamelToUnderscore(paramKey);
if (convertedKey !== paramKey) {
params[convertedKey] = params[paramKey];
delete params[paramKey];
}
});
}
return params;
}
}
function isPopulatedArray(arr) {
return !!(isArray(arr) && arr.length > 0);
}
function deleteValues(keyPairObj) {
if (isPopulatedArray(keyPairObj.value) && keyPairObj.value[0] === '') {
delete keyPairObj.value;
}
}
function formatRequest(payload, bidderRequest) {
let request = [];
if (payload.tags.length > MAX_IMPS_PER_REQUEST) {
const clonedPayload = deepClone(payload);
chunk(payload.tags, MAX_IMPS_PER_REQUEST).forEach(tags => {
clonedPayload.tags = tags;
const payloadString = JSON.stringify(clonedPayload);
request.push({
method: 'POST',
url: URL,
data: payloadString,
bidderRequest
});
});
} else {
const payloadString = JSON.stringify(payload);
request = {
method: 'POST',
url: URL,
data: payloadString,
bidderRequest
};
}
return request;
}
function newRenderer(adUnitCode, rtbBid, rendererOptions = {}) {
const renderer = Renderer.install({
id: rtbBid.renderer_id,
url: rtbBid.renderer_url,
config: rendererOptions,
loaded: false,
adUnitCode
});
try {
renderer.setRender(outstreamRender);
} catch (err) {
logWarn('Prebid Error calling setRender on renderer', err);
}
renderer.setEventHandlers({
impression: () => logMessage('AdRelevantis outstream video impression event'),
loaded: () => logMessage('AdRelevantis outstream video loaded event'),
ended: () => {
logMessage('AdRelevantis outstream renderer video event');
document.querySelector(`#${adUnitCode}`).style.display = 'none';
}
});
return renderer;
}
/**
* This function hides google div container for outstream bids to remove unwanted space on page. Appnexus renderer creates a new iframe outside of google iframe to render the outstream creative.
* @param {string} elementId element id
*/
function hidedfpContainer(elementId) {
var el = document.getElementById(elementId).querySelectorAll("div[id^='google_ads']");
if (el[0]) {
el[0].style.setProperty('display', 'none');
}
}
function outstreamRender(bid) {
// push to render queue because ANOutstreamVideo may not be loaded yet
hidedfpContainer(bid.adUnitCode);
bid.renderer.push(() => {
window.ANOutstreamVideo.renderAd({
tagId: bid.adResponse.tag_id,
sizes: [bid.getSize().split('x')],
targetId: bid.adUnitCode, // target div id to render video
uuid: bid.adResponse.uuid,
adResponse: bid.adResponse,
rendererOptions: bid.renderer.getConfig()
}, handleOutstreamRendererEvents.bind(null, bid));
});
}
function handleOutstreamRendererEvents(bid, id, eventName) {
bid.renderer.handleVideoEvent({ id, eventName });
}
/**
* Unpack the Server's Bid into a Prebid-compatible one.
* @param serverBid
* @param rtbBid
* @param bidderRequest
* @return Bid
*/
function newBid(serverBid, rtbBid, bidderRequest) {
const bidRequest = getBidRequest(serverBid.uuid, [bidderRequest]);
const bid = {
requestId: serverBid.uuid,
cpm: rtbBid.cpm,
creativeId: rtbBid.creative_id,
dealId: rtbBid.deal_id,
currency: 'USD',
netRevenue: true,
ttl: 300,
adUnitCode: bidRequest.adUnitCode,
adrelevantis: {
buyerMemberId: rtbBid.buyer_member_id,
dealPriority: rtbBid.deal_priority,
dealCode: rtbBid.deal_code
}
};
if (rtbBid.advertiser_id) {
bid.meta = Object.assign({}, bid.meta, { advertiserId: rtbBid.advertiser_id });
}
if (rtbBid.rtb.video) {
Object.assign(bid, {
width: rtbBid.rtb.video.player_width,
height: rtbBid.rtb.video.player_height,
vastImpUrl: rtbBid.notify_url,
ttl: 3600
});
const videoContext = deepAccess(bidRequest, 'mediaTypes.video.context');
switch (videoContext) {
case OUTSTREAM:
bid.adResponse = serverBid;
bid.adResponse.ad = bid.adResponse.ads[0];
bid.adResponse.ad.video = bid.adResponse.ad.rtb.video;
bid.vastXml = rtbBid.rtb.video.content;
if (rtbBid.renderer_url) {
const videoBid = find(bidderRequest.bids, bid => bid.bidId === serverBid.uuid);
const rendererOptions = deepAccess(videoBid, 'renderer.options');
bid.renderer = newRenderer(bid.adUnitCode, rtbBid, rendererOptions);
}
break;
case INSTREAM:
bid.vastUrl = rtbBid.notify_url + '&redir=' + encodeURIComponent(rtbBid.rtb.video.asset_url);
break;
}
} else if (rtbBid.rtb[NATIVE]) {
const nativeAd = rtbBid.rtb[NATIVE];
// setting up the jsTracker:
// we put it as a data-src attribute so that the tracker isn't called
// until we have the adId (see onBidWon)
let jsTrackerDisarmed = rtbBid.viewability.config.replace('src=', 'data-src=');
let jsTrackers = nativeAd.javascript_trackers;
if (jsTrackers == undefined) {
jsTrackers = jsTrackerDisarmed;
} else if (isStr(jsTrackers)) {
jsTrackers = [jsTrackers, jsTrackerDisarmed];
} else {
jsTrackers.push(jsTrackerDisarmed);
}
bid[NATIVE] = {
title: nativeAd.title,
body: nativeAd.desc,
body2: nativeAd.desc2,
cta: nativeAd.ctatext,
rating: nativeAd.rating,
sponsoredBy: nativeAd.sponsored,
privacyLink: nativeAd.privacy_link,
address: nativeAd.address,
downloads: nativeAd.downloads,
likes: nativeAd.likes,
phone: nativeAd.phone,
price: nativeAd.price,
salePrice: nativeAd.saleprice,
clickUrl: nativeAd.link.url,
displayUrl: nativeAd.displayurl,
clickTrackers: nativeAd.link.click_trackers,
impressionTrackers: nativeAd.impression_trackers,
javascriptTrackers: jsTrackers
};
if (nativeAd.main_img) {
bid['native'].image = {
url: nativeAd.main_img.url,
height: nativeAd.main_img.height,
width: nativeAd.main_img.width,
};
}
if (nativeAd.icon) {
bid['native'].icon = {
url: nativeAd.icon.url,
height: nativeAd.icon.height,
width: nativeAd.icon.width,
};
}
} else {
Object.assign(bid, {
width: rtbBid.rtb.banner.width,
height: rtbBid.rtb.banner.height,
ad: rtbBid.rtb.banner.content
});
try {
const url = rtbBid.rtb.trackers[0].impression_urls[0];
const tracker = createTrackPixelHtml(url);
bid.ad += tracker;
} catch (error) {
logError('Error appending tracking pixel', error);
}
}
return bid;
}
function bidToTag(bid) {
const tag = {};
tag.sizes = transformSizes(bid.sizes);
tag.primary_size = tag.sizes[0];
tag.ad_types = [];
tag.uuid = bid.bidId;
if (bid.params.placementId) {
tag.id = parseInt(bid.params.placementId, 10);
}
if (bid.params.cpm) {
tag.cpm = bid.params.cpm;
}
tag.allow_smaller_sizes = bid.params.allowSmallerSizes || false;
tag.use_pmt_rule = bid.params.usePaymentRule || false
tag.prebid = true;
tag.disable_psa = true;
if (bid.params.position) {
tag.position = {'above': 1, 'below': 2}[bid.params.position] || 0;
}
if (bid.params.trafficSourceCode) {
tag.traffic_source_code = bid.params.trafficSourceCode;
}
if (bid.params.privateSizes) {
tag.private_sizes = transformSizes(bid.params.privateSizes);
}
if (bid.params.supplyType) {
tag.supply_type = bid.params.supplyType;
}
if (bid.params.pubClick) {
tag.pubclick = bid.params.pubClick;
}
if (bid.params.extInvCode) {
tag.ext_inv_code = bid.params.extInvCode;
}
if (bid.params.externalImpId) {
tag.external_imp_id = bid.params.externalImpId;
}
if (!isEmpty(bid.params.keywords)) {
let keywords = transformBidderParamKeywords(bid.params.keywords);
if (keywords.length > 0) {
keywords.forEach(deleteValues);
}
tag.keywords = keywords;
}
if (bid.params.category) {
tag.category = bid.params.category;
}
if (bid.mediaType === NATIVE || deepAccess(bid, `mediaTypes.${NATIVE}`)) {
tag.ad_types.push(NATIVE);
if (tag.sizes.length === 0) {
tag.sizes = transformSizes([1, 1]);
}
if (bid.nativeParams) {
const nativeRequest = buildNativeRequest(bid.nativeParams);
tag[NATIVE] = {layouts: [nativeRequest]};
}
}
const videoMediaType = deepAccess(bid, `mediaTypes.${VIDEO}`);
const context = deepAccess(bid, 'mediaTypes.video.context');
tag.hb_source = 1;
if (bid.mediaType === VIDEO || videoMediaType) {
tag.ad_types.push(VIDEO);
}
// instream gets vastUrl, outstream gets vastXml
if (bid.mediaType === VIDEO || (videoMediaType && context !== 'outstream')) {
tag.require_asset_url = true;
}
if (bid.params.video) {
tag.video = {};
// place any valid video params on the tag
Object.keys(bid.params.video)
.filter(param => includes(VIDEO_TARGETING, param))
.forEach(param => tag.video[param] = bid.params.video[param]);
}
if (bid.renderer) {
tag.video = Object.assign({}, tag.video, {custom_renderer_present: true});
}
if (
(isEmpty(bid.mediaType) && isEmpty(bid.mediaTypes)) ||
(bid.mediaType === BANNER || (bid.mediaTypes && bid.mediaTypes[BANNER]))
) {
tag.ad_types.push(BANNER);
}
return tag;
}
/* Turn bid request sizes into ut-compatible format */
function transformSizes(requestSizes) {
let sizes = [];
let sizeObj = {};
if (isArray(requestSizes) && requestSizes.length === 2 &&
!isArray(requestSizes[0])) {
sizeObj.width = parseInt(requestSizes[0], 10);
sizeObj.height = parseInt(requestSizes[1], 10);
sizes.push(sizeObj);
} else if (typeof requestSizes === 'object') {
for (let i = 0; i < requestSizes.length; i++) {
let size = requestSizes[i];
sizeObj = {};
sizeObj.width = parseInt(size[0], 10);
sizeObj.height = parseInt(size[1], 10);
sizes.push(sizeObj);
}
}
return sizes;
}
function hasUserInfo(bid) {
return !!bid.params.user;
}
function hasAppDeviceInfo(bid) {
if (bid.params) {
return !!bid.params.app
}
}
function hasAppId(bid) {
if (bid.params && bid.params.app) {
return !!bid.params.app.id
}
return !!bid.params.app
}
function getRtbBid(tag) {
return tag && tag.ads && tag.ads.length && find(tag.ads, ad => ad.rtb);
}
function buildNativeRequest(params) {
const request = {};
// map standard prebid native asset identifier to /ut parameters
// e.g., tag specifies `body` but /ut only knows `description`.
// mapping may be in form {tag: '<server name>'} or
// {tag: {serverName: '<server name>', requiredParams: {...}}}
Object.keys(params).forEach(key => {
// check if one of the <server name> forms is used, otherwise
// a mapping wasn't specified so pass the key straight through
const requestKey =
(NATIVE_MAPPING[key] && NATIVE_MAPPING[key].serverName) ||
NATIVE_MAPPING[key] ||
key;
// required params are always passed on request
const requiredParams = NATIVE_MAPPING[key] && NATIVE_MAPPING[key].requiredParams;
request[requestKey] = Object.assign({}, requiredParams, params[key]);
// convert the sizes of image/icon assets to proper format (if needed)
const isImageAsset = !!(requestKey === NATIVE_MAPPING.image.serverName || requestKey === NATIVE_MAPPING.icon.serverName);
if (isImageAsset && request[requestKey].sizes) {
let sizes = request[requestKey].sizes;
if (isArrayOfNums(sizes) || (isArray(sizes) && sizes.length > 0 && sizes.every(sz => isArrayOfNums(sz)))) {
request[requestKey].sizes = transformSizes(request[requestKey].sizes);
}
}
if (requestKey === NATIVE_MAPPING.privacyLink) {
request.privacy_supported = true;
}
});
return request;
}
function parseMediaType(rtbBid) {
const adType = rtbBid.ad_type;
if (adType === VIDEO) {
return VIDEO;
} else {
return BANNER;
}
}
registerBidder(spec);