forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpubmaticAnalyticsAdapter.js
executable file
·735 lines (668 loc) · 25.5 KB
/
pubmaticAnalyticsAdapter.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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
import {_each, isArray, isStr, logError, logWarn, pick, generateUUID} from '../src/utils.js';
import adapter from '../libraries/analyticsAdapter/AnalyticsAdapter.js';
import adapterManager from '../src/adapterManager.js';
import { BID_STATUS, EVENTS, STATUS, REJECTION_REASON } from '../src/constants.js';
import {ajax} from '../src/ajax.js';
import {config} from '../src/config.js';
import {getGlobal} from '../src/prebidGlobal.js';
import {getGptSlotInfoForAdUnitCode} from '../libraries/gptUtils/gptUtils.js';
const FLOOR_VALUES = {
NO_DATA: 'noData',
AD_UNIT: 'adUnit',
SET_CONFIG: 'setConfig',
FETCH: 'fetch',
SUCCESS: 'success',
ERROR: 'error',
TIMEOUT: 'timeout'
};
/// /////////// CONSTANTS //////////////
const ADAPTER_CODE = 'pubmatic';
const VENDOR_OPENWRAP = 'openwrap';
const SEND_TIMEOUT = 2000;
const END_POINT_HOST = 'https://t.pubmatic.com/';
const END_POINT_BID_LOGGER = END_POINT_HOST + 'wl?';
const END_POINT_WIN_BID_LOGGER = END_POINT_HOST + 'wt?';
const LOG_PRE_FIX = 'PubMatic-Analytics: ';
const cache = {
auctions: {}
};
const SUCCESS = 'success';
const NO_BID = 'no-bid';
const ERROR = 'error';
const REQUEST_ERROR = 'request-error';
const TIMEOUT_ERROR = 'timeout-error';
const EMPTY_STRING = '';
const OPEN_AUCTION_DEAL_ID = '-1';
const MEDIA_TYPE_BANNER = 'banner';
const CURRENCY_USD = 'USD';
const BID_PRECISION = 2;
// todo: input profileId and profileVersionId ; defaults to zero or one
const DEFAULT_PUBLISHER_ID = 0;
const DEFAULT_PROFILE_ID = 0;
const DEFAULT_PROFILE_VERSION_ID = 0;
const enc = window.encodeURIComponent;
const MEDIATYPE = {
BANNER: 0,
VIDEO: 1,
NATIVE: 2
}
/// /////////// VARIABLES //////////////
let publisherId = DEFAULT_PUBLISHER_ID; // int: mandatory
let profileId = DEFAULT_PROFILE_ID; // int: optional
let profileVersionId = DEFAULT_PROFILE_VERSION_ID; // int: optional
let s2sBidders = [];
/// /////////// HELPER FUNCTIONS //////////////
function sizeToDimensions(size) {
return {
width: size.w || size[0],
height: size.h || size[1]
};
}
function validMediaType(type) {
return ({'banner': 1, 'native': 1, 'video': 1}).hasOwnProperty(type);
}
function formatSource(src) {
if (typeof src === 'undefined') {
src = 'client';
} else if (src === 's2s') {
src = 'server';
}
return src.toLowerCase();
}
function setMediaTypes(types, bid) {
if (bid.mediaType && validMediaType(bid.mediaType)) {
return [bid.mediaType];
}
if (Array.isArray(types)) {
return types.filter(validMediaType);
}
if (typeof types === 'object') {
if (!bid.sizes) {
bid.dimensions = [];
_each(types, (type) =>
bid.dimensions = bid.dimensions.concat(
type.sizes.map(sizeToDimensions)
)
);
}
return Object.keys(types).filter(validMediaType);
}
return [MEDIA_TYPE_BANNER];
}
function copyRequiredBidDetails(bid) {
return pick(bid, [
'bidder',
'bidderCode',
'adapterCode',
'bidId',
'status', () => NO_BID, // default a bid to NO_BID until response is received or bid is timed out
'finalSource as source',
'params',
'floorData',
'adUnit', () => pick(bid, [
'adUnitCode',
'transactionId',
'sizes as dimensions', sizes => sizes && sizes.map(sizeToDimensions),
'mediaTypes', (types) => setMediaTypes(types, bid)
])
]);
}
function setBidStatus(bid, args) {
switch (args.getStatusCode()) {
case STATUS.GOOD:
bid.status = SUCCESS;
delete bid.error; // it's possible for this to be set by a previous timeout
break;
default:
bid.status = ERROR;
bid.error = {
code: REQUEST_ERROR
};
}
}
function parseBidResponse(bid) {
return pick(bid, [
'bidPriceUSD', () => {
// todo: check whether currency cases are handled here
if (typeof bid.currency === 'string' && bid.currency.toUpperCase() === CURRENCY_USD) {
return window.parseFloat(Number(bid.cpm).toFixed(BID_PRECISION));
}
// use currency conversion function if present
if (typeof bid.getCpmInNewCurrency === 'function') {
return window.parseFloat(Number(bid.getCpmInNewCurrency(CURRENCY_USD)).toFixed(BID_PRECISION));
}
logWarn(LOG_PRE_FIX + 'Could not determine the Net cpm in USD for the bid thus using bid.cpm', bid);
return bid.cpm
},
'bidGrossCpmUSD', () => {
if (typeof bid.originalCurrency === 'string' && bid.originalCurrency.toUpperCase() === CURRENCY_USD) {
return window.parseFloat(Number(bid.originalCpm).toFixed(BID_PRECISION));
}
// use currency conversion function if present
if (typeof getGlobal().convertCurrency === 'function') {
return window.parseFloat(Number(getGlobal().convertCurrency(bid.originalCpm, bid.originalCurrency, CURRENCY_USD)).toFixed(BID_PRECISION));
}
logWarn(LOG_PRE_FIX + 'Could not determine the Gross cpm in USD for the bid, thus using bid.originalCpm', bid);
return bid.originalCpm
},
'dealId',
'currency',
'cpm', () => window.parseFloat(Number(bid.cpm).toFixed(BID_PRECISION)),
'originalCpm', () => window.parseFloat(Number(bid.originalCpm).toFixed(BID_PRECISION)),
'originalCurrency',
'adserverTargeting',
'dealChannel',
'meta',
'status',
'error',
'bidId',
'mediaType',
'params',
'floorData',
'mi',
'regexPattern', () => bid.regexPattern || undefined,
'partnerImpId', // partner impression ID
'dimensions', () => pick(bid, [
'width',
'height'
])
]);
}
function getDomainFromUrl(url) {
let a = window.document.createElement('a');
a.href = url;
return a.hostname;
}
function getDevicePlatform() {
var deviceType = 3;
try {
var ua = navigator.userAgent;
if (ua && isStr(ua) && ua.trim() != '') {
ua = ua.toLowerCase().trim();
var isMobileRegExp = new RegExp('(mobi|tablet|ios).*');
if (ua.match(isMobileRegExp)) {
deviceType = 2;
} else {
deviceType = 1;
}
}
} catch (ex) {}
return deviceType;
}
function getValueForKgpv(bid, adUnitId) {
if (bid.params && bid.params.regexPattern) {
return bid.params.regexPattern;
} else if (bid.bidResponse && bid.bidResponse.regexPattern) {
return bid.bidResponse.regexPattern;
} else if (bid.params && bid.params.kgpv) {
return bid.params.kgpv;
} else {
return adUnitId;
}
}
function getAdapterNameForAlias(aliasName) {
return adapterManager.aliasRegistry[aliasName] || aliasName;
}
function getAdDomain(bidResponse) {
if (bidResponse.meta && bidResponse.meta.advertiserDomains) {
let adomain = bidResponse.meta.advertiserDomains[0]
if (adomain) {
try {
let hostname = (new URL(adomain));
return hostname.hostname.replace('www.', '');
} catch (e) {
logWarn(LOG_PRE_FIX + 'Adomain URL (Not a proper URL):', adomain);
return adomain.replace('www.', '');
}
}
}
}
function isObject(object) {
return typeof object === 'object' && object !== null;
};
function isEmptyObject(object) {
return isObject(object) && Object.keys(object).length === 0;
};
/**
* Prepare meta object to pass in logger call
* @param {*} meta
*/
export function getMetadata(meta) {
if (!meta || isEmptyObject(meta)) return;
const metaObj = {};
if (meta.networkId) metaObj.nwid = meta.networkId;
if (meta.advertiserId) metaObj.adid = meta.advertiserId;
if (meta.networkName) metaObj.nwnm = meta.networkName;
if (meta.primaryCatId) metaObj.pcid = meta.primaryCatId;
if (meta.advertiserName) metaObj.adnm = meta.advertiserName;
if (meta.agencyId) metaObj.agid = meta.agencyId;
if (meta.agencyName) metaObj.agnm = meta.agencyName;
if (meta.brandId) metaObj.brid = meta.brandId;
if (meta.brandName) metaObj.brnm = meta.brandName;
if (meta.dchain) metaObj.dc = meta.dchain;
if (meta.demandSource) metaObj.ds = meta.demandSource;
if (meta.secondaryCatIds) metaObj.scids = meta.secondaryCatIds;
if (isEmptyObject(metaObj)) return;
return metaObj;
}
function isS2SBidder(bidder) {
return (s2sBidders.indexOf(bidder) > -1) ? 1 : 0
}
function isOWPubmaticBid(adapterName) {
let s2sConf = config.getConfig('s2sConfig');
let s2sConfArray = isArray(s2sConf) ? s2sConf : [s2sConf];
return s2sConfArray.some(conf => {
if (adapterName === ADAPTER_CODE && conf.defaultVendor === VENDOR_OPENWRAP &&
conf.bidders.indexOf(ADAPTER_CODE) > -1) {
return true;
}
})
}
function gatherPartnerBidsForAdUnitForLogger(adUnit, adUnitId, highestBid, e) {
highestBid = (highestBid && highestBid.length > 0) ? highestBid[0] : null;
return Object.keys(adUnit.bids).reduce(function(partnerBids, bidId) {
adUnit.bids[bidId].forEach(function(bid) {
let adapterName = getAdapterNameForAlias(bid.adapterCode || bid.bidder);
if (isOWPubmaticBid(adapterName) && isS2SBidder(bid.bidder)) {
return;
}
const pg = window.parseFloat(Number(bid.bidResponse?.adserverTargeting?.hb_pb || bid.bidResponse?.adserverTargeting?.pwtpb).toFixed(BID_PRECISION));
const prebidBidsReceived = e?.bidsReceived;
if (isArray(prebidBidsReceived) && prebidBidsReceived.length > 0) {
prebidBidsReceived.forEach(function(iBid) {
if (iBid.adId === bid.adId) {
bid.bidderCode = iBid.bidderCode;
}
});
}
partnerBids.push({
'pn': adapterName,
'bc': bid.bidderCode || bid.bidder,
'bidid': bid.bidId || bidId,
'origbidid': bid?.bidResponse?.partnerImpId || bid?.bidResponse?.prebidBidId || bid.bidId || bidId,
'db': bid.bidResponse ? 0 : 1,
'kgpv': getValueForKgpv(bid, adUnitId),
'kgpsv': bid.params && bid.params.kgpv ? bid.params.kgpv : adUnitId,
'psz': bid.bidResponse ? (bid.bidResponse.dimensions.width + 'x' + bid.bidResponse.dimensions.height) : '0x0',
'eg': bid.bidResponse ? bid.bidResponse.bidGrossCpmUSD : 0,
'en': bid.bidResponse ? bid.bidResponse.bidPriceUSD : 0,
'di': bid.bidResponse ? (bid.bidResponse.dealId || OPEN_AUCTION_DEAL_ID) : OPEN_AUCTION_DEAL_ID,
'dc': bid.bidResponse ? (bid.bidResponse.dealChannel || EMPTY_STRING) : EMPTY_STRING,
'l1': bid.bidResponse ? bid.partnerTimeToRespond : 0,
'ol1': bid.bidResponse ? bid.clientLatencyTimeMs : 0,
'l2': 0,
'adv': bid.bidResponse ? getAdDomain(bid.bidResponse) || undefined : undefined,
'ss': isS2SBidder(bid.bidder),
't': (bid.status == ERROR && bid.error.code == TIMEOUT_ERROR) ? 1 : 0,
'wb': (highestBid && highestBid.adId === bid.adId ? 1 : 0),
'mi': bid.bidResponse ? (bid.bidResponse.mi || undefined) : undefined,
'af': bid.bidResponse ? (bid.bidResponse.mediaType || undefined) : undefined,
'ocpm': bid.bidResponse ? (bid.bidResponse.originalCpm || 0) : 0,
'ocry': bid.bidResponse ? (bid.bidResponse.originalCurrency || CURRENCY_USD) : CURRENCY_USD,
'frv': bid.bidResponse ? bid.bidResponse.floorData?.floorRuleValue : undefined,
'md': bid.bidResponse ? getMetadata(bid.bidResponse.meta) : undefined,
'pb': pg || undefined
});
});
return partnerBids;
}, [])
}
function getSizesForAdUnit(adUnit) {
var bid = Object.values(adUnit.bids).filter((bid) => !!bid.bidResponse && bid.bidResponse.mediaType === 'native')[0];
if (!!bid || (bid === undefined && adUnit.dimensions.length === 0)) {
return ['1x1'];
} else {
return adUnit.dimensions.map(function (e) {
return e[0] + 'x' + e[1];
})
}
}
function getAdUnitAdFormats(adUnit) {
var af = adUnit ? Object.keys(adUnit.mediaTypes || {}).map(format => MEDIATYPE[format.toUpperCase()]) : [];
return af;
}
function getAdUnit(adUnits, adUnitId) {
return adUnits.filter(adUnit => (adUnit.divID && adUnit.divID == adUnitId) || (adUnit.code == adUnitId))[0];
}
function getTgId() {
var testGroupId = parseInt(config.getConfig('testGroupId') || 0);
if (testGroupId <= 15 && testGroupId >= 0) {
return testGroupId;
}
return 0;
}
function getFloorFetchStatus(floorData) {
if (!floorData?.floorRequestData) {
return false;
}
const { location, fetchStatus } = floorData?.floorRequestData;
const isDataValid = location !== FLOOR_VALUES.NO_DATA;
const isFetchSuccessful = location === FLOOR_VALUES.FETCH && fetchStatus === FLOOR_VALUES.SUCCESS;
const isAdUnitOrSetConfig = location === FLOOR_VALUES.AD_UNIT || location === FLOOR_VALUES.SET_CONFIG;
return isDataValid && (isAdUnitOrSetConfig || isFetchSuccessful);
}
function executeBidsLoggerCall(e, highestCpmBids) {
let auctionId = e.auctionId;
let referrer = config.getConfig('pageUrl') || cache.auctions[auctionId].referer || '';
let auctionCache = cache.auctions[auctionId];
let wiid = auctionCache?.wiid || auctionId;
let floorData = auctionCache?.floorData;
let floorFetchStatus = getFloorFetchStatus(auctionCache?.floorData);
let outputObj = { s: [] };
let pixelURL = END_POINT_BID_LOGGER;
if (!auctionCache) {
return;
}
if (auctionCache.sent) {
return;
}
pixelURL += 'pubid=' + publisherId;
outputObj['pubid'] = '' + publisherId;
outputObj['iid'] = '' + wiid;
outputObj['to'] = '' + auctionCache.timeout;
outputObj['purl'] = referrer;
outputObj['orig'] = getDomainFromUrl(referrer);
outputObj['tst'] = Math.round((new window.Date()).getTime() / 1000);
outputObj['pid'] = '' + profileId;
outputObj['pdvid'] = '' + profileVersionId;
outputObj['dvc'] = {'plt': getDevicePlatform()};
outputObj['tgid'] = getTgId();
outputObj['pbv'] = '$prebid.version$' || '-1';
if (floorData && floorFetchStatus) {
outputObj['fmv'] = floorData.floorRequestData ? floorData.floorRequestData.modelVersion || undefined : undefined;
outputObj['ft'] = floorData.floorResponseData ? (floorData.floorResponseData.enforcements.enforceJS == false ? 0 : 1) : undefined;
}
outputObj.s = Object.keys(auctionCache.adUnitCodes).reduce(function(slotsArray, adUnitId) {
let adUnit = auctionCache.adUnitCodes[adUnitId];
let origAdUnit = getAdUnit(auctionCache.origAdUnits, adUnitId) || {};
// getGptSlotInfoForAdUnitCode returns gptslot corresponding to adunit provided as input.
let slotObject = {
'sn': adUnitId,
'au': origAdUnit.owAdUnitId || getGptSlotInfoForAdUnitCode(adUnitId)?.gptSlot || adUnitId,
'mt': getAdUnitAdFormats(origAdUnit),
'sz': getSizesForAdUnit(adUnit, adUnitId),
'ps': gatherPartnerBidsForAdUnitForLogger(adUnit, adUnitId, highestCpmBids.filter(bid => bid.adUnitCode === adUnitId), e),
'fskp': floorData && floorFetchStatus ? (floorData.floorRequestData ? (floorData.floorRequestData.skipped == false ? 0 : 1) : undefined) : undefined,
'sid': generateUUID()
};
if (floorData?.floorRequestData) {
const { location, fetchStatus, floorProvider } = floorData?.floorRequestData;
slotObject.ffs = {
[FLOOR_VALUES.SUCCESS]: 1,
[FLOOR_VALUES.ERROR]: 2,
[FLOOR_VALUES.TIMEOUT]: 4,
undefined: 0
}[fetchStatus];
slotObject.fsrc = {
[FLOOR_VALUES.FETCH]: 2,
[FLOOR_VALUES.NO_DATA]: 2,
[FLOOR_VALUES.AD_UNIT]: 1,
[FLOOR_VALUES.SET_CONFIG]: 1
}[location];
slotObject.fp = floorProvider;
}
slotsArray.push(slotObject);
return slotsArray;
}, []);
auctionCache.sent = true;
ajax(
pixelURL,
null,
'json=' + enc(JSON.stringify(outputObj)),
{
contentType: 'application/x-www-form-urlencoded',
withCredentials: true,
method: 'POST'
}
);
}
function executeBidWonLoggerCall(auctionId, adUnitId) {
const winningBidId = cache.auctions[auctionId].adUnitCodes[adUnitId].bidWon;
const winningBids = cache.auctions[auctionId].adUnitCodes[adUnitId].bids[winningBidId];
if (!winningBids) {
logWarn(LOG_PRE_FIX + 'Could not find winningBids for : ', auctionId);
return;
}
let winningBid = winningBids[0];
if (winningBids.length > 1) {
winningBid = winningBids.filter(bid => bid.adId === cache.auctions[auctionId].adUnitCodes[adUnitId].bidWonAdId)[0];
}
const adapterName = getAdapterNameForAlias(winningBid.adapterCode || winningBid.bidder);
if (isOWPubmaticBid(adapterName) && isS2SBidder(winningBid.bidder)) {
return;
}
let origAdUnit = getAdUnit(cache.auctions[auctionId].origAdUnits, adUnitId) || {};
let owAdUnitId = origAdUnit.owAdUnitId || getGptSlotInfoForAdUnitCode(adUnitId)?.gptSlot || adUnitId;
let auctionCache = cache.auctions[auctionId];
let floorData = auctionCache.floorData;
let wiid = cache.auctions[auctionId]?.wiid || auctionId;
let referrer = config.getConfig('pageUrl') || cache.auctions[auctionId].referer || '';
let adv = winningBid.bidResponse ? getAdDomain(winningBid.bidResponse) || undefined : undefined;
let fskp = floorData ? (floorData.floorRequestData ? (floorData.floorRequestData.skipped == false ? 0 : 1) : undefined) : undefined;
let pg = window.parseFloat(Number(winningBid?.bidResponse?.adserverTargeting?.hb_pb || winningBid?.bidResponse?.adserverTargeting?.pwtpb)) || undefined;
let pixelURL = END_POINT_WIN_BID_LOGGER;
pixelURL += 'pubid=' + publisherId;
pixelURL += '&purl=' + enc(config.getConfig('pageUrl') || cache.auctions[auctionId].referer || '');
pixelURL += '&tst=' + Math.round((new window.Date()).getTime() / 1000);
pixelURL += '&iid=' + enc(wiid);
pixelURL += '&bidid=' + enc(winningBidId);
pixelURL += '&pid=' + enc(profileId);
pixelURL += '&pdvid=' + enc(profileVersionId);
pixelURL += '&slot=' + enc(adUnitId);
pixelURL += '&au=' + enc(owAdUnitId);
pixelURL += '&pn=' + enc(adapterName);
pixelURL += '&bc=' + enc(winningBid.bidderCode || winningBid.bidder);
pixelURL += '&en=' + enc(winningBid.bidResponse.bidPriceUSD);
pixelURL += '&eg=' + enc(winningBid.bidResponse.bidGrossCpmUSD);
pixelURL += '&kgpv=' + enc(getValueForKgpv(winningBid, adUnitId));
pixelURL += '&origbidid=' + enc(winningBid?.bidResponse?.partnerImpId || winningBid?.bidResponse?.prebidBidId || winningBid.bidId);
pixelURL += '&di=' + enc(winningBid?.bidResponse?.dealId || OPEN_AUCTION_DEAL_ID);
pg && (pixelURL += '&pb=' + enc(pg));
pixelURL += '&plt=' + enc(getDevicePlatform());
pixelURL += '&psz=' + enc((winningBid?.bidResponse?.dimensions?.width || '0') + 'x' +
(winningBid?.bidResponse?.dimensions?.height || '0'));
pixelURL += '&tgid=' + enc(getTgId());
adv && (pixelURL += '&adv=' + enc(adv));
pixelURL += '&orig=' + enc(getDomainFromUrl(referrer));
pixelURL += '&ss=' + enc(isS2SBidder(winningBid.bidder));
(fskp != undefined) && (pixelURL += '&fskp=' + enc(fskp));
pixelURL += '&af=' + enc(winningBid.bidResponse ? (winningBid.bidResponse.mediaType || undefined) : undefined);
ajax(
pixelURL,
null,
null,
{
contentType: 'application/x-www-form-urlencoded',
withCredentials: true,
method: 'GET'
}
);
}
/// /////////// ADAPTER EVENT HANDLER FUNCTIONS //////////////
function auctionInitHandler(args) {
s2sBidders = (function() {
let s2sConf = config.getConfig('s2sConfig');
let s2sBidders = [];
(s2sConf || []) &&
isArray(s2sConf) ? s2sConf.map(conf => s2sBidders.push(...conf.bidders)) : s2sBidders.push(...s2sConf.bidders);
return s2sBidders || [];
}());
let cacheEntry = pick(args, [
'timestamp',
'timeout',
'bidderDonePendingCount', () => args.bidderRequests.length,
]);
cacheEntry.adUnitCodes = {};
cacheEntry.floorData = {};
cacheEntry.origAdUnits = args.adUnits;
cacheEntry.referer = args.bidderRequests[0].refererInfo.topmostLocation;
cache.auctions[args.auctionId] = cacheEntry;
}
function bidRequestedHandler(args) {
args.bids.forEach(function(bid) {
if (!cache.auctions[args.auctionId].adUnitCodes.hasOwnProperty(bid.adUnitCode)) {
cache.auctions[args.auctionId].adUnitCodes[bid.adUnitCode] = {
bids: {},
bidWon: false,
dimensions: bid.sizes
};
}
if (bid.bidder === 'pubmatic' && !!bid?.params?.wiid) {
cache.auctions[args.auctionId].wiid = bid.params.wiid;
}
cache.auctions[args.auctionId].adUnitCodes[bid.adUnitCode].bids[bid.bidId] = [copyRequiredBidDetails(bid)];
if (bid.floorData) {
cache.auctions[args.auctionId].floorData['floorRequestData'] = bid.floorData;
}
})
}
function bidResponseHandler(args) {
if (!args.requestId) {
logWarn(LOG_PRE_FIX + 'Got null requestId in bidResponseHandler');
return;
}
let requestId = args.originalRequestId || args.requestId;
let bid = cache.auctions[args.auctionId].adUnitCodes[args.adUnitCode].bids[requestId][0];
if (!bid) {
logError(LOG_PRE_FIX + 'Could not find associated bid request for bid response with requestId: ', args.requestId);
return;
}
if ((bid.bidder && args.bidderCode && bid.bidder !== args.bidderCode) || (bid.bidder === args.bidderCode && bid.status === SUCCESS)) {
bid = copyRequiredBidDetails(args);
cache.auctions[args.auctionId].adUnitCodes[args.adUnitCode].bids[requestId].push(bid);
} else if (args.originalRequestId) {
bid.bidId = args.requestId;
}
if (args.floorData) {
cache.auctions[args.auctionId].floorData['floorResponseData'] = args.floorData;
}
bid.adId = args.adId;
bid.source = formatSource(bid.source || args.source);
setBidStatus(bid, args);
const latency = args?.timeToRespond || Date.now() - cache.auctions[args.auctionId].timestamp;
const auctionTime = cache.auctions[args.auctionId].timeout;
// Check if latency is greater than auctiontime+150, then log auctiontime+150 to avoid large numbers
bid.partnerTimeToRespond = latency > (auctionTime + 150) ? (auctionTime + 150) : latency;
bid.clientLatencyTimeMs = Date.now() - cache.auctions[args.auctionId].timestamp;
bid.bidResponse = parseBidResponse(args);
}
function bidRejectedHandler(args) {
// If bid is rejected due to floors value did not met
// make cpm as 0, status as bidRejected and forward the bid for logging
if (args.rejectionReason === REJECTION_REASON.FLOOR_NOT_MET) {
args.cpm = 0;
args.status = BID_STATUS.BID_REJECTED;
bidResponseHandler(args);
}
}
function bidderDoneHandler(args) {
cache.auctions[args.auctionId].bidderDonePendingCount--;
args.bids.forEach(bid => {
let cachedBid = cache.auctions[bid.auctionId].adUnitCodes[bid.adUnitCode].bids[bid.bidId || bid.originalRequestId || bid.requestId];
if (typeof bid.serverResponseTimeMs !== 'undefined') {
cachedBid.serverLatencyTimeMs = bid.serverResponseTimeMs;
}
if (!cachedBid.status) {
cachedBid.status = NO_BID;
}
if (!cachedBid.clientLatencyTimeMs) {
cachedBid.clientLatencyTimeMs = Date.now() - cache.auctions[bid.auctionId].timestamp;
}
});
}
function bidWonHandler(args) {
let auctionCache = cache.auctions[args.auctionId];
auctionCache.adUnitCodes[args.adUnitCode].bidWon = args.originalRequestId || args.requestId;
auctionCache.adUnitCodes[args.adUnitCode].bidWonAdId = args.adId;
executeBidWonLoggerCall(args.auctionId, args.adUnitCode);
}
function auctionEndHandler(args) {
// if for the given auction bidderDonePendingCount == 0 then execute logger call sooners
let highestCpmBids = getGlobal().getHighestCpmBids() || [];
setTimeout(() => {
executeBidsLoggerCall.call(this, args, highestCpmBids);
}, (cache.auctions[args.auctionId]?.bidderDonePendingCount === 0 ? 500 : SEND_TIMEOUT));
}
function bidTimeoutHandler(args) {
// db = 1 and t = 1 means bidder did NOT respond with a bid but we got a timeout notification
// db = 0 and t = 1 means bidder did respond with a bid but post timeout
args.forEach(badBid => {
let auctionCache = cache.auctions[badBid.auctionId];
let bid = auctionCache.adUnitCodes[badBid.adUnitCode].bids[ badBid.bidId || badBid.originalRequestId || badBid.requestId ][0];
if (bid) {
bid.status = ERROR;
bid.error = {
code: TIMEOUT_ERROR
};
} else {
logWarn(LOG_PRE_FIX + 'bid not found');
}
});
}
/// /////////// ADAPTER DEFINITION //////////////
let baseAdapter = adapter({analyticsType: 'endpoint'});
let pubmaticAdapter = Object.assign({}, baseAdapter, {
enableAnalytics(conf = {}) {
let error = false;
if (typeof conf.options === 'object') {
if (conf.options.publisherId) {
publisherId = Number(conf.options.publisherId);
}
profileId = Number(conf.options.profileId) || DEFAULT_PROFILE_ID;
profileVersionId = Number(conf.options.profileVersionId) || DEFAULT_PROFILE_VERSION_ID;
} else {
logError(LOG_PRE_FIX + 'Config not found.');
error = true;
}
if (!publisherId) {
logError(LOG_PRE_FIX + 'Missing publisherId(Number).');
error = true;
}
if (error) {
logError(LOG_PRE_FIX + 'Not collecting data due to error(s).');
} else {
baseAdapter.enableAnalytics.call(this, conf);
}
},
disableAnalytics() {
publisherId = DEFAULT_PUBLISHER_ID;
profileId = DEFAULT_PROFILE_ID;
profileVersionId = DEFAULT_PROFILE_VERSION_ID;
s2sBidders = [];
baseAdapter.disableAnalytics.apply(this, arguments);
},
track({eventType, args}) {
switch (eventType) {
case EVENTS.AUCTION_INIT:
auctionInitHandler(args);
break;
case EVENTS.BID_REQUESTED:
bidRequestedHandler(args);
break;
case EVENTS.BID_RESPONSE:
bidResponseHandler(args);
break;
case EVENTS.BID_REJECTED:
bidRejectedHandler(args)
break;
case EVENTS.BIDDER_DONE:
bidderDoneHandler(args);
break;
case EVENTS.BID_WON:
bidWonHandler(args);
break;
case EVENTS.AUCTION_END:
auctionEndHandler(args);
break;
case EVENTS.BID_TIMEOUT:
bidTimeoutHandler(args);
break;
}
}
});
/// /////////// ADAPTER REGISTRATION //////////////
adapterManager.registerAnalyticsAdapter({
adapter: pubmaticAdapter,
code: ADAPTER_CODE
});
export default pubmaticAdapter;