forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautomatadAnalyticsAdapter.js
336 lines (301 loc) · 12.2 KB
/
automatadAnalyticsAdapter.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
import {
logError,
logInfo,
logMessage
} from '../src/utils.js';
import { EVENTS } from '../src/constants.js';
import adapter from '../libraries/analyticsAdapter/AnalyticsAdapter.js';
import adapterManager from '../src/adapterManager.js';
import { config } from '../src/config.js'
/** Prebid Event Handlers */
const ADAPTER_CODE = 'automatadAnalytics'
const trialCountMilsMapping = [1500, 3000, 5000, 10000];
var isLoggingEnabled; var queuePointer = 0; var retryCount = 0; var timer = null; var __atmtdAnalyticsQueue = []; var qBeingUsed; var qTraversalComplete;
const prettyLog = (level, text, isGroup = false, cb = () => {}) => {
if (self.isLoggingEnabled === undefined) {
// TODO FIX THIS RULES VIOLATION
// eslint-disable-next-line prebid/no-global
if (window.localStorage.getItem('__aggLoggingEnabled')) {
self.isLoggingEnabled = true
} else {
const queryParams = new URLSearchParams(new URL(window.location.href).search)
self.isLoggingEnabled = queryParams.has('aggLoggingEnabled')
}
}
if (self.isLoggingEnabled) {
if (isGroup) {
logInfo(`ATD Analytics Adapter: ${level.toUpperCase()}: ${text} --- Group Start ---`)
try {
cb();
} catch (error) {
logError(`ATD Analytics Adapter: ERROR: ${'Error during cb function in prettyLog'}`)
}
logInfo(`ATD Analytics Adapter: ${level.toUpperCase()}: ${text} --- Group End ---`)
} else {
logInfo(`ATD Analytics Adapter: ${level.toUpperCase()}: ${text}`)
}
}
}
const processEvents = () => {
if (self.retryCount === trialCountMilsMapping.length) {
self.prettyLog('error', `Aggregator still hasn't loaded. Processing que stopped`, trialCountMilsMapping, self.retryCount)
return;
}
self.prettyLog('status', `Que has been inactive for a while. Adapter starting to process que now... Trial Count = ${self.retryCount + 1}`)
let shouldTryAgain = false
while (self.queuePointer < self.__atmtdAnalyticsQueue.length) {
const eventType = self.__atmtdAnalyticsQueue[self.queuePointer][0]
const args = self.__atmtdAnalyticsQueue[self.queuePointer][1]
try {
switch (eventType) {
case EVENTS.AUCTION_INIT:
if (window.atmtdAnalytics && window.atmtdAnalytics.auctionInitHandler) {
window.atmtdAnalytics.auctionInitHandler(args);
} else {
shouldTryAgain = true
}
break;
case EVENTS.BID_REQUESTED:
if (window.atmtdAnalytics && window.atmtdAnalytics.bidRequestedHandler) {
window.atmtdAnalytics.bidRequestedHandler(args);
}
break;
case EVENTS.BID_RESPONSE:
if (window.atmtdAnalytics && window.atmtdAnalytics.bidResponseHandler) {
window.atmtdAnalytics.bidResponseHandler(args);
}
break;
case EVENTS.BID_REJECTED:
if (window.atmtdAnalytics && window.atmtdAnalytics.bidRejectedHandler) {
window.atmtdAnalytics.bidRejectedHandler(args);
}
break;
case EVENTS.BIDDER_DONE:
if (window.atmtdAnalytics && window.atmtdAnalytics.bidderDoneHandler) {
window.atmtdAnalytics.bidderDoneHandler(args);
}
break;
case EVENTS.BID_WON:
if (window.atmtdAnalytics && window.atmtdAnalytics.bidWonHandler) {
window.atmtdAnalytics.bidWonHandler(args);
}
break;
case EVENTS.NO_BID:
if (window.atmtdAnalytics && window.atmtdAnalytics.noBidHandler) {
window.atmtdAnalytics.noBidHandler(args);
}
break;
case EVENTS.BID_TIMEOUT:
if (window.atmtdAnalytics && window.atmtdAnalytics.bidderTimeoutHandler) {
window.atmtdAnalytics.bidderTimeoutHandler(args);
}
break;
case EVENTS.AUCTION_DEBUG:
if (window.atmtdAnalytics && window.atmtdAnalytics.auctionDebugHandler) {
window.atmtdAnalytics.auctionDebugHandler(args);
}
break;
case 'slotRenderEnded':
if (window.atmtdAnalytics && window.atmtdAnalytics.slotRenderEndedGPTHandler) {
window.atmtdAnalytics.slotRenderEndedGPTHandler(args);
} else {
shouldTryAgain = true
}
break;
case 'impressionViewable':
if (window.atmtdAnalytics && window.atmtdAnalytics.impressionViewableHandler) {
window.atmtdAnalytics.impressionViewableHandler(args);
} else {
shouldTryAgain = true
}
break;
}
if (shouldTryAgain) break;
} catch (error) {
self.prettyLog('error', `Unhandled Error while processing ${eventType} of ${self.queuePointer}th index in the que. Will not be retrying this raw event ...`, true, () => {
logError(`The error is `, error)
})
}
self.queuePointer = self.queuePointer + 1
}
if (shouldTryAgain) {
if (trialCountMilsMapping[self.retryCount]) self.prettyLog('warn', `Adapter failed to process event as aggregator has not loaded. Retrying in ${trialCountMilsMapping[self.retryCount]}ms ...`);
setTimeout(self.processEvents, trialCountMilsMapping[self.retryCount])
self.retryCount = self.retryCount + 1
} else {
self.qBeingUsed = false
self.qTraversalComplete = true
}
}
const addGPTHandlers = () => {
const googletag = window.googletag || {}
googletag.cmd = googletag.cmd || []
googletag.cmd.push(() => {
googletag.pubads().addEventListener('slotRenderEnded', (event) => {
if (window.atmtdAnalytics && window.atmtdAnalytics.slotRenderEndedGPTHandler && !self.qBeingUsed) {
window.atmtdAnalytics.slotRenderEndedGPTHandler(event)
return;
}
self.__atmtdAnalyticsQueue.push(['slotRenderEnded', event])
self.prettyLog(`warn`, `Aggregator not initialised at auctionInit, exiting slotRenderEnded handler and pushing to que instead`)
})
googletag.pubads().addEventListener('impressionViewable', (event) => {
if (window.atmtdAnalytics && window.atmtdAnalytics.impressionViewableHandler && !self.qBeingUsed) {
window.atmtdAnalytics.impressionViewableHandler(event)
return;
}
self.__atmtdAnalyticsQueue.push(['impressionViewable', event])
self.prettyLog(`warn`, `Aggregator not initialised at auctionInit, exiting impressionViewable handler and pushing to que instead`)
})
})
}
const initializeQueue = () => {
self.__atmtdAnalyticsQueue.push = (args) => {
self.qBeingUsed = true
Array.prototype.push.apply(self.__atmtdAnalyticsQueue, [args]);
if (timer) {
clearTimeout(timer);
timer = null;
}
if (args[0] === EVENTS.AUCTION_INIT) {
const timeout = parseInt(config.getConfig('bidderTimeout')) + 1500
timer = setTimeout(() => {
self.processEvents()
}, timeout);
} else {
timer = setTimeout(() => {
self.processEvents()
}, 1500);
}
};
}
// ANALYTICS ADAPTER
let baseAdapter = adapter({analyticsType: 'bundle'});
let atmtdAdapter = Object.assign({}, baseAdapter, {
disableAnalytics() {
baseAdapter.disableAnalytics.apply(this, arguments);
},
track({eventType, args}) {
const shouldNotPushToQueue = !self.qBeingUsed
switch (eventType) {
case EVENTS.AUCTION_INIT:
if (window.atmtdAnalytics && window.atmtdAnalytics.auctionInitHandler && shouldNotPushToQueue) {
self.prettyLog('status', 'Aggregator loaded, initialising auction through handlers');
window.atmtdAnalytics.auctionInitHandler(args);
} else {
self.prettyLog('warn', 'Aggregator not loaded, initialising auction through que ...');
self.__atmtdAnalyticsQueue.push([eventType, args])
}
break;
case EVENTS.BID_REQUESTED:
if (window.atmtdAnalytics && window.atmtdAnalytics.bidRequestedHandler && shouldNotPushToQueue) {
window.atmtdAnalytics.bidRequestedHandler(args);
} else {
self.prettyLog('warn', `Aggregator not loaded, pushing ${eventType} to que instead ...`);
self.__atmtdAnalyticsQueue.push([eventType, args])
}
break;
case EVENTS.BID_REJECTED:
if (window.atmtdAnalytics && window.atmtdAnalytics.bidRejectedHandler && shouldNotPushToQueue) {
window.atmtdAnalytics.bidRejectedHandler(args);
} else {
self.prettyLog('warn', `Aggregator not loaded, pushing ${eventType} to que instead ...`);
self.__atmtdAnalyticsQueue.push([eventType, args])
}
break;
case EVENTS.BID_RESPONSE:
if (window.atmtdAnalytics && window.atmtdAnalytics.bidResponseHandler && shouldNotPushToQueue) {
window.atmtdAnalytics.bidResponseHandler(args);
} else {
self.prettyLog('warn', `Aggregator not loaded, pushing ${eventType} to que instead ...`);
self.__atmtdAnalyticsQueue.push([eventType, args])
}
break;
case EVENTS.BIDDER_DONE:
if (window.atmtdAnalytics && window.atmtdAnalytics.bidderDoneHandler && shouldNotPushToQueue) {
window.atmtdAnalytics.bidderDoneHandler(args);
} else {
self.prettyLog('warn', `Aggregator not loaded, pushing ${eventType} to que instead ...`);
self.__atmtdAnalyticsQueue.push([eventType, args])
}
break;
case EVENTS.BID_WON:
if (window.atmtdAnalytics && window.atmtdAnalytics.bidWonHandler && shouldNotPushToQueue) {
window.atmtdAnalytics.bidWonHandler(args);
} else {
self.prettyLog('warn', `Aggregator not loaded, pushing ${eventType} to que instead ...`);
self.__atmtdAnalyticsQueue.push([eventType, args])
}
break;
case EVENTS.NO_BID:
if (window.atmtdAnalytics && window.atmtdAnalytics.noBidHandler && shouldNotPushToQueue) {
window.atmtdAnalytics.noBidHandler(args);
} else {
self.prettyLog('warn', `Aggregator not loaded, pushing ${eventType} to que instead ...`);
self.__atmtdAnalyticsQueue.push([eventType, args])
}
break;
case EVENTS.AUCTION_DEBUG:
if (window.atmtdAnalytics && window.atmtdAnalytics.auctionDebugHandler && shouldNotPushToQueue) {
window.atmtdAnalytics.auctionDebugHandler(args);
} else {
self.prettyLog('warn', `Aggregator not loaded, pushing ${eventType} to que instead ...`);
self.__atmtdAnalyticsQueue.push([eventType, args])
}
break;
case EVENTS.BID_TIMEOUT:
if (window.atmtdAnalytics && window.atmtdAnalytics.bidderTimeoutHandler && shouldNotPushToQueue) {
window.atmtdAnalytics.bidderTimeoutHandler(args);
} else {
self.prettyLog('warn', `Aggregator not loaded, pushing ${eventType} to que instead ...`);
self.__atmtdAnalyticsQueue.push([eventType, args])
}
break;
}
}
});
atmtdAdapter.originEnableAnalytics = atmtdAdapter.enableAnalytics
atmtdAdapter.enableAnalytics = function (configuration) {
if ((configuration === undefined && typeof configuration !== 'object') || configuration.options === undefined) {
logError('A valid configuration must be passed to the Atmtd Analytics Adapter.');
return;
}
const conf = configuration.options
if (conf === undefined || typeof conf !== 'object' || conf.siteID === undefined || conf.publisherID === undefined) {
logError('A valid publisher ID and siteID must be passed to the Atmtd Analytics Adapter.');
return;
}
self.initializeQueue()
self.addGPTHandlers()
window.__atmtdSDKConfig = {
publisherID: conf.publisherID,
siteID: conf.siteID,
collectDebugMessages: conf.logDebug ? conf.logDebug : false
}
logMessage(`Automatad Analytics Adapter enabled with sdk config`, window.__atmtdSDKConfig)
// eslint-disable-next-line
atmtdAdapter.originEnableAnalytics(configuration)
};
/// /////////// ADAPTER REGISTRATION /////////////
adapterManager.registerAnalyticsAdapter({
adapter: atmtdAdapter,
code: ADAPTER_CODE
});
export var self = {
__atmtdAnalyticsQueue,
processEvents,
initializeQueue,
addGPTHandlers,
prettyLog,
queuePointer,
retryCount,
isLoggingEnabled,
qBeingUsed,
qTraversalComplete
}
window.__atmtdAnalyticsGlobalObject = {
q: self.__atmtdAnalyticsQueue,
qBeingUsed: self.qBeingUsed,
qTraversalComplete: self.qTraversalComplete
}
export default atmtdAdapter;