forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
marsmediaBidAdapter.js
444 lines (383 loc) · 12.5 KB
/
marsmediaBidAdapter.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
'use strict';
import { deepAccess, getDNT, parseSizesInput, isArray, getWindowTop, deepSetValue, triggerPixel, getWindowSelf } from '../src/utils.js';
import {registerBidder} from '../src/adapters/bidderFactory.js';
import { BANNER, VIDEO } from '../src/mediaTypes.js';
import {config} from '../src/config.js';
function MarsmediaAdapter() {
this.code = 'marsmedia';
this.aliases = ['mars'];
this.supportedMediaTypes = [VIDEO, BANNER];
let SUPPORTED_VIDEO_PROTOCOLS = [2, 3, 5, 6];
let SUPPORTED_VIDEO_MIMES = ['video/mp4'];
let SUPPORTED_VIDEO_PLAYBACK_METHODS = [1, 2, 3, 4];
let SUPPORTED_VIDEO_DELIVERY = [1];
let SUPPORTED_VIDEO_API = [1, 2, 5];
let slotsToBids = {};
let version = '2.5';
this.isBidRequestValid = function (bid) {
return !!(bid.params && bid.params.zoneId);
};
this.getUserSyncs = function (syncOptions, responses, gdprConsent) {
return [];
};
function frameImp(BRs, bidderRequest) {
var impList = [];
var isSecure = 0;
if (bidderRequest && bidderRequest.refererInfo && bidderRequest.refererInfo.stack.length) {
// clever trick to get the protocol
// TODO: this should probably use parseUrl
var el = document.createElement('a');
el.href = bidderRequest.refererInfo.stack[0];
isSecure = (el.protocol == 'https:') ? 1 : 0;
}
for (var i = 0; i < BRs.length; i++) {
slotsToBids[BRs[i].adUnitCode] = BRs[i];
var impObj = {};
impObj.id = BRs[i].adUnitCode;
impObj.secure = isSecure;
if (deepAccess(BRs[i], 'mediaTypes.banner') || deepAccess(BRs[i], 'mediaType') === 'banner') {
let banner = frameBanner(BRs[i]);
if (banner) {
impObj.banner = banner;
}
}
if (deepAccess(BRs[i], 'mediaTypes.video') || deepAccess(BRs[i], 'mediaType') === 'video') {
impObj.video = frameVideo(BRs[i]);
}
if (!(impObj.banner || impObj.video)) {
continue;
}
impObj.bidfloor = _getFloor(BRs[i]);
impObj.ext = frameExt(BRs[i]);
impList.push(impObj);
}
return impList;
}
function frameSite(bidderRequest) {
var site = {
domain: '',
page: '',
ref: ''
}
if (bidderRequest && bidderRequest.refererInfo) {
var ri = bidderRequest.refererInfo;
// TODO: is 'ref' the right value here?
site.ref = ri.ref;
if (ri.stack.length) {
site.page = ri.stack[ri.stack.length - 1];
// clever trick to get the domain
// TODO: does this logic make sense? why should domain be set to the lowermost frame's?
// TODO: this should probably use parseUrl
var el = document.createElement('a');
el.href = ri.stack[0];
site.domain = el.hostname;
}
}
return site;
}
function frameDevice() {
return {
ua: navigator.userAgent,
ip: '', // Empty Ip string is required, server gets the ip from HTTP header
dnt: getDNT() ? 1 : 0,
}
}
function getValidSizeSet(dimensionList) {
let w = parseInt(dimensionList[0]);
let h = parseInt(dimensionList[1]);
// clever check for NaN
if (! (w !== w || h !== h)) { // eslint-disable-line
return [w, h];
}
return false;
}
function frameBanner(adUnit) {
// adUnit.sizes is scheduled to be deprecated, continue its support but prefer adUnit.mediaTypes.banner
var sizeList = adUnit.sizes;
if (adUnit.mediaTypes && adUnit.mediaTypes.banner) {
sizeList = adUnit.mediaTypes.banner.sizes;
}
var sizeStringList = parseSizesInput(sizeList);
var format = [];
sizeStringList.forEach(function(size) {
if (size) {
var dimensionList = getValidSizeSet(size.split('x'));
if (dimensionList) {
format.push({
'w': dimensionList[0],
'h': dimensionList[1],
});
}
}
});
if (format.length) {
return {
'format': format
};
}
return false;
}
function frameVideo(bid) {
var size = [];
if (deepAccess(bid, 'mediaTypes.video.playerSize')) {
var dimensionSet = bid.mediaTypes.video.playerSize;
if (isArray(bid.mediaTypes.video.playerSize[0])) {
dimensionSet = bid.mediaTypes.video.playerSize[0];
}
var validSize = getValidSizeSet(dimensionSet)
if (validSize) {
size = validSize;
}
}
return {
mimes: deepAccess(bid, 'mediaTypes.video.mimes') || SUPPORTED_VIDEO_MIMES,
protocols: deepAccess(bid, 'mediaTypes.video.protocols') || SUPPORTED_VIDEO_PROTOCOLS,
w: size[0],
h: size[1],
startdelay: deepAccess(bid, 'mediaTypes.video.startdelay') || 0,
skip: deepAccess(bid, 'mediaTypes.video.skip') || 0,
playbackmethod: deepAccess(bid, 'mediaTypes.video.playbackmethod') || SUPPORTED_VIDEO_PLAYBACK_METHODS,
delivery: deepAccess(bid, 'mediaTypes.video.delivery') || SUPPORTED_VIDEO_DELIVERY,
api: deepAccess(bid, 'mediaTypes.video.api') || SUPPORTED_VIDEO_API,
}
}
function frameExt(bid) {
if ((bid.mediaTypes && bid.mediaTypes.banner && bid.mediaTypes.banner.sizes)) {
let bidSizes = (bid.mediaTypes && bid.mediaTypes.banner && bid.mediaTypes.banner.sizes) || bid.sizes;
bidSizes = ((isArray(bidSizes) && isArray(bidSizes[0])) ? bidSizes : [bidSizes]);
bidSizes = bidSizes.filter(size => isArray(size));
const processedSizes = bidSizes.map(size => ({w: parseInt(size[0], 10), h: parseInt(size[1], 10)}));
const element = document.getElementById(bid.adUnitCode);
const minSize = _getMinSize(processedSizes);
const viewabilityAmount = _isViewabilityMeasurable(element)
? _getViewability(element, getWindowTop(), minSize)
: 'na';
const viewabilityAmountRounded = isNaN(viewabilityAmount) ? viewabilityAmount : Math.round(viewabilityAmount);
return {
bidder: {
zoneId: bid.params['zoneId']
},
viewability: viewabilityAmountRounded
}
} else {
return {
bidder: {
zoneId: bid.params['zoneId']
},
viewability: 'na'
}
}
}
function frameBid(BRs, bidderRequest) {
let bid = {
id: BRs[0].bidderRequestId,
imp: frameImp(BRs, bidderRequest),
site: frameSite(bidderRequest),
device: frameDevice(),
user: {
ext: {
consent: deepAccess(bidderRequest, 'gdprConsent.gdprApplies') ? bidderRequest.gdprConsent.consentString : ''
}
},
at: 1,
tmax: 650,
regs: {
ext: {
gdpr: deepAccess(bidderRequest, 'gdprConsent.gdprApplies') ? Boolean(bidderRequest.gdprConsent.gdprApplies & 1) : false
}
}
};
if (BRs[0].schain) {
deepSetValue(bid, 'source.ext.schain', BRs[0].schain);
}
if (bidderRequest.uspConsent) {
deepSetValue(bid, 'regs.ext.us_privacy', bidderRequest.uspConsent)
}
if (config.getConfig('coppa') === true) {
deepSetValue(bid, 'regs.coppa', config.getConfig('coppa') & 1)
}
return bid;
}
function getFirstParam(key, validBidRequests) {
for (let i = 0; i < validBidRequests.length; i++) {
if (validBidRequests[i].params && validBidRequests[i].params[key]) {
return validBidRequests[i].params[key];
}
}
}
this.buildRequests = function (BRs, bidderRequest) {
let fallbackZoneId = getFirstParam('zoneId', BRs);
if (fallbackZoneId === undefined || BRs.length < 1) {
return [];
}
var uri = 'https://hb.go2speed.media/bidder/?bid=3mhdom&zoneId=' + fallbackZoneId;
var fat = /(^v|(\.0)+$)/gi;
var prebidVersion = '$prebid.version$';
uri += '&hbv=' + prebidVersion.replace(fat, '') + ',' + version.replace(fat, '');
var bidRequest = frameBid(BRs, bidderRequest);
if (!bidRequest.imp.length) {
return {};
}
return {
method: 'POST',
url: uri,
data: JSON.stringify(bidRequest)
};
};
this.onBidWon = function (bid) {
if (typeof bid.nurl !== 'undefined') {
const cpm = bid.pbMg;
bid.nurl = bid.nurl.replace(
/\$\{AUCTION_PRICE\}/,
cpm
);
triggerPixel(bid.nurl, null);
};
sendbeacon(bid, 17)
};
this.onTimeout = function (bid) {
sendbeacon(bid, 19)
};
this.onSetTargeting = function (bid) {
sendbeacon(bid, 20)
};
this.interpretResponse = function (serverResponse) {
let responses = serverResponse.body || [];
let bids = [];
let i = 0;
if (responses.seatbid) {
let temp = [];
for (i = 0; i < responses.seatbid.length; i++) {
for (let j = 0; j < responses.seatbid[i].bid.length; j++) {
temp.push(responses.seatbid[i].bid[j]);
}
}
responses = temp;
}
for (i = 0; i < responses.length; i++) {
let bid = responses[i];
let bidRequest = slotsToBids[bid.impid];
let bidResponse = {
requestId: bidRequest.bidId,
cpm: parseFloat(bid.price),
width: bid.w,
height: bid.h,
creativeId: bid.crid,
currency: 'USD',
netRevenue: true,
ttl: 350,
nurl: bid.nurl
};
if (bidRequest.mediaTypes && bidRequest.mediaTypes.video) {
if (bid.adm.charAt(0) === '<') {
bidResponse.vastXml = bid.adm;
} else {
bidResponse.vastUrl = bid.adm;
}
bidResponse.mediaType = 'video';
bidResponse.ttl = 600;
} else {
bidResponse.ad = bid.adm;
}
bids.push(bidResponse);
}
return bids;
};
function sendbeacon(bid, type) {
const bidString = JSON.stringify(bid);
const encodedBuf = window.btoa(bidString);
triggerPixel('https://ping-hqx-1.go2speed.media/notification/rtb/beacon/?bt=' + type + '&bid=3mhdom&hb_j=' + encodedBuf, null);
}
/**
* Gets bidfloor
* @param {Object} bid
* @returns {Number} floor
*/
function _getFloor (bid) {
const curMediaType = bid.mediaTypes.video ? 'video' : 'banner';
let floor = 0;
if (typeof bid.getFloor === 'function') {
const floorInfo = bid.getFloor({
currency: 'USD',
mediaType: curMediaType,
size: '*'
});
if (typeof floorInfo === 'object' &&
floorInfo.currency === 'USD' &&
!isNaN(parseFloat(floorInfo.floor))) {
floor = floorInfo.floor;
}
}
return floor;
}
function _getMinSize(sizes) {
return sizes.reduce((min, size) => size.h * size.w < min.h * min.w ? size : min);
}
function _isViewabilityMeasurable(element) {
return !_isIframe() && element !== null;
}
function _isIframe() {
try {
return getWindowSelf() !== getWindowTop();
} catch (e) {
return true;
}
}
function _getViewability(element, topWin, { w, h } = {}) {
return topWin.document.visibilityState === 'visible'
? _getPercentInView(element, topWin, { w, h })
: 0;
}
function _getPercentInView(element, topWin, { w, h } = {}) {
const elementBoundingBox = _getBoundingBox(element, { w, h });
const elementInViewBoundingBox = _getIntersectionOfRects([ {
left: 0,
top: 0,
right: topWin.innerWidth,
bottom: topWin.innerHeight
}, elementBoundingBox ]);
let elementInViewArea, elementTotalArea;
if (elementInViewBoundingBox !== null) {
// Some or all of the element is in view
elementInViewArea = elementInViewBoundingBox.width * elementInViewBoundingBox.height;
elementTotalArea = elementBoundingBox.width * elementBoundingBox.height;
return ((elementInViewArea / elementTotalArea) * 100);
}
return 0;
}
function _getBoundingBox(element, { w, h } = {}) {
let { width, height, left, top, right, bottom } = element.getBoundingClientRect();
if ((width === 0 || height === 0) && w && h) {
width = w;
height = h;
right = left + w;
bottom = top + h;
}
return { width, height, left, top, right, bottom };
}
function _getIntersectionOfRects(rects) {
const bbox = {
left: rects[0].left,
right: rects[0].right,
top: rects[0].top,
bottom: rects[0].bottom
};
for (let i = 1; i < rects.length; ++i) {
bbox.left = Math.max(bbox.left, rects[i].left);
bbox.right = Math.min(bbox.right, rects[i].right);
if (bbox.left >= bbox.right) {
return null;
}
bbox.top = Math.max(bbox.top, rects[i].top);
bbox.bottom = Math.min(bbox.bottom, rects[i].bottom);
if (bbox.top >= bbox.bottom) {
return null;
}
}
bbox.width = bbox.right - bbox.left;
bbox.height = bbox.bottom - bbox.top;
return bbox;
}
}
export const spec = new MarsmediaAdapter();
registerBidder(spec);