-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathemoteevBidAdapter.js
491 lines (449 loc) · 13.5 KB
/
emoteevBidAdapter.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
/**
* This file contains Emoteev bid adpater.
*
* It is organised as follows:
* - Constants values;
* - Spec API functions, which should be pristine pure;
* - Ancillary functions, which should be as pure as possible;
* - Adapter API, where unpure side-effects happen.
*
* The code style is « functional core, imperative shell ».
*
* @link https://www.emoteev.io
* @file This files defines the spec of EmoteevBidAdapter.
* @author Emoteev Engineering <[email protected]>.
*/
import {registerBidder} from '../src/adapters/bidderFactory';
import {BANNER} from '../src/mediaTypes';
import {
triggerPixel,
getUniqueIdentifierStr,
contains,
deepAccess,
isArray,
getParameterByName
} from '../src/utils';
import {config} from '../src/config';
import * as url from '../src/url';
import {getCookie} from './pubCommonId';
export const BIDDER_CODE = 'emoteev';
/**
* Version number of the adapter API.
*/
export const ADAPTER_VERSION = '1.35.0';
export const DOMAIN = 'prebid.emoteev.io';
export const DOMAIN_STAGING = 'prebid-staging.emoteev.io';
export const DOMAIN_DEVELOPMENT = 'localhost:3000';
/**
* Path of Emoteev endpoint for events.
*/
export const EVENTS_PATH = '/api/ad_event.json';
/**
* Path of Emoteev bidder.
*/
export const BIDDER_PATH = '/api/prebid/bid';
export const USER_SYNC_IFRAME_PATH = '/api/prebid/sync-iframe';
export const USER_SYNC_IMAGE_PATH = '/api/prebid/sync-image';
export const PRODUCTION = 'production';
export const STAGING = 'staging';
export const DEVELOPMENT = 'development';
export const DEFAULT_ENV = PRODUCTION;
export const ON_ADAPTER_CALLED = 'on_adapter_called';
export const ON_BID_WON = 'on_bid_won';
export const ON_BIDDER_TIMEOUT = 'on_bidder_timeout';
/**
* Pure function. See http://prebid.org/dev-docs/bidder-adaptor.html#valid-build-requests-array for detailed semantic.
*
* @param {AdUnit.bidRequest} bidRequest
* @returns {boolean} Is this bidRequest valid?
*/
export const isBidRequestValid = (bidRequest) => {
return !!(
bidRequest &&
bidRequest.params &&
deepAccess(bidRequest, 'params.adSpaceId') &&
bidRequest.bidder === BIDDER_CODE &&
validateSizes(deepAccess(bidRequest, 'mediaTypes.banner.sizes')));
};
/**
* Pure function. See http://prebid.org/dev-docs/bidder-adaptor.html#serverrequest-objects for detailed semantic.
*
* @param {string} env Emoteev environment parameter
* @param {boolean} debug Pbjs debug parameter.
* @param {string} currency See http://prebid.org/dev-docs/modules/currency.html for detailed semantic.
* @param {Array<BidRequest>} validBidRequests Takes an array of bid requests, which are guaranteed to have passed the isBidRequestValid() test.
* @param bidderRequest General context for a bidder request being constructed
* @returns {ServerRequest}
*/
export const buildRequests = (env, debug, currency, validBidRequests, bidderRequest) => {
return {
method: 'POST',
url: bidderUrl(env),
data: JSON.stringify(requestsPayload(debug, currency, validBidRequests, bidderRequest))
};
};
/**
* Pure function. See http://prebid.org/dev-docs/bidder-adaptor.html#interpreting-the-response for detailed semantic.
*
* @param {Array} serverResponse.body The body of the server response is an array of bid objects.
* @returns {Array}
*/
export const interpretResponse = (serverResponse) => serverResponse.body;
/**
* Pure function. See http://prebid.org/dev-docs/bidder-adaptor.html#registering-on-set-targeting for detailed semantic.
*
* @param {string} env Emoteev environment parameter.
* @param {BidRequest} bidRequest
* @returns {UrlObject}
*/
export function onAdapterCalled(env, bidRequest) {
return {
protocol: 'https',
hostname: domain(env),
pathname: EVENTS_PATH,
search: {
eventName: ON_ADAPTER_CALLED,
pubcId: deepAccess(bidRequest, 'crumbs.pubcid'),
bidId: bidRequest.bidId,
adSpaceId: deepAccess(bidRequest, 'params.adSpaceId'),
cache_buster: getUniqueIdentifierStr()
}
};
}
/**
* Pure function. See http://prebid.org/dev-docs/bidder-adaptor.html#registering-on-bid-won for detailed semantic.
*
* @param {string} env Emoteev environment parameter.
* @param {string} pubcId Publisher common id. See http://prebid.org/dev-docs/modules/pubCommonId.html for detailed semantic.
* @param bidObject
* @returns {UrlObject}
*/
export const onBidWon = (env, pubcId, bidObject) => {
const bidId = bidObject.requestId;
return {
protocol: 'https',
hostname: domain(env),
pathname: EVENTS_PATH,
search: {
eventName: ON_BID_WON,
pubcId,
bidId,
cache_buster: getUniqueIdentifierStr()
}
};
};
/**
* Pure function. See http://prebid.org/dev-docs/bidder-adaptor.html#registering-on-timeout for detailed semantic.
*
* @param {string} env Emoteev environment parameter.
* @param {BidRequest} bidRequest
* @returns {UrlObject}
*/
export const onTimeout = (env, bidRequest) => {
return {
protocol: 'https',
hostname: domain(env),
pathname: EVENTS_PATH,
search: {
eventName: ON_BIDDER_TIMEOUT,
pubcId: deepAccess(bidRequest, 'crumbs.pubcid'),
bidId: bidRequest.bidId,
adSpaceId: deepAccess(bidRequest, 'params.adSpaceId'),
timeout: bidRequest.timeout,
cache_buster: getUniqueIdentifierStr()
}
}
};
/**
* Pure function. See http://prebid.org/dev-docs/bidder-adaptor.html#registering-user-syncs for detailed semantic.
*
* @param {string} env Emoteev environment parameter
* @param {SyncOptions} syncOptions
* @returns userSyncs
*/
export const getUserSyncs = (env, syncOptions) => {
let syncs = [];
if (syncOptions.pixelEnabled) {
syncs.push({
type: 'image',
url: userSyncImageUrl(env),
});
}
if (syncOptions.iframeEnabled) {
syncs.push({
type: 'iframe',
url: userSyncIframeUrl(env),
});
}
return syncs;
};
/**
* Pure function.
*
* @param {string} env Emoteev environment parameter
* @returns {string} The domain for network calls to Emoteev.
*/
export const domain = (env) => {
switch (env) {
case DEVELOPMENT:
return DOMAIN_DEVELOPMENT;
case STAGING:
return DOMAIN_STAGING;
default:
return DOMAIN;
}
};
/**
* Pure function.
*
* @param {string} env Emoteev environment parameter
* @returns {string} The full URL which events is sent to.
*/
export const eventsUrl = env => url.format({
protocol: (env === DEVELOPMENT) ? 'http' : 'https',
hostname: domain(env),
pathname: EVENTS_PATH
});
/**
* Pure function.
*
* @param {string} env Emoteev environment parameter
* @returns {string} The full URL which bidderRequest is sent to.
*/
export const bidderUrl = env => url.format({
protocol: (env === DEVELOPMENT) ? 'http' : 'https',
hostname: domain(env),
pathname: BIDDER_PATH
});
/**
* Pure function.
*
* @param {string} env Emoteev environment parameter
* @returns {string} The full URL called for iframe-based user sync
*/
export const userSyncIframeUrl = env => url.format({
protocol: (env === DEVELOPMENT) ? 'http' : 'https',
hostname: domain(env),
pathname: USER_SYNC_IFRAME_PATH
});
/**
* Pure function.
*
* @param {string} env Emoteev environment parameter
* @returns {string} The full URL called for image-based user sync
*/
export const userSyncImageUrl = env => url.format({
protocol: (env === DEVELOPMENT) ? 'http' : 'https',
hostname: domain(env),
pathname: USER_SYNC_IMAGE_PATH
});
/**
* Pure function.
*
* @param {Array<Array<int>>} sizes
* @returns {boolean} are sizes valid?
*/
const validateSizes = sizes => isArray(sizes) && sizes.some(size => isArray(size) && size.length === 2);
/**
* Pure function.
*
* @param {BidRequest} bidRequest
* @returns {object} An object which represents a BidRequest for Emoteev server side.
*/
export const conformBidRequest = bidRequest => {
return {
params: bidRequest.params,
crumbs: bidRequest.crumbs,
sizes: bidRequest.sizes,
bidId: bidRequest.bidId,
bidderRequestId: bidRequest.bidderRequestId,
};
};
/**
* Pure function.
*
* @param {boolean} debug Pbjs debug parameter
* @param {string} currency See http://prebid.org/dev-docs/modules/currency.html for detailed information
* @param {BidRequest} validBidRequests
* @param {object} bidderRequest
* @returns
*/
export const requestsPayload = (debug, currency, validBidRequests, bidderRequest) => {
return {
akPbjsVersion: ADAPTER_VERSION,
bidRequests: validBidRequests.map(conformBidRequest),
currency: currency,
debug: debug,
language: navigator.language,
refererInfo: bidderRequest.refererInfo,
deviceInfo: getDeviceInfo(
getDeviceDimensions(window),
getViewDimensions(window, document),
getDocumentDimensions(document),
isWebGLEnabled(document)),
userAgent: navigator.userAgent,
gdprApplies: deepAccess(bidderRequest, 'gdprConsent.gdprApplies'),
gdprConsent: deepAccess(bidderRequest, 'gdprConsent.consentString'),
};
};
/**
* Pure function
* @param {Window} window
* @param {Document} document
* @returns {{width: number, height: number}} View dimensions
*/
export const getViewDimensions = (window, document) => {
let w = window;
let prefix = 'inner';
if (window.innerWidth === undefined || window.innerWidth === null) {
w = document.documentElement || document.body;
prefix = 'client';
}
return {
width: w[`${prefix}Width`],
height: w[`${prefix}Height`],
};
};
/**
* Pure function
* @param {Window} window
* @returns {{width: number, height: number}} Device dimensions
*/
export const getDeviceDimensions = (window) => {
return {
width: window.screen ? window.screen.width : '',
height: window.screen ? window.screen.height : '',
};
};
/**
* Pure function
* @param {Document} document
* @returns {{width: number, height: number}} Document dimensions
*/
export const getDocumentDimensions = (document) => {
const de = document.documentElement;
const be = document.body;
const bodyHeight = be ? Math.max(be.offsetHeight, be.scrollHeight) : 0;
const w = Math.max(de.clientWidth, de.offsetWidth, de.scrollWidth);
const h = Math.max(
de.clientHeight,
de.offsetHeight,
de.scrollHeight,
bodyHeight
);
return {
width: isNaN(w) ? '' : w,
height: isNaN(h) ? '' : h,
};
};
/**
* Unpure function
* @param {Document} document
* @returns {boolean} Is WebGL enabled?
*/
export const isWebGLEnabled = (document) => {
// Create test canvas
let canvas = document.createElement('canvas');
// The gl context
let gl = null;
// Try to get the regular WebGL
try {
gl = canvas.getContext('webgl');
} catch (ex) {
canvas = undefined;
return false;
}
// No regular WebGL found
if (!gl) {
// Try experimental WebGL
try {
gl = canvas.getContext('experimental-webgl');
} catch (ex) {
canvas = undefined;
return false;
}
}
return !!gl;
};
/**
* Pure function
* @param {{width: number, height: number}} deviceDimensions
* @param {{width: number, height: number}} viewDimensions
* @param {{width: number, height: number}} documentDimensions
* @param {boolean} webGL
* @returns {object} Device information
*/
export const getDeviceInfo = (deviceDimensions, viewDimensions, documentDimensions, webGL) => {
return {
browserWidth: viewDimensions.width,
browserHeight: viewDimensions.height,
deviceWidth: deviceDimensions.width,
deviceHeight: deviceDimensions.height,
documentWidth: documentDimensions.width,
documentHeight: documentDimensions.height,
webGL: webGL,
};
};
/**
* Pure function
* @param {object} config pbjs config value
* @param {string} parameter Environment override from URL query param.
* @returns One of [PRODUCTION, STAGING, DEVELOPMENT].
*/
export const resolveEnv = (config, parameter) => {
const configEnv = deepAccess(config, 'emoteev.env');
if (contains([PRODUCTION, STAGING, DEVELOPMENT], parameter)) return parameter;
else if (contains([PRODUCTION, STAGING, DEVELOPMENT], configEnv)) return configEnv;
else return DEFAULT_ENV;
};
/**
* Pure function
* @param {object} config pbjs config value
* @param {string} parameter Debug override from URL query param.
* @returns {boolean}
*/
export const resolveDebug = (config, parameter) => {
if (parameter && parameter.length && parameter.length > 0) return JSON.parse(parameter);
else if (config.debug) return config.debug;
else return false;
};
/**
* EmoteevBidAdapter spec
* @access public
* @type {BidderSpec}
*/
export const spec = {
code: BIDDER_CODE,
supportedMediaTypes: [BANNER],
isBidRequestValid: (bidRequest) => {
const isValid = isBidRequestValid(bidRequest);
if (isValid) {
triggerPixel(url.format(onAdapterCalled(
resolveEnv(config.getConfig(), getParameterByName('emoteevEnv')),
bidRequest)))
}
return isValid;
},
buildRequests: (validBidRequests, bidderRequest) =>
buildRequests(
resolveEnv(config.getConfig(), getParameterByName('emoteevEnv')),
resolveDebug(config.getConfig(), getParameterByName('debug')),
config.getConfig('currency'),
validBidRequests,
bidderRequest),
interpretResponse: interpretResponse,
onBidWon: (bidObject) =>
triggerPixel(url.format(onBidWon(
resolveEnv(config.getConfig(), getParameterByName('emoteevEnv')),
getCookie('_pubcid'),
bidObject))),
onTimeout: (bidRequest) =>
triggerPixel(url.format(onTimeout(
resolveEnv(config.getConfig(), getParameterByName('emoteevEnv')),
bidRequest))),
getUserSyncs: (syncOptions) =>
getUserSyncs(
resolveEnv(config.getConfig(), getParameterByName('emoteevEnv')),
syncOptions),
};
registerBidder(spec);