forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasteriobidAnalyticsAdapter.js
336 lines (306 loc) · 9.88 KB
/
asteriobidAnalyticsAdapter.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 { generateUUID, getParameterByName, logError, logInfo, parseUrl, deepClone, hasNonSerializableProperty } 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'
import {getRefererInfo} from '../src/refererDetection.js';
/**
* asteriobidAnalyticsAdapter.js - analytics adapter for AsterioBid
*/
export const storage = getStorageManager({ moduleType: MODULE_TYPE_ANALYTICS, moduleName: 'asteriobid' })
const DEFAULT_EVENT_URL = 'https://endpt.asteriobid.com/endpoint'
const analyticsType = 'endpoint'
const analyticsName = 'AsterioBid Analytics'
const utmTags = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content']
const _VERSION = 1
let ajax = ajaxBuilder(20000)
let initOptions
let auctionStarts = {}
let auctionTimeouts = {}
let sampling
let pageViewId
let flushInterval
let eventQueue = []
let asteriobidAnalyticsEnabled = false
let asteriobidAnalytics = Object.assign(adapter({ url: DEFAULT_EVENT_URL, analyticsType }), {
track({ eventType, args }) {
handleEvent(eventType, args)
}
})
asteriobidAnalytics.originEnableAnalytics = asteriobidAnalytics.enableAnalytics
asteriobidAnalytics.enableAnalytics = function (config) {
initOptions = config.options || {}
pageViewId = initOptions.pageViewId || generateUUID()
sampling = initOptions.sampling || 1
if (Math.floor(Math.random() * sampling) === 0) {
asteriobidAnalyticsEnabled = true
flushInterval = setInterval(flush, 1000)
} else {
logInfo(`${analyticsName} isn't enabled because of sampling`)
}
asteriobidAnalytics.originEnableAnalytics(config)
}
asteriobidAnalytics.originDisableAnalytics = asteriobidAnalytics.disableAnalytics
asteriobidAnalytics.disableAnalytics = function () {
if (!asteriobidAnalyticsEnabled) {
return
}
flush()
clearInterval(flushInterval)
asteriobidAnalytics.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
}
const refererInfo = getRefererInfo()
pageInfo.page = refererInfo.page
pageInfo.ref = refererInfo.ref
return pageInfo
}
function flush() {
if (!asteriobidAnalyticsEnabled) {
return
}
if (eventQueue.length > 0) {
const data = {
pageViewId: pageViewId,
ver: _VERSION,
bundleId: initOptions.bundleId,
events: eventQueue,
utmTags: collectUtmTagData(),
pageInfo: collectPageInfo(),
sampling: sampling
}
eventQueue = []
if ('version' in initOptions) {
data.version = initOptions.version
}
if ('tcf_compliant' in initOptions) {
data.tcf_compliant = initOptions.tcf_compliant
}
if ('adUnitDict' in initOptions) {
data.adUnitDict = initOptions.adUnitDict;
}
if ('customParam' in initOptions) {
data.customParam = initOptions.customParam;
}
const url = initOptions.url ? initOptions.url : DEFAULT_EVENT_URL
ajax(
url,
() => logInfo(`${analyticsName} sent events batch`),
_VERSION + ':' + JSON.stringify(data),
{
contentType: 'text/plain',
method: 'POST',
withCredentials: true
}
)
}
}
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 (!asteriobidAnalyticsEnabled) {
return
}
if (eventArgs) {
eventArgs = hasNonSerializableProperty(eventArgs) ? eventArgs : deepClone(eventArgs)
} else {
eventArgs = {}
}
const pmEvent = {}
pmEvent.timestamp = eventArgs.timestamp || Date.now()
pmEvent.eventType = eventType
switch (eventType) {
case EVENTS.AUCTION_INIT: {
pmEvent.auctionId = eventArgs.auctionId
pmEvent.timeout = eventArgs.timeout
pmEvent.adUnits = eventArgs.adUnits && eventArgs.adUnits.map(trimAdUnit)
pmEvent.bidderRequests = eventArgs.bidderRequests && eventArgs.bidderRequests.map(trimBidderRequest)
auctionStarts[pmEvent.auctionId] = pmEvent.timestamp
auctionTimeouts[pmEvent.auctionId] = 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 = auctionStarts[pmEvent.auctionId]
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 = auctionTimeouts[pmEvent.auctionId]
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.requestTimestamp = eventArgs.requestTimestamp
pmEvent.responseTimestamp = eventArgs.responseTimestamp
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
}
sendEvent(pmEvent)
}
function sendEvent(event) {
eventQueue.push(event)
logInfo(`${analyticsName} Event ${event.eventType}:`, event)
if (event.eventType === EVENTS.AUCTION_END) {
flush()
}
}
adapterManager.registerAnalyticsAdapter({
adapter: asteriobidAnalytics,
code: 'asteriobid'
})
asteriobidAnalytics.getOptions = function () {
return initOptions
}
asteriobidAnalytics.flush = flush
export default asteriobidAnalytics