-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
AsteriobidPbmAnalyticsAdapter.js
345 lines (314 loc) · 10.2 KB
/
AsteriobidPbmAnalyticsAdapter.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
import { deepClone, generateUUID, getParameterByName, hasNonSerializableProperty, logError, parseUrl, logInfo } from '../src/utils.js';
import {ajaxBuilder} from '../src/ajax.js';
import adapter from '../libraries/analyticsAdapter/AnalyticsAdapter.js';
import adapterManager from '../src/adapterManager.js';
import {getStorageManager} from '../src/storageManager.js';
import { EVENTS } from '../src/constants.js';
import {MODULE_TYPE_ANALYTICS} from '../src/activities/modules.js';
/**
* prebidmanagerAnalyticsAdapter.js - analytics adapter for prebidmanager
*/
export const storage = getStorageManager({moduleType: MODULE_TYPE_ANALYTICS, moduleName: 'asteriobidpbm'});
const DEFAULT_EVENT_URL = 'https://endpt.prebidmanager.com/endpoint';
const analyticsType = 'endpoint';
const analyticsName = 'Asteriobid PBM Analytics';
let ajax = ajaxBuilder(0);
var _VERSION = 1;
var initOptions = null;
var _pageViewId = generateUUID();
var _startAuction = 0;
var _bidRequestTimeout = 0;
let flushInterval;
var pmAnalyticsEnabled = false;
const utmTags = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content'];
var w = window;
var d = document;
var e = d.documentElement;
var g = d.getElementsByTagName('body')[0];
var x = (w && w.innerWidth) || (e && e.clientWidth) || (g && g.clientWidth);
var y = (w && w.innerHeight) || (e && e.clientHeight) || (g && g.clientHeight);
var _pageView = {
eventType: 'pageView',
userAgent: window.navigator.userAgent,
timestamp: Date.now(),
timezoneOffset: new Date().getTimezoneOffset(),
language: window.navigator.language,
vendor: window.navigator.vendor,
screenWidth: x,
screenHeight: y
};
var _eventQueue = [
_pageView
];
let prebidmanagerAnalytics = Object.assign(adapter({url: DEFAULT_EVENT_URL, analyticsType}), {
track({eventType, args}) {
handleEvent(eventType, args);
}
});
prebidmanagerAnalytics.originEnableAnalytics = prebidmanagerAnalytics.enableAnalytics;
prebidmanagerAnalytics.enableAnalytics = function (config) {
initOptions = config.options || {};
initOptions.url = initOptions.url || DEFAULT_EVENT_URL;
initOptions.sampling = initOptions.sampling || 1;
if (Math.floor(Math.random() * initOptions.sampling) === 0) {
pmAnalyticsEnabled = true;
flushInterval = setInterval(flush, 1000);
} else {
logInfo(`${analyticsName} isn't enabled because of sampling`);
}
prebidmanagerAnalytics.originEnableAnalytics(config);
};
prebidmanagerAnalytics.originDisableAnalytics = prebidmanagerAnalytics.disableAnalytics;
prebidmanagerAnalytics.disableAnalytics = function () {
if (!pmAnalyticsEnabled) {
return;
}
flush();
clearInterval(flushInterval);
prebidmanagerAnalytics.originDisableAnalytics();
};
function collectUtmTagData() {
let newUtm = false;
let pmUtmTags = {};
try {
utmTags.forEach(function (utmKey) {
let utmValue = getParameterByName(utmKey);
if (utmValue !== '') {
newUtm = true;
}
pmUtmTags[utmKey] = utmValue;
});
if (newUtm === false) {
utmTags.forEach(function (utmKey) {
let itemValue = storage.getDataFromLocalStorage(`pm_${utmKey}`);
if (itemValue && itemValue.length !== 0) {
pmUtmTags[utmKey] = itemValue;
}
});
} else {
utmTags.forEach(function (utmKey) {
storage.setDataInLocalStorage(`pm_${utmKey}`, pmUtmTags[utmKey]);
});
}
} catch (e) {
logError(`${analyticsName} Error`, e);
pmUtmTags['error_utm'] = 1;
}
return pmUtmTags;
}
function collectPageInfo() {
const pageInfo = {
domain: window.location.hostname,
}
if (document.referrer) {
pageInfo.referrerDomain = parseUrl(document.referrer).hostname;
}
return pageInfo;
}
function flush() {
if (!pmAnalyticsEnabled) {
return;
}
if (_eventQueue.length > 1) {
var data = {
pageViewId: _pageViewId,
ver: _VERSION,
bundleId: initOptions.bundleId,
events: _eventQueue,
utmTags: collectUtmTagData(),
pageInfo: collectPageInfo(),
};
if ('version' in initOptions) {
data.version = initOptions.version;
}
if ('tcf_compliant' in initOptions) {
data.tcf_compliant = initOptions.tcf_compliant;
}
if ('sampling' in initOptions) {
data.sampling = initOptions.sampling;
}
ajax(
initOptions.url,
() => logInfo(`${analyticsName} sent events batch`),
_VERSION + ':' + JSON.stringify(data),
{
contentType: 'text/plain',
method: 'POST',
withCredentials: true
}
);
_eventQueue = [
_pageView
];
}
}
function trimAdUnit(adUnit) {
if (!adUnit) return adUnit;
const res = {};
res.code = adUnit.code;
res.sizes = adUnit.sizes;
return res;
}
function trimBid(bid) {
if (!bid) return bid;
const res = {};
res.auctionId = bid.auctionId;
res.bidder = bid.bidder;
res.bidderRequestId = bid.bidderRequestId;
res.bidId = bid.bidId;
res.crumbs = bid.crumbs;
res.cpm = bid.cpm;
res.currency = bid.currency;
res.mediaTypes = bid.mediaTypes;
res.sizes = bid.sizes;
res.transactionId = bid.transactionId;
res.adUnitCode = bid.adUnitCode;
res.bidRequestsCount = bid.bidRequestsCount;
res.serverResponseTimeMs = bid.serverResponseTimeMs;
return res;
}
function trimBidderRequest(bidderRequest) {
if (!bidderRequest) return bidderRequest;
const res = {};
res.auctionId = bidderRequest.auctionId;
res.auctionStart = bidderRequest.auctionStart;
res.bidderRequestId = bidderRequest.bidderRequestId;
res.bidderCode = bidderRequest.bidderCode;
res.bids = bidderRequest.bids && bidderRequest.bids.map(trimBid);
return res;
}
function handleEvent(eventType, eventArgs) {
if (eventArgs) {
eventArgs = hasNonSerializableProperty(eventArgs) ? eventArgs : deepClone(eventArgs)
} else {
eventArgs = {}
}
const pmEvent = {};
switch (eventType) {
case EVENTS.AUCTION_INIT: {
pmEvent.auctionId = eventArgs.auctionId;
pmEvent.timeout = eventArgs.timeout;
pmEvent.eventType = eventArgs.eventType;
pmEvent.adUnits = eventArgs.adUnits && eventArgs.adUnits.map(trimAdUnit)
pmEvent.bidderRequests = eventArgs.bidderRequests && eventArgs.bidderRequests.map(trimBidderRequest)
_startAuction = pmEvent.timestamp;
_bidRequestTimeout = pmEvent.timeout;
break;
}
case EVENTS.AUCTION_END: {
pmEvent.auctionId = eventArgs.auctionId;
pmEvent.end = eventArgs.end;
pmEvent.start = eventArgs.start;
pmEvent.adUnitCodes = eventArgs.adUnitCodes;
pmEvent.bidsReceived = eventArgs.bidsReceived && eventArgs.bidsReceived.map(trimBid);
pmEvent.start = _startAuction;
pmEvent.end = Date.now();
break;
}
case EVENTS.BID_ADJUSTMENT: {
break;
}
case EVENTS.BID_TIMEOUT: {
pmEvent.bidders = eventArgs && eventArgs.map ? eventArgs.map(trimBid) : eventArgs;
pmEvent.duration = _bidRequestTimeout;
break;
}
case EVENTS.BID_REQUESTED: {
pmEvent.auctionId = eventArgs.auctionId;
pmEvent.bidderCode = eventArgs.bidderCode;
pmEvent.doneCbCallCount = eventArgs.doneCbCallCount;
pmEvent.start = eventArgs.start;
pmEvent.bidderRequestId = eventArgs.bidderRequestId;
pmEvent.bids = eventArgs.bids && eventArgs.bids.map(trimBid);
pmEvent.auctionStart = eventArgs.auctionStart;
pmEvent.timeout = eventArgs.timeout;
break;
}
case EVENTS.BID_RESPONSE: {
pmEvent.bidderCode = eventArgs.bidderCode;
pmEvent.width = eventArgs.width;
pmEvent.height = eventArgs.height;
pmEvent.adId = eventArgs.adId;
pmEvent.mediaType = eventArgs.mediaType;
pmEvent.cpm = eventArgs.cpm;
pmEvent.currency = eventArgs.currency;
pmEvent.requestId = eventArgs.requestId;
pmEvent.adUnitCode = eventArgs.adUnitCode;
pmEvent.auctionId = eventArgs.auctionId;
pmEvent.timeToRespond = eventArgs.timeToRespond;
pmEvent.responseTimestamp = eventArgs.responseTimestamp;
pmEvent.requestTimestamp = eventArgs.requestTimestamp;
pmEvent.netRevenue = eventArgs.netRevenue;
pmEvent.size = eventArgs.size;
pmEvent.adserverTargeting = eventArgs.adserverTargeting;
break;
}
case EVENTS.BID_WON: {
pmEvent.auctionId = eventArgs.auctionId;
pmEvent.adId = eventArgs.adId;
pmEvent.adserverTargeting = eventArgs.adserverTargeting;
pmEvent.adUnitCode = eventArgs.adUnitCode;
pmEvent.bidderCode = eventArgs.bidderCode;
pmEvent.height = eventArgs.height;
pmEvent.mediaType = eventArgs.mediaType;
pmEvent.netRevenue = eventArgs.netRevenue;
pmEvent.cpm = eventArgs.cpm;
pmEvent.requestTimestamp = eventArgs.requestTimestamp;
pmEvent.responseTimestamp = eventArgs.responseTimestamp;
pmEvent.size = eventArgs.size;
pmEvent.width = eventArgs.width;
pmEvent.currency = eventArgs.currency;
pmEvent.bidder = eventArgs.bidder;
break;
}
case EVENTS.BIDDER_DONE: {
pmEvent.auctionId = eventArgs.auctionId;
pmEvent.auctionStart = eventArgs.auctionStart;
pmEvent.bidderCode = eventArgs.bidderCode;
pmEvent.bidderRequestId = eventArgs.bidderRequestId;
pmEvent.bids = eventArgs.bids && eventArgs.bids.map(trimBid);
pmEvent.doneCbCallCount = eventArgs.doneCbCallCount;
pmEvent.start = eventArgs.start;
pmEvent.timeout = eventArgs.timeout;
pmEvent.tid = eventArgs.tid;
pmEvent.src = eventArgs.src;
break;
}
case EVENTS.SET_TARGETING: {
break;
}
case EVENTS.REQUEST_BIDS: {
break;
}
case EVENTS.ADD_AD_UNITS: {
break;
}
case EVENTS.AD_RENDER_FAILED: {
pmEvent.bid = eventArgs.bid;
pmEvent.message = eventArgs.message;
pmEvent.reason = eventArgs.reason;
break;
}
default:
return;
}
pmEvent.eventType = eventType;
pmEvent.timestamp = pmEvent.timestamp || Date.now();
sendEvent(pmEvent);
}
function sendEvent(event) {
_eventQueue.push(event);
logInfo(`${analyticsName} Event ${event.eventType}:`, event);
if (event.eventType === EVENTS.AUCTION_END) {
flush();
}
}
adapterManager.registerAnalyticsAdapter({
adapter: prebidmanagerAnalytics,
code: 'prebidmanager'
});
prebidmanagerAnalytics.getOptions = function () {
return initOptions;
};
prebidmanagerAnalytics.flush = flush;
export default prebidmanagerAnalytics;