forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kueezBidAdapter.js
486 lines (427 loc) · 12.4 KB
/
kueezBidAdapter.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
import {
logWarn,
logInfo,
isArray,
isFn,
deepAccess,
isEmpty,
contains,
timestamp,
triggerPixel,
isInteger,
getBidIdParameter
} from '../src/utils.js';
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { BANNER, VIDEO } from '../src/mediaTypes.js';
import { config } from '../src/config.js';
const BIDDER_ENDPOINT = 'https://hb.kueezssp.com/hb-kz-multi';
const BIDDER_TEST_ENDPOINT = 'https://hb.kueezssp.com/hb-multi-kz-test'
const BIDDER_CODE = 'kueez';
const MAIN_CURRENCY = 'USD';
const MEDIA_TYPES = [BANNER, VIDEO];
const TTL = 420;
const VERSION = '1.0.0';
const SUPPORTED_SYNC_METHODS = {
IFRAME: 'iframe',
PIXEL: 'pixel'
}
export const spec = {
code: BIDDER_CODE,
version: VERSION,
supportedMediaTypes: MEDIA_TYPES,
isBidRequestValid: function (bidRequest) {
return validateParams(bidRequest);
},
buildRequests: function (validBidRequests, bidderRequest) {
const [ sharedParams ] = validBidRequests;
const testMode = sharedParams.params.testMode;
const bidsToSend = prepareBids(validBidRequests, sharedParams, bidderRequest);
return {
method: 'POST',
url: getBidderEndpoint(testMode),
data: bidsToSend
}
},
interpretResponse: function ({body}) {
const bidResponses = body?.bids;
if (!bidResponses || !bidResponses.length) {
return [];
}
return parseBidResponses(bidResponses);
},
getUserSyncs: function (syncOptions, serverResponses) {
const syncs = [];
for (const response of serverResponses) {
if (syncOptions.pixelEnabled && isArray(response.body.params.userSyncPixels)) {
const pixels = response.body.params.userSyncPixels.map(pixel => {
return {
type: 'image',
url: pixel
}
})
syncs.push(...pixels)
}
if (syncOptions.iframeEnabled && response.body.params.userSyncURL) {
syncs.push({
type: 'iframe',
url: response.body.params.userSyncURL
});
}
}
return syncs;
},
onBidWon: function (bid) {
if (bid == null) {
return;
}
logInfo('onBidWon:', bid);
if (bid.hasOwnProperty('nurl') && bid.nurl.length > 0) {
triggerPixel(bid.nurl);
}
}
};
registerBidder(spec);
/**
* Get schain string value
* @param schainObject {Object}
* @returns {string}
*/
function getSupplyChain(schainObject) {
if (isEmpty(schainObject)) {
return '';
}
let scStr = `${schainObject.ver},${schainObject.complete}`;
schainObject.nodes.forEach((node) => {
scStr += '!';
scStr += `${getEncodedValIfNotEmpty(node.asi)},`;
scStr += `${getEncodedValIfNotEmpty(node.sid)},`;
scStr += `${node.hp ? encodeURIComponent(node.hp) : ''},`;
scStr += `${getEncodedValIfNotEmpty(node.rid)},`;
scStr += `${getEncodedValIfNotEmpty(node.name)},`;
scStr += `${getEncodedValIfNotEmpty(node.domain)}`;
});
return scStr;
}
/**
* Get the encoded value
* @param val {string}
* @returns {string}
*/
function getEncodedValIfNotEmpty(val) {
return !isEmpty(val) ? encodeURIComponent(val) : '';
}
/**
* get device type
* @returns {string}
*/
function getDeviceType() {
const ua = navigator.userAgent;
if (/ipad|android 3.0|xoom|sch-i800|playbook|tablet|kindle/i
.test(ua.toLowerCase())) {
return '5';
}
if (/iphone|ipod|android|blackberry|opera|mini|windows\sce|palm|smartphone|iemobile/i
.test(ua.toLowerCase())) {
return '4';
}
if (/smart[-_\s]?tv|hbbtv|appletv|googletv|hdmi|netcast|viera|nettv|roku|\bdtv\b|sonydtv|inettvbrowser|\btv\b/i
.test(ua.toLowerCase())) {
return '3';
}
return '1';
}
/**
* Get floor price
* @param bid {bid}
* @param mediaType {string}
* @returns {Number}
*/
function getFloorPrice(bid, mediaType) {
let floor = 0;
if (isFn(bid.getFloor)) {
let floorResult = bid.getFloor({
currency: MAIN_CURRENCY,
mediaType: mediaType,
size: '*'
});
floor = floorResult.currency === MAIN_CURRENCY && floorResult.floor ? floorResult.floor : 0;
}
return floor;
}
/**
* Get the ad sizes array from the bid
* @param bid {bid}
* @param mediaType {string}
* @returns {Array}
*/
function getSizesArray(bid, mediaType) {
let sizes = []
if (deepAccess(bid, `mediaTypes.${mediaType}.sizes`)) {
sizes = bid.mediaTypes[mediaType].sizes;
} else if (Array.isArray(bid.sizes) && bid.sizes.length > 0) {
sizes = bid.sizes;
}
return sizes;
}
/**
* Get the preferred user-sync method
* @param filterSettings {filterSettings}
* @param bidderCode {string}
* @returns {string}
*/
function getSyncMethod(filterSettings, bidderCode) {
const iframeConfigs = ['all', 'iframe'];
const pixelConfig = 'image';
if (filterSettings && iframeConfigs.some(config => isSyncMethodAllowed(filterSettings[config], bidderCode))) {
return SUPPORTED_SYNC_METHODS.IFRAME;
}
if (!filterSettings || !filterSettings[pixelConfig] || isSyncMethodAllowed(filterSettings[pixelConfig], bidderCode)) {
return SUPPORTED_SYNC_METHODS.PIXEL;
}
}
/**
* Check sync rule support
* @param filterSetting {Object}
* @param bidderCode {string}
* @returns {boolean}
*/
function isSyncMethodAllowed(filterSetting, bidderCode) {
if (!filterSetting) {
return false;
}
const bidders = isArray(filterSetting.bidders) ? filterSetting.bidders : [bidderCode];
return filterSetting.filter === 'include' && contains(bidders, bidderCode);
}
/**
* Get the bidder endpoint
* @param testMode {boolean}
* @returns {string}
*/
function getBidderEndpoint(testMode) {
return testMode ? BIDDER_TEST_ENDPOINT : BIDDER_ENDPOINT;
}
/**
* Generates the bidder parameters
* @param validBidRequests {Array}
* @param bidderRequest {bidderRequest}
* @returns {Array}
*/
function generateBidParams(validBidRequests, bidderRequest) {
const bidsArray = [];
if (validBidRequests.length) {
validBidRequests.forEach(bid => {
bidsArray.push(generateBidParameters(bid, bidderRequest));
});
}
return bidsArray;
}
/**
* Generate bid specific parameters
* @param bid {bid}
* @param bidderRequest {bidderRequest}
* @returns {Object} bid specific params object
*/
function generateBidParameters(bid, bidderRequest) {
const {params} = bid;
const mediaType = isBanner(bid) ? BANNER : VIDEO;
const sizesArray = getSizesArray(bid, mediaType);
const gpid = deepAccess(bid, `ortb2Imp.ext.gpid`);
const pos = deepAccess(bid, `mediaTypes.${mediaType}.pos`);
const placementId = params.placementId || deepAccess(bid, `mediaTypes.${mediaType}.name`);
const paramsFloorPrice = isNaN(params.floorPrice) ? 0 : params.floorPrice;
const bidObject = {
adUnitCode: getBidIdParameter('adUnitCode', bid),
bidId: getBidIdParameter('bidId', bid),
loop: getBidIdParameter('bidderRequestsCount', bid),
bidderRequestId: getBidIdParameter('bidderRequestId', bid),
floorPrice: Math.max(getFloorPrice(bid, mediaType), paramsFloorPrice),
mediaType,
sizes: sizesArray,
transactionId: bid.ortb2Imp?.ext?.tid || ''
};
if (pos) {
bidObject.pos = pos;
}
if (gpid) {
bidObject.gpid = gpid;
}
if (placementId) {
bidObject.placementId = placementId;
}
if (mediaType === VIDEO) {
populateVideoParams(bidObject, bid);
}
return bidObject;
}
/**
* Checks if the media type is a banner
* @param bid {bid}
* @returns {boolean}
*/
function isBanner(bid) {
return bid.mediaTypes && bid.mediaTypes.banner;
}
/**
* Generate params that are common between all bids
* @param sharedParams {sharedParams}
* @param bidderRequest {bidderRequest}
* @returns {object} the common params object
*/
function generateSharedParams(sharedParams, bidderRequest) {
const {bidderCode} = bidderRequest;
const {syncEnabled, filterSettings} = config.getConfig('userSync') || {};
const domain = window.location.hostname;
const generalBidParams = getBidIdParameter('params', sharedParams);
const userIds = getBidIdParameter('userId', sharedParams);
const ortb2Metadata = bidderRequest.ortb2 || {};
const timeout = bidderRequest.timeout;
const params = {
adapter_version: VERSION,
auction_start: timestamp(),
device_type: getDeviceType(),
dnt: (navigator.doNotTrack === 'yes' || navigator.doNotTrack === '1' || navigator.msDoNotTrack === '1') ? 1 : 0,
publisher_id: generalBidParams.org,
publisher_name: domain,
session_id: getBidIdParameter('auctionId', sharedParams),
site_domain: domain,
tmax: timeout,
ua: navigator.userAgent,
wrapper_type: 'prebidjs',
wrapper_vendor: '$$PREBID_GLOBAL$$',
wrapper_version: '$prebid.version$'
};
if (syncEnabled) {
const allowedSyncMethod = getSyncMethod(filterSettings, bidderCode);
if (allowedSyncMethod) {
params.cs_method = allowedSyncMethod;
}
}
if (bidderRequest && bidderRequest.gdprConsent && bidderRequest.gdprConsent.gdprApplies) {
params.gdpr = bidderRequest.gdprConsent.gdprApplies;
params.gdpr_consent = bidderRequest.gdprConsent.consentString;
}
if (bidderRequest.uspConsent) {
params.us_privacy = bidderRequest.uspConsent;
}
if (generalBidParams.ifa) {
params.ifa = generalBidParams.ifa;
}
if (ortb2Metadata.site) {
params.site_metadata = JSON.stringify(ortb2Metadata.site);
}
if (ortb2Metadata.user) {
params.user_metadata = JSON.stringify(ortb2Metadata.user);
}
if (bidderRequest && bidderRequest.refererInfo) {
params.referrer = deepAccess(bidderRequest, 'refererInfo.ref');
params.page_url = deepAccess(bidderRequest, 'refererInfo.page') || deepAccess(window, 'location.href');
}
if (sharedParams.schain) {
params.schain = getSupplyChain(sharedParams.schain);
}
if (userIds) {
params.userIds = JSON.stringify(userIds);
}
return params;
}
/**
* Validates the bidder params
* @param bidRequest {bidRequest}
* @returns {boolean}
*/
function validateParams(bidRequest) {
let isValid = true;
if (!bidRequest.params) {
logWarn('Kueez adapter - missing params');
isValid = false;
}
if (!bidRequest.params.org) {
logWarn('Kueez adapter - org is a required param');
isValid = false;
}
return isValid;
}
/**
* Validates the bidder params
* @param validBidRequests {Array}
* @param sharedParams {sharedParams}
* @param bidderRequest {bidderRequest}
* @returns {Object}
*/
function prepareBids(validBidRequests, sharedParams, bidderRequest) {
return {
params: generateSharedParams(sharedParams, bidderRequest),
bids: generateBidParams(validBidRequests, bidderRequest)
}
}
function getPlaybackMethod(bid) {
const playbackMethod = deepAccess(bid, `mediaTypes.video.playbackmethod`);
if (Array.isArray(playbackMethod) && isInteger(playbackMethod[0])) {
return playbackMethod[0];
} else if (isInteger(playbackMethod)) {
return playbackMethod;
}
}
function populateVideoParams(params, bid) {
const linearity = deepAccess(bid, `mediaTypes.video.linearity`);
const maxDuration = deepAccess(bid, `mediaTypes.video.maxduration`);
const minDuration = deepAccess(bid, `mediaTypes.video.minduration`);
const placement = deepAccess(bid, `mediaTypes.video.placement`);
const plcmt = deepAccess(bid, `mediaTypes.video.plcmt`);
const playbackMethod = getPlaybackMethod(bid);
const skip = deepAccess(bid, `mediaTypes.video.skip`);
if (linearity) {
params.linearity = linearity;
}
if (maxDuration) {
params.maxDuration = maxDuration;
}
if (minDuration) {
params.minDuration = minDuration;
}
if (placement) {
params.placement = placement;
}
if (plcmt) {
params.plcmt = plcmt;
}
if (playbackMethod) {
params.playbackMethod = playbackMethod;
}
if (skip) {
params.skip = skip;
}
}
/**
* Processes the bid responses
* @param bids {Array}
* @returns {Array}
*/
function parseBidResponses(bids) {
return bids.map(bid => {
const bidResponse = {
cpm: bid.cpm,
creativeId: bid.requestId,
currency: bid.currency || MAIN_CURRENCY,
height: bid.height,
mediaType: bid.mediaType,
meta: {
mediaType: bid.mediaType
},
netRevenue: bid.netRevenue || true,
nurl: bid.nurl,
requestId: bid.requestId,
ttl: bid.ttl || TTL,
width: bid.width
};
if (bid.adomain && bid.adomain.length) {
bidResponse.meta.advertiserDomains = bid.adomain;
}
if (bid.mediaType === VIDEO) {
bidResponse.vastXml = bid.vastXml;
} else if (bid.mediaType === BANNER) {
bidResponse.ad = bid.ad;
}
return bidResponse;
});
}