-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
zeta_global_sspBidAdapter.js
428 lines (395 loc) · 12.2 KB
/
zeta_global_sspBidAdapter.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
import {deepAccess, deepSetValue, isArray, isBoolean, isNumber, isStr, logWarn} from '../src/utils.js';
import {registerBidder} from '../src/adapters/bidderFactory.js';
import {BANNER, VIDEO} from '../src/mediaTypes.js';
import {config} from '../src/config.js';
import {parseDomain} from '../src/refererDetection.js';
/**
* @typedef {import('../src/adapters/bidderFactory.js').BidRequest} BidRequest
* @typedef {import('../src/adapters/bidderFactory.js').Bid} Bid
* @typedef {import('../src/adapters/bidderFactory.js').ServerResponse} ServerResponse
* @typedef {import('../src/adapters/bidderFactory.js').Bids} Bids
* @typedef {import('../src/adapters/bidderFactory.js').BidderRequest} BidderRequest
*/
const BIDDER_CODE = 'zeta_global_ssp';
const ENDPOINT_URL = 'https://ssp.disqus.com/bid/prebid';
const USER_SYNC_URL_IFRAME = 'https://ssp.disqus.com/sync?type=iframe';
const USER_SYNC_URL_IMAGE = 'https://ssp.disqus.com/sync?type=image';
const DEFAULT_CUR = 'USD';
const TTL = 300;
const NET_REV = true;
const DATA_TYPES = {
'NUMBER': 'number',
'STRING': 'string',
'BOOLEAN': 'boolean',
'ARRAY': 'array',
'OBJECT': 'object'
};
const VIDEO_CUSTOM_PARAMS = {
'mimes': DATA_TYPES.ARRAY,
'minduration': DATA_TYPES.NUMBER,
'maxduration': DATA_TYPES.NUMBER,
'startdelay': DATA_TYPES.NUMBER,
'playbackmethod': DATA_TYPES.ARRAY,
'api': DATA_TYPES.ARRAY,
'protocols': DATA_TYPES.ARRAY,
'w': DATA_TYPES.NUMBER,
'h': DATA_TYPES.NUMBER,
'battr': DATA_TYPES.ARRAY,
'linearity': DATA_TYPES.NUMBER,
'placement': DATA_TYPES.NUMBER,
'plcmt': DATA_TYPES.NUMBER,
'minbitrate': DATA_TYPES.NUMBER,
'maxbitrate': DATA_TYPES.NUMBER,
'skip': DATA_TYPES.NUMBER
}
export const spec = {
code: BIDDER_CODE,
gvlid: 469,
supportedMediaTypes: [BANNER, VIDEO],
/**
* Determines whether the given bid request is valid.
*
* @param {BidRequest} bid The bid params to validate.
* @return boolean True if this is a valid bid, and false otherwise.
*/
isBidRequestValid: function (bid) {
// check for all required bid fields
if (!(bid &&
bid.bidId &&
bid.params &&
bid.params.sid)) {
logWarn('Invalid bid request - missing required bid data');
return false;
}
return true;
},
/**
* Make a server request from the list of BidRequests.
*
* @param {Bids[]} validBidRequests - an array of bidRequest objects
* @param {BidderRequest} bidderRequest - master bidRequest object
* @return ServerRequest Info describing the request to the server.
*/
buildRequests: function (validBidRequests, bidderRequest) {
const secure = 1; // treat all requests as secure
const params = validBidRequests[0].params;
const imps = validBidRequests.map(request => {
const impData = {
id: request.bidId,
secure: secure
};
const tagid = request.params?.tagid;
if (tagid) {
impData.tagid = tagid;
}
if (request.mediaTypes) {
for (const mediaType in request.mediaTypes) {
switch (mediaType) {
case BANNER:
impData.banner = buildBanner(request);
break;
case VIDEO:
impData.video = buildVideo(request);
break;
}
}
}
if (!impData.banner && !impData.video) {
impData.banner = buildBanner(request);
}
if (typeof request.getFloor === 'function') {
const floorInfo = request.getFloor({
currency: 'USD',
mediaType: impData.video ? 'video' : 'banner',
size: [ impData.video ? impData.video.w : impData.banner.w, impData.video ? impData.video.h : impData.banner.h ]
});
if (floorInfo && floorInfo.floor) {
impData.bidfloor = floorInfo.floor;
}
}
if (!impData.bidfloor) {
const bidfloor = request.params?.bidfloor;
if (bidfloor) {
impData.bidfloor = bidfloor;
}
}
return impData;
});
let payload = {
id: bidderRequest.bidderRequestId,
cur: [DEFAULT_CUR],
imp: imps,
site: params.site ? params.site : {},
device: {...(bidderRequest.ortb2?.device || {}), ...params.device},
user: params.user ? params.user : {},
app: params.app ? params.app : {},
ext: {
tags: params.tags ? params.tags : {},
sid: params.sid ? params.sid : undefined
}
};
const rInfo = bidderRequest.refererInfo;
// TODO: do the fallbacks make sense here?
payload.site.page = cropPage(rInfo.page || rInfo.topmostLocation);
payload.site.domain = parseDomain(payload.site.page, {noLeadingWww: true});
payload.device.ua = navigator.userAgent;
payload.device.language = navigator.language;
payload.device.w = screen.width;
payload.device.h = screen.height;
if (bidderRequest.ortb2?.user?.geo && bidderRequest.ortb2?.device?.geo) {
payload.device.geo = { ...payload.device.geo, ...bidderRequest.ortb2?.device.geo };
payload.user.geo = { ...payload.user.geo, ...bidderRequest.ortb2?.user.geo };
} else {
if (bidderRequest.ortb2?.user?.geo) {
payload.user.geo = payload.device.geo = { ...payload.user.geo, ...bidderRequest.ortb2?.user.geo };
}
if (bidderRequest.ortb2?.device?.geo) {
payload.user.geo = payload.device.geo = { ...payload.user.geo, ...bidderRequest.ortb2?.device.geo };
}
}
if (bidderRequest?.ortb2?.device?.sua) {
payload.device.sua = bidderRequest.ortb2.device.sua;
}
if (params.test) {
payload.test = params.test;
}
// Attaching GDPR Consent Params
if (bidderRequest && bidderRequest.gdprConsent) {
deepSetValue(payload, 'user.ext.consent', bidderRequest.gdprConsent.consentString);
deepSetValue(payload, 'regs.ext.gdpr', (bidderRequest.gdprConsent.gdprApplies ? 1 : 0));
}
// CCPA
if (bidderRequest && bidderRequest.uspConsent) {
deepSetValue(payload, 'regs.ext.us_privacy', bidderRequest.uspConsent);
}
// schain
if (validBidRequests[0].schain) {
payload.source = {
ext: {
schain: validBidRequests[0].schain
}
}
}
if (bidderRequest?.timeout) {
payload.tmax = bidderRequest.timeout;
}
provideEids(validBidRequests[0], payload);
provideSegments(bidderRequest, payload);
const url = params.sid ? ENDPOINT_URL.concat('?sid=', params.sid) : ENDPOINT_URL;
return {
method: 'POST',
url: url,
data: JSON.stringify(clearEmpties(payload)),
};
},
/**
* Unpack the response from the server into a list of bids.
*
* @param {ServerResponse} serverResponse A successful response from the server.
* @param bidRequest The payload from the server's response.
* @return {Bid[]} An array of bids which were nested inside the server.
*/
interpretResponse: function (serverResponse, bidRequest) {
let bidResponses = [];
const response = (serverResponse || {}).body;
if (response && response.seatbid && response.seatbid[0].bid && response.seatbid[0].bid.length) {
response.seatbid.forEach(zetaSeatbid => {
const seat = zetaSeatbid.seat;
zetaSeatbid.bid.forEach(zetaBid => {
let bid = {
requestId: zetaBid.impid,
cpm: zetaBid.price,
currency: response.cur,
width: zetaBid.w,
height: zetaBid.h,
ad: zetaBid.adm,
ttl: TTL,
creativeId: zetaBid.crid,
netRevenue: NET_REV,
};
if (zetaBid.adomain && zetaBid.adomain.length) {
bid.meta = {
advertiserDomains: zetaBid.adomain
};
}
provideMediaType(zetaBid, bid, bidRequest.data);
if (bid.mediaType === VIDEO) {
bid.vastXml = bid.ad;
}
if (seat) {
bid.dspId = seat;
}
bidResponses.push(bid);
})
})
}
return bidResponses;
},
/**
* Register User Sync.
*/
getUserSyncs: (syncOptions, responses, gdprConsent, uspConsent) => {
let syncurl = '';
// Attaching GDPR Consent Params in UserSync url
if (gdprConsent) {
syncurl += '&gdpr=' + (gdprConsent.gdprApplies ? 1 : 0);
syncurl += '&gdpr_consent=' + encodeURIComponent(gdprConsent.consentString || '');
}
// CCPA
if (uspConsent) {
syncurl += '&us_privacy=' + encodeURIComponent(uspConsent);
}
// coppa compliance
if (config.getConfig('coppa') === true) {
syncurl += '&coppa=1';
}
if (syncOptions.iframeEnabled) {
return [{
type: 'iframe',
url: USER_SYNC_URL_IFRAME + syncurl
}];
} else {
return [{
type: 'image',
url: USER_SYNC_URL_IMAGE + syncurl
}];
}
}
}
function buildBanner(request) {
let sizes = request.sizes;
if (request.mediaTypes &&
request.mediaTypes.banner &&
request.mediaTypes.banner.sizes) {
sizes = request.mediaTypes.banner.sizes;
}
if (sizes.length > 1) {
const format = sizes.map(s => {
return {
w: s[0],
h: s[1]
}
});
return {
w: sizes[0][0],
h: sizes[0][1],
format: format
}
} else {
return {
w: sizes[0][0],
h: sizes[0][1]
}
}
}
function buildVideo(request) {
let video = {};
const videoParams = deepAccess(request, 'mediaTypes.video', {});
for (const key in VIDEO_CUSTOM_PARAMS) {
if (videoParams.hasOwnProperty(key)) {
video[key] = checkParamDataType(key, videoParams[key], VIDEO_CUSTOM_PARAMS[key]);
}
}
if (videoParams.playerSize) {
if (isArray(videoParams.playerSize[0])) {
video.w = parseInt(videoParams.playerSize[0][0], 10);
video.h = parseInt(videoParams.playerSize[0][1], 10);
} else if (isNumber(videoParams.playerSize[0])) {
video.w = parseInt(videoParams.playerSize[0], 10);
video.h = parseInt(videoParams.playerSize[1], 10);
}
}
return video;
}
function checkParamDataType(key, value, datatype) {
let functionToExecute;
switch (datatype) {
case DATA_TYPES.BOOLEAN:
functionToExecute = isBoolean;
break;
case DATA_TYPES.NUMBER:
functionToExecute = isNumber;
break;
case DATA_TYPES.STRING:
functionToExecute = isStr;
break;
case DATA_TYPES.ARRAY:
functionToExecute = isArray;
break;
}
if (functionToExecute(value)) {
return value;
}
logWarn('Ignoring param key: ' + key + ', expects ' + datatype + ', found ' + typeof value);
return undefined;
}
function provideEids(request, payload) {
if (Array.isArray(request.userIdAsEids) && request.userIdAsEids.length > 0) {
deepSetValue(payload, 'user.ext.eids', request.userIdAsEids);
}
}
function provideSegments(bidderRequest, payload) {
const data = bidderRequest.ortb2?.user?.data;
if (isArray(data)) {
const segments = data.filter(d => d?.segment).map(d => d.segment).filter(s => isArray(s)).flatMap(s => s).filter(s => s?.id);
if (segments.length > 0) {
if (!payload.user) {
payload.user = {};
}
if (!isArray(payload.user.data)) {
payload.user.data = [];
}
const payloadData = {
segment: segments
};
payload.user.data.push(payloadData);
}
}
}
function provideMediaType(zetaBid, bid, bidRequest) {
if (zetaBid.ext && zetaBid.ext.prebid && zetaBid.ext.prebid.type) {
bid.mediaType = zetaBid.ext.prebid.type === VIDEO ? VIDEO : BANNER;
} else {
bid.mediaType = bidRequest.imp[0].video ? VIDEO : BANNER;
}
}
function cropPage(page) {
if (page) {
if (page.length > 100) {
page = page.substring(0, 100);
}
if (page.startsWith('https://')) {
page = page.substring(8);
} else if (page.startsWith('http://')) {
page = page.substring(7);
}
if (page.startsWith('www.')) {
page = page.substring(4);
}
for (let i = 3; i < page.length; i++) {
const c = page[i];
if (c === '#' || c === '?') {
return page.substring(0, i);
}
}
return page;
}
return '';
}
function clearEmpties(o) {
for (let k in o) {
if (o[k] === null) {
delete o[k];
continue;
}
if (!o[k] || typeof o[k] !== 'object') {
continue;
}
clearEmpties(o[k]);
if (Object.keys(o[k]).length === 0) {
delete o[k];
}
}
return o;
}
registerBidder(spec);