forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadkernelAdnAnalyticsAdapter.js
416 lines (373 loc) · 10.5 KB
/
adkernelAdnAnalyticsAdapter.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
import adapter from '../libraries/analyticsAdapter/AnalyticsAdapter.js';
import {EVENTS} from '../src/constants.js';
import adapterManager from '../src/adapterManager.js';
import { logError, parseUrl, _each } from '../src/utils.js';
import {ajax} from '../src/ajax.js';
import {getStorageManager} from '../src/storageManager.js';
import {config} from '../src/config.js';
import {MODULE_TYPE_ANALYTICS} from '../src/activities/modules.js';
const MODULE_CODE = 'adkernelAdn';
const GVLID = 14;
const ANALYTICS_VERSION = '1.0.2';
const DEFAULT_QUEUE_TIMEOUT = 4000;
const DEFAULT_HOST = 'tag.adkernel.com';
const storageObj = getStorageManager({moduleType: MODULE_TYPE_ANALYTICS, moduleName: MODULE_CODE});
const ADK_HB_EVENTS = {
AUCTION_INIT: 'auctionInit',
BID_REQUEST: 'bidRequested',
BID_RESPONSE: 'bidResponse',
BID_WON: 'bidWon',
AUCTION_END: 'auctionEnd',
TIMEOUT: 'adapterTimedOut'
};
function buildRequestTemplate(pubId) {
const {loc, ref} = getNavigationInfo();
return {
ver: ANALYTICS_VERSION,
domain: loc.hostname,
path: loc.pathname,
accId: pubId,
env: {
screen: {
w: window.screen.width,
h: window.screen.height
},
lang: navigator.language
},
user: {},
src: getUmtSource(loc.href, ref)
}
}
let analyticsAdapter = Object.assign(adapter({analyticsType: 'endpoint'}),
{
track({eventType, args}) {
if (!analyticsAdapter.context) {
return;
}
let handler = null;
switch (eventType) {
case EVENTS.AUCTION_INIT:
if (analyticsAdapter.context.queue) {
analyticsAdapter.context.queue.init();
}
initPrivacy(analyticsAdapter.context.requestTemplate, args.bidderRequests);
handler = trackAuctionInit;
break;
case EVENTS.BID_REQUESTED:
handler = trackBidRequest;
break;
case EVENTS.BID_RESPONSE:
handler = trackBidResponse;
break;
case EVENTS.BID_WON:
handler = trackBidWon;
break;
case EVENTS.BID_TIMEOUT:
handler = trackBidTimeout;
break;
case EVENTS.AUCTION_END:
handler = trackAuctionEnd;
break;
}
if (handler) {
let events = handler(args);
if (analyticsAdapter.context.queue) {
analyticsAdapter.context.queue.push(events);
}
if (eventType === EVENTS.AUCTION_END) {
sendAll();
}
}
}
});
analyticsAdapter.context = {};
analyticsAdapter.originEnableAnalytics = analyticsAdapter.enableAnalytics;
analyticsAdapter.enableAnalytics = (config) => {
if (!config.options.pubId) {
logError('PubId is not defined. Analytics won\'t work');
return;
}
analyticsAdapter.context = {
host: config.options.host || DEFAULT_HOST,
pubId: config.options.pubId,
requestTemplate: buildRequestTemplate(config.options.pubId),
queue: new ExpiringQueue(sendAll, config.options.queueTimeout || DEFAULT_QUEUE_TIMEOUT)
};
analyticsAdapter.originEnableAnalytics(config);
};
adapterManager.registerAnalyticsAdapter({
adapter: analyticsAdapter,
code: MODULE_CODE,
gvlid: GVLID
});
export default analyticsAdapter;
function sendAll() {
let events = analyticsAdapter.context.queue.popAll();
if (events.length !== 0) {
let req = Object.assign({}, analyticsAdapter.context.requestTemplate, {hb_ev: events});
analyticsAdapter.ajaxCall(JSON.stringify(req));
}
}
analyticsAdapter.ajaxCall = function ajaxCall(data) {
ajax(`https://${analyticsAdapter.context.host}/hb-analytics`, () => {
}, data);
};
function trackAuctionInit() {
analyticsAdapter.context.auctionTimeStart = Date.now();
const event = createHbEvent(undefined, ADK_HB_EVENTS.AUCTION_INIT);
return [event];
}
function trackBidRequest(args) {
return args.bids.map(bid =>
createHbEvent(args.bidderCode, ADK_HB_EVENTS.BID_REQUEST, bid.adUnitCode));
}
function trackBidResponse(args) {
const event = createHbEvent(args.bidderCode, ADK_HB_EVENTS.BID_RESPONSE,
args.adUnitCode, args.cpm, args.timeToRespond / 1000);
return [event];
}
function trackBidWon(args) {
const event = createHbEvent(args.bidderCode, ADK_HB_EVENTS.BID_WON, args.adUnitCode, args.cpm);
return [event];
}
function trackAuctionEnd(args) {
const event = createHbEvent(undefined, ADK_HB_EVENTS.AUCTION_END, undefined,
undefined, (Date.now() - analyticsAdapter.context.auctionTimeStart) / 1000);
return [event];
}
function trackBidTimeout(args) {
return args.map(bidderName => createHbEvent(bidderName, ADK_HB_EVENTS.TIMEOUT));
}
function createHbEvent(adapter, event, tagid = undefined, value = 0, time = 0) {
let ev = {event: event};
if (adapter) {
ev.adapter = adapter
}
if (tagid) {
ev.tagid = tagid;
}
if (value) {
ev.val = value;
}
if (time) {
ev.time = time;
}
return ev;
}
const UTM_TAGS = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',
'utm_c1', 'utm_c2', 'utm_c3', 'utm_c4', 'utm_c5'];
const ADKERNEL_PREBID_KEY = 'adk_dpt_analytics';
const DIRECT = '(direct)';
const REFERRAL = '(referral)';
const ORGANIC = '(organic)';
export let storage = {
getItem: (name) => {
return storageObj.getDataFromLocalStorage(name);
},
setItem: (name, value) => {
storageObj.setDataInLocalStorage(name, value);
}
};
export function getUmtSource(pageUrl, referrer) {
let prevUtm = getPreviousTrafficSource();
let currUtm = getCurrentTrafficSource(pageUrl, referrer);
let [updated, actual] = chooseActualUtm(prevUtm, currUtm);
if (updated) {
storeUtm(actual);
}
return actual;
function getPreviousTrafficSource() {
let val = storage.getItem(ADKERNEL_PREBID_KEY);
if (!val) {
return getDirect();
}
return JSON.parse(val);
}
function getCurrentTrafficSource(pageUrl, referrer) {
var source = getUTM(pageUrl);
if (source) {
return source;
}
if (referrer) {
let se = getSearchEngine(referrer);
if (se) {
return asUtm(se, ORGANIC, ORGANIC);
}
let parsedUrl = parseUrl(pageUrl);
let [refHost, refPath] = getReferrer(referrer);
if (refHost && refHost !== parsedUrl.hostname) {
return asUtm(refHost, REFERRAL, REFERRAL, '', refPath);
}
}
return getDirect();
}
function getSearchEngine(pageUrl) {
let engines = {
'google': /^https?\:\/\/(?:www\.)?(?:google\.(?:com?\.)?(?:com|cat|[a-z]{2})|g.cn)\//i,
'yandex': /^https?\:\/\/(?:www\.)?ya(?:ndex\.(?:com|net)?\.?(?:asia|mobi|org|[a-z]{2})?|\.ru)\//i,
'bing': /^https?\:\/\/(?:www\.)?bing\.com\//i,
'duckduckgo': /^https?\:\/\/(?:www\.)?duckduckgo\.com\//i,
'ask': /^https?\:\/\/(?:www\.)?ask\.com\//i,
'yahoo': /^https?\:\/\/(?:[-a-z]+\.)?(?:search\.)?yahoo\.com\//i
};
for (let engine in engines) {
if (engines.hasOwnProperty(engine) && engines[engine].test(pageUrl)) {
return engine;
}
}
}
function getReferrer(referrer) {
let ref = parseUrl(referrer);
return [ref.hostname, ref.pathname];
}
function getUTM(pageUrl) {
let urlParameters = parseUrl(pageUrl).search;
if (!urlParameters['utm_campaign'] || !urlParameters['utm_source']) {
return;
}
let utmArgs = [];
_each(UTM_TAGS, (utmTagName) => {
let utmValue = urlParameters[utmTagName] || '';
utmArgs.push(utmValue);
});
return asUtm.apply(this, utmArgs);
}
function getDirect() {
return asUtm(DIRECT, DIRECT, DIRECT);
}
function storeUtm(utm) {
let val = JSON.stringify(utm);
storage.setItem(ADKERNEL_PREBID_KEY, val);
}
function asUtm(source, medium, campaign, term = '', content = '', c1 = '', c2 = '', c3 = '', c4 = '', c5 = '') {
let result = {
source: source,
medium: medium,
campaign: campaign
};
if (term) {
result.term = term;
}
if (content) {
result.content = content;
}
if (c1) {
result.c1 = c1;
}
if (c2) {
result.c2 = c2;
}
if (c3) {
result.c3 = c3;
}
if (c4) {
result.c4 = c4;
}
if (c5) {
result.c5 = c5;
}
return result;
}
function chooseActualUtm(prev, curr) {
if (ord(prev) < ord(curr)) {
return [true, curr];
} else if (ord(prev) > ord(curr)) {
return [false, prev];
} else {
if (prev.campaign === REFERRAL && prev.content !== curr.content) {
return [true, curr];
} else if (prev.campaign === ORGANIC && prev.source !== curr.source) {
return [true, curr];
} else if (isCampaignTraffic(prev) && (prev.campaign !== curr.campaign || prev.source !== curr.source)) {
return [true, curr];
}
}
return [false, prev];
}
function ord(utm) {
switch (utm.campaign) {
case DIRECT:
return 0;
case ORGANIC:
return 1;
case REFERRAL:
return 2;
default:
return 3;
}
}
function isCampaignTraffic(utm) {
return [DIRECT, REFERRAL, ORGANIC].indexOf(utm.campaign) === -1;
}
}
/**
* Expiring queue implementation. Fires callback on elapsed timeout since last update or creation.
* @param callback
* @param ttl
* @constructor
*/
export function ExpiringQueue(callback, ttl) {
let queue = [];
let timeoutId;
this.push = (event) => {
if (event instanceof Array) {
queue.push.apply(queue, event);
} else {
queue.push(event);
}
reset();
};
this.popAll = () => {
let result = queue;
queue = [];
reset();
return result;
};
/**
* For test/debug purposes only
* @return {Array}
*/
this.peekAll = () => {
return queue;
};
this.init = reset;
function reset() {
if (timeoutId) {
clearTimeout(timeoutId);
}
timeoutId = setTimeout(() => {
if (queue.length) {
callback();
}
}, ttl);
}
}
// TODO: this should reuse logic from refererDetection
function getNavigationInfo() {
try {
return getLocationAndReferrer(self.top);
} catch (e) {
return getLocationAndReferrer(self);
}
}
function getLocationAndReferrer(win) {
return {
ref: win.document.referrer,
loc: win.location
};
}
function initPrivacy(template, requests) {
let consent = requests[0].gdprConsent;
if (consent && consent.gdprApplies) {
template.user.gdpr = ~~consent.gdprApplies;
}
if (consent && consent.consentString) {
template.user.gdpr_consent = consent.consentString;
}
if (requests[0].uspConsent) {
template.user.us_privacy = requests[0].uspConsent;
}
if (config.getConfig('coppa')) {
template.user.coppa = 1;
}
}