forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadkernelBidAdapter.js
611 lines (573 loc) · 16.8 KB
/
adkernelBidAdapter.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
import {
_each,
contains,
createTrackPixelHtml,
deepAccess,
deepSetValue,
getDefinedParams,
getDNT,
isArray,
isArrayOfNums,
isEmpty,
isNumber,
isPlainObject,
isStr,
mergeDeep,
parseGPTSingleSizeArrayToRtbSize
} from '../src/utils.js';
import {BANNER, NATIVE, VIDEO} from '../src/mediaTypes.js';
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {find} from '../src/polyfill.js';
import {config} from '../src/config.js';
import {getAdUnitSizes} from '../libraries/sizeUtils/sizeUtils.js';
import {getBidFloor} from '../libraries/adkernelUtils/adkernelUtils.js'
/**
* In case you're AdKernel whitelable platform's client who needs branded adapter to
* work with Adkernel platform - DO NOT COPY THIS ADAPTER UNDER NEW NAME
*
* Please contact [email protected] and we'll add your adapter as an alias
* @typedef {import('../src/adapters/bidderFactory.js').Bid} Bid
* @typedef {import('../src/adapters/bidderFactory.js').ServerRequest} ServerRequest
* @typedef {import('../src/adapters/bidderFactory.js').UserSync} UserSync
*/
const VIDEO_PARAMS = ['pos', 'context', 'placement', 'plcmt', 'api', 'mimes', 'protocols', 'playbackmethod', 'minduration', 'maxduration',
'startdelay', 'linearity', 'skip', 'skipmin', 'skipafter', 'minbitrate', 'maxbitrate', 'delivery', 'playbackend', 'boxingallowed'];
const VIDEO_FPD = ['battr', 'pos'];
const NATIVE_FPD = ['battr', 'api'];
const BANNER_PARAMS = ['pos'];
const BANNER_FPD = ['btype', 'battr', 'pos', 'api'];
const VERSION = '1.7';
const SYNC_IFRAME = 1;
const SYNC_IMAGE = 2;
const SYNC_TYPES = {
1: 'iframe',
2: 'image'
};
const GVLID = 14;
const MULTI_FORMAT_SUFFIX = '__mf';
const MULTI_FORMAT_SUFFIX_BANNER = 'b' + MULTI_FORMAT_SUFFIX;
const MULTI_FORMAT_SUFFIX_VIDEO = 'v' + MULTI_FORMAT_SUFFIX;
const MULTI_FORMAT_SUFFIX_NATIVE = 'n' + MULTI_FORMAT_SUFFIX;
const MEDIA_TYPES = {
BANNER: 1,
VIDEO: 2,
NATIVE: 4
};
/**
* Adapter for requesting bids from AdKernel white-label display platform
*/
export const spec = {
code: 'adkernel',
gvlid: GVLID,
aliases: [
{code: 'headbidding'},
{code: 'adsolut'},
{code: 'oftmediahb'},
{code: 'audiencemedia'},
{code: 'waardex_ak'},
{code: 'roqoon'},
{code: 'adbite'},
{code: 'houseofpubs'},
{code: 'torchad'},
{code: 'stringads'},
{code: 'bcm'},
{code: 'engageadx'},
{code: 'converge', gvlid: 248},
{code: 'adomega'},
{code: 'denakop'},
{code: 'rtbanalytica'},
{code: 'unibots'},
{code: 'ergadx'},
{code: 'turktelekom'},
{code: 'motionspots'},
{code: 'sonic_twist'},
{code: 'displayioads'},
{code: 'rtbdemand_com'},
{code: 'bidbuddy'},
{code: 'didnadisplay'},
{code: 'qortex'},
{code: 'adpluto'},
{code: 'headbidder'},
{code: 'digiad'},
{code: 'monetix'},
{code: 'hyperbrainz'},
{code: 'voisetech'},
{code: 'global_sun'},
{code: 'rxnetwork'},
{code: 'revbid'},
{code: 'spinx', gvlid: 1308},
{code: 'oppamedia'},
{code: 'pixelpluses', gvlid: 1209}
],
supportedMediaTypes: [BANNER, VIDEO, NATIVE],
/**
* Validates bid request for adunit
* @param bidRequest {BidRequest}
* @returns {boolean}
*/
isBidRequestValid: function (bidRequest) {
return 'params' in bidRequest &&
typeof bidRequest.params.host !== 'undefined' &&
'zoneId' in bidRequest.params &&
!isNaN(Number(bidRequest.params.zoneId)) &&
bidRequest.params.zoneId > 0 &&
bidRequest.mediaTypes &&
(bidRequest.mediaTypes.banner || bidRequest.mediaTypes.video ||
(bidRequest.mediaTypes.native && validateNativeAdUnit(bidRequest.mediaTypes.native))
);
},
/**
* Builds http request for each unique combination of adkernel host/zone
* @param bidRequests {BidRequest[]}
* @param bidderRequest {BidderRequest}
* @returns {ServerRequest[]}
*/
buildRequests: function (bidRequests, bidderRequest) {
let impGroups = groupImpressionsByHostZone(bidRequests, bidderRequest.refererInfo);
let requests = [];
let schain = bidRequests[0].schain;
_each(impGroups, impGroup => {
let {host, zoneId, imps} = impGroup;
const request = buildRtbRequest(imps, bidderRequest, schain);
requests.push({
method: 'POST',
url: `https://${host}/hb?zone=${zoneId}&v=${VERSION}`,
data: JSON.stringify(request)
});
});
return requests;
},
/**
* Parse response from adkernel backend
* @param serverResponse {ServerResponse}
* @param serverRequest {ServerRequest}
* @returns {Bid[]}
*/
interpretResponse: function (serverResponse, serverRequest) {
let response = serverResponse.body;
if (!response.seatbid) {
return [];
}
let rtbRequest = JSON.parse(serverRequest.data);
let rtbBids = response.seatbid
.map(seatbid => seatbid.bid)
.reduce((a, b) => a.concat(b), []);
return rtbBids.map(rtbBid => {
let imp = find(rtbRequest.imp, imp => imp.id === rtbBid.impid);
let prBid = {
requestId: rtbBid.impid,
cpm: rtbBid.price,
creativeId: rtbBid.crid,
currency: response.cur || 'USD',
ttl: 360,
netRevenue: true
};
if (prBid.requestId.endsWith(MULTI_FORMAT_SUFFIX)) {
prBid.requestId = stripMultiformatSuffix(prBid.requestId);
}
if (rtbBid.mtype === MEDIA_TYPES.BANNER) {
prBid.mediaType = BANNER;
prBid.width = rtbBid.w;
prBid.height = rtbBid.h;
prBid.ad = formatAdMarkup(rtbBid);
} else if (rtbBid.mtype === MEDIA_TYPES.VIDEO) {
prBid.mediaType = VIDEO;
prBid.vastUrl = rtbBid.nurl;
prBid.width = imp.video.w;
prBid.height = imp.video.h;
} else if (rtbBid.mtype === MEDIA_TYPES.NATIVE) {
prBid.mediaType = NATIVE;
prBid.native = {
ortb: buildNativeAd(rtbBid.adm)
};
}
if (isStr(rtbBid.dealid)) {
prBid.dealId = rtbBid.dealid;
}
if (isArray(rtbBid.adomain)) {
deepSetValue(prBid, 'meta.advertiserDomains', rtbBid.adomain);
}
if (isArray(rtbBid.cat)) {
deepSetValue(prBid, 'meta.secondaryCatIds', rtbBid.cat);
}
if (isPlainObject(rtbBid.ext)) {
if (isNumber(rtbBid.ext.advertiser_id)) {
deepSetValue(prBid, 'meta.advertiserId', rtbBid.ext.advertiser_id);
}
if (isStr(rtbBid.ext.advertiser_name)) {
deepSetValue(prBid, 'meta.advertiserName', rtbBid.ext.advertiser_name);
}
if (isStr(rtbBid.ext.agency_name)) {
deepSetValue(prBid, 'meta.agencyName', rtbBid.ext.agency_name);
}
}
return prBid;
});
},
/**
* Extracts user-syncs information from server response
* @param syncOptions {SyncOptions}
* @param serverResponses {ServerResponse[]}
* @returns {UserSync[]}
*/
getUserSyncs: function (syncOptions, serverResponses) {
if (!serverResponses || serverResponses.length === 0 || (!syncOptions.iframeEnabled && !syncOptions.pixelEnabled)) {
return [];
}
return serverResponses.filter(rsp => rsp.body && rsp.body.ext && rsp.body.ext.adk_usersync)
.map(rsp => rsp.body.ext.adk_usersync)
.reduce((a, b) => a.concat(b), [])
.map(({url, type}) => ({type: SYNC_TYPES[type], url: url}));
}
};
registerBidder(spec);
/**
* Dispatch impressions by ad network host and zone
* @param bidRequests {BidRequest[]}
* @param refererInfo {refererInfo}
*/
function groupImpressionsByHostZone(bidRequests, refererInfo) {
let secure = (refererInfo && refererInfo.page?.indexOf('https:') === 0);
return Object.values(
bidRequests.map(bidRequest => buildImps(bidRequest, secure))
.reduce((acc, curr, index) => {
let bidRequest = bidRequests[index];
let {zoneId, host} = bidRequest.params;
let key = `${host}_${zoneId}`;
acc[key] = acc[key] || {host: host, zoneId: zoneId, imps: []};
acc[key].imps.push(...curr);
return acc;
}, {})
);
}
/**
* Builds rtb imp object(s) for single adunit
* @param bidRequest {BidRequest}
* @param secure {boolean}
*/
function buildImps(bidRequest, secure) {
let imp = {
'id': bidRequest.bidId,
'tagid': bidRequest.adUnitCode
};
if (secure) {
imp.secure = bidRequest.ortb2Imp?.secure ?? 1;
}
var sizes = [];
let mediaTypes = bidRequest.mediaTypes;
let isMultiformat = (~~!!mediaTypes?.banner + ~~!!mediaTypes?.video + ~~!!mediaTypes?.native) > 1;
let result = [];
let typedImp;
if (mediaTypes?.banner) {
if (isMultiformat) {
typedImp = {...imp};
typedImp.id = imp.id + MULTI_FORMAT_SUFFIX_BANNER;
} else {
typedImp = imp;
}
sizes = getAdUnitSizes(bidRequest);
let pbBanner = mediaTypes.banner;
typedImp.banner = {
...getDefinedParamsOrEmpty(bidRequest.ortb2Imp, BANNER_FPD),
...getDefinedParamsOrEmpty(pbBanner, BANNER_PARAMS),
format: sizes.map(wh => parseGPTSingleSizeArrayToRtbSize(wh)),
topframe: 0
};
initImpBidfloor(typedImp, bidRequest, sizes, isMultiformat ? '*' : BANNER);
result.push(typedImp);
}
if (mediaTypes?.video) {
if (isMultiformat) {
typedImp = {...imp};
typedImp.id = typedImp.id + MULTI_FORMAT_SUFFIX_VIDEO;
} else {
typedImp = imp;
}
let pbVideo = mediaTypes.video;
typedImp.video = {
...getDefinedParamsOrEmpty(bidRequest.ortb2Imp, VIDEO_FPD),
...getDefinedParamsOrEmpty(pbVideo, VIDEO_PARAMS)
};
if (pbVideo.playerSize) {
sizes = pbVideo.playerSize[0];
typedImp.video = Object.assign(typedImp.video, parseGPTSingleSizeArrayToRtbSize(sizes) || {});
} else if (pbVideo.w && pbVideo.h) {
typedImp.video.w = pbVideo.w;
typedImp.video.h = pbVideo.h;
}
initImpBidfloor(typedImp, bidRequest, sizes, isMultiformat ? '*' : VIDEO);
result.push(typedImp);
}
if (mediaTypes?.native) {
if (isMultiformat) {
typedImp = {...imp};
typedImp.id = typedImp.id + MULTI_FORMAT_SUFFIX_NATIVE;
} else {
typedImp = imp;
}
typedImp.native = {
...getDefinedParamsOrEmpty(bidRequest.ortb2Imp, NATIVE_FPD),
request: JSON.stringify(bidRequest.nativeOrtbRequest)
};
initImpBidfloor(typedImp, bidRequest, sizes, isMultiformat ? '*' : NATIVE);
result.push(typedImp);
}
return result;
}
function initImpBidfloor(imp, bid, sizes, mediaType) {
let bidfloor = getBidFloor(bid, mediaType, sizes);
if (bidfloor) {
imp.bidfloor = bidfloor;
}
}
function getDefinedParamsOrEmpty(object, params) {
if (object === undefined) {
return {};
}
return getDefinedParams(object, params);
}
/**
* Checks if configuration allows specified sync method
* @param syncRule {Object}
* @param bidderCode {string}
* @returns {boolean}
*/
function isSyncMethodAllowed(syncRule, bidderCode) {
if (!syncRule) {
return false;
}
let bidders = isArray(syncRule.bidders) ? syncRule.bidders : [bidderCode];
let rule = syncRule.filter === 'include';
return contains(bidders, bidderCode) === rule;
}
/**
* Get preferred user-sync method based on publisher configuration
* @param bidderCode {string}
* @returns {number|undefined}
*/
function getAllowedSyncMethod(bidderCode) {
if (!config.getConfig('userSync.syncEnabled')) {
return;
}
let filterConfig = config.getConfig('userSync.filterSettings');
if (isSyncMethodAllowed(filterConfig.all, bidderCode) || isSyncMethodAllowed(filterConfig.iframe, bidderCode)) {
return SYNC_IFRAME;
} else if (isSyncMethodAllowed(filterConfig.image, bidderCode)) {
return SYNC_IMAGE;
}
}
/**
* Create device object from fpd and host-collected data
* @param fpd {Object}
* @returns {{device: Object}}
*/
function makeDevice(fpd) {
let device = mergeDeep({
'ip': 'caller',
'ipv6': 'caller',
'ua': 'caller',
'js': 1,
'language': getLanguage()
}, fpd.device || {});
if (getDNT()) {
device.dnt = 1;
}
return {device: device};
}
/**
* Create site or app description object
* @param bidderRequest {BidderRequest}
* @param fpd {Object}
* @returns {{site: Object}|{app: Object}}
*/
function makeSiteOrApp(bidderRequest, fpd) {
let {refererInfo} = bidderRequest;
let appConfig = config.getConfig('app');
if (isEmpty(appConfig)) {
return {site: createSite(refererInfo, fpd)}
} else {
return {app: appConfig};
}
}
/**
* Create user description object
* @param bidderRequest {BidderRequest}
* @param fpd {Object}
* @returns {{user: Object} | undefined}
*/
function makeUser(bidderRequest, fpd) {
let {gdprConsent} = bidderRequest;
let user = fpd.user || {};
if (gdprConsent && gdprConsent.consentString !== undefined) {
deepSetValue(user, 'ext.consent', gdprConsent.consentString);
}
let eids = getExtendedUserIds(bidderRequest);
if (eids) {
deepSetValue(user, 'ext.eids', eids);
}
if (!isEmpty(user)) {
return {user: user};
}
}
/**
* Create privacy regulations object
* @param bidderRequest {BidderRequest}
* @returns {{regs: Object} | undefined}
*/
function makeRegulations(bidderRequest) {
let {gdprConsent, uspConsent, gppConsent} = bidderRequest;
let regs = {};
if (gdprConsent) {
if (gdprConsent.gdprApplies !== undefined) {
deepSetValue(regs, 'regs.ext.gdpr', ~~gdprConsent.gdprApplies);
}
}
if (gppConsent) {
deepSetValue(regs, 'regs.gpp', gppConsent.gppString);
deepSetValue(regs, 'regs.gpp_sid', gppConsent.applicableSections);
}
if (uspConsent) {
deepSetValue(regs, 'regs.ext.us_privacy', uspConsent);
}
if (config.getConfig('coppa')) {
deepSetValue(regs, 'regs.coppa', 1);
}
if (!isEmpty(regs)) {
return regs;
}
}
/**
* Create top-level request object
* @param bidderRequest {BidderRequest}
* @param imps {Object} Impressions
* @param fpd {Object} First party data
* @returns
*/
function makeBaseRequest(bidderRequest, imps, fpd) {
let request = {
'id': bidderRequest.bidderRequestId,
'imp': imps,
'at': 1,
'tmax': parseInt(bidderRequest.timeout)
};
if (!isEmpty(fpd.bcat)) {
request.bcat = fpd.bcat;
}
if (!isEmpty(fpd.badv)) {
request.badv = fpd.badv;
}
return request;
}
/**
* Initialize sync capabilities
* @param bidderRequest {BidderRequest}
*/
function makeSyncInfo(bidderRequest) {
let {bidderCode} = bidderRequest;
let syncMethod = getAllowedSyncMethod(bidderCode);
if (syncMethod) {
let res = {};
deepSetValue(res, 'ext.adk_usersync', syncMethod);
return res;
}
}
/**
* Builds complete rtb request
* @param imps {Object} Collection of rtb impressions
* @param bidderRequest {BidderRequest}
* @param schain {Object=} Supply chain config
* @return {Object} Complete rtb request
*/
function buildRtbRequest(imps, bidderRequest, schain) {
let fpd = bidderRequest.ortb2 || {};
let req = mergeDeep(
makeBaseRequest(bidderRequest, imps, fpd),
makeDevice(fpd),
makeSiteOrApp(bidderRequest, fpd),
makeUser(bidderRequest, fpd),
makeRegulations(bidderRequest),
makeSyncInfo(bidderRequest)
);
if (schain) {
deepSetValue(req, 'source.ext.schain', schain);
}
return req;
}
/**
* Get browser language
* @returns {String}
*/
function getLanguage() {
const language = navigator.language ? 'language' : 'userLanguage';
return navigator[language].split('-')[0];
}
/**
* Creates site description object
*/
function createSite(refInfo, fpd) {
let site = {
'domain': refInfo.domain,
'page': refInfo.page
};
mergeDeep(site, fpd.site);
if (refInfo.ref != null) {
site.ref = refInfo.ref;
} else {
delete site.ref;
}
return site;
}
function getExtendedUserIds(bidderRequest) {
let eids = deepAccess(bidderRequest, 'bids.0.userIdAsEids');
if (isArray(eids)) {
return eids;
}
}
/**
* Format creative with optional nurl call
* @param bid rtb Bid object
*/
function formatAdMarkup(bid) {
let adm = bid.adm;
if ('nurl' in bid) {
adm += createTrackPixelHtml(`${bid.nurl}&px=1`);
}
return adm;
}
/**
* Basic validates to comply with platform requirements
*/
function validateNativeAdUnit(adUnit) {
return validateNativeImageSize(adUnit.image) && validateNativeImageSize(adUnit.icon) &&
!deepAccess(adUnit, 'privacyLink.required') && // not supported yet
!deepAccess(adUnit, 'privacyIcon.required'); // not supported yet
}
/**
* Validates image asset size definition
*/
function validateNativeImageSize(img) {
if (!img) {
return true;
}
if (img.sizes) {
return isArrayOfNums(img.sizes, 2);
}
if (isArray(img.aspect_ratios)) {
return img.aspect_ratios.length > 0 && img.aspect_ratios[0].min_height && img.aspect_ratios[0].min_width;
}
return true;
}
/**
* Creates native ad for native 1.2 response
*/
function buildNativeAd(adm) {
let resp = JSON.parse(adm);
// temporary workaround for top-level native object wrapper
if ('native' in resp) {
resp = resp.native;
}
return resp;
}
function stripMultiformatSuffix(impid) {
return impid.substr(0, impid.length - MULTI_FORMAT_SUFFIX.length - 1);
}