forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvRedAnalyticsAdapter.js
198 lines (170 loc) · 5.03 KB
/
advRedAnalyticsAdapter.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
import {generateUUID, 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 {EVENTS} from '../src/constants.js'
import {getRefererInfo} from '../src/refererDetection.js';
/**
* advRedAnalyticsAdapter.js - analytics adapter for AdvRed
*/
const DEFAULT_EVENT_URL = 'https://api.adv.red/api/event'
let ajax = ajaxBuilder(10000)
let pwId
let initOptions
let flushInterval
let queue = []
let advRedAnalytics = Object.assign(adapter({url: DEFAULT_EVENT_URL, analyticsType: 'endpoint'}), {
track({eventType, args}) {
handleEvent(eventType, args)
}
})
function sendEvents() {
if (queue.length > 0) {
const message = {
pwId: pwId,
publisherId: initOptions.publisherId,
events: queue,
pageUrl: getRefererInfo().page
}
queue = []
const url = initOptions.url ? initOptions.url : DEFAULT_EVENT_URL
ajax(
url,
() => logInfo('AdvRed Analytics sent ' + queue.length + ' events'),
JSON.stringify(message),
{
method: 'POST',
contentType: 'application/json',
withCredentials: true
}
)
}
}
function convertAdUnit(adUnit) {
if (!adUnit) return adUnit
const shortAdUnit = {}
shortAdUnit.code = adUnit.code
shortAdUnit.sizes = adUnit.sizes
return shortAdUnit
}
function convertBid(bid) {
if (!bid) return bid
const shortBid = {}
shortBid.adUnitCode = bid.adUnitCode
shortBid.bidder = bid.bidder
shortBid.cpm = bid.cpm
shortBid.currency = bid.currency
shortBid.mediaTypes = bid.mediaTypes
shortBid.sizes = bid.sizes
shortBid.serverResponseTimeMs = bid.serverResponseTimeMs
return shortBid
}
function convertAuctionInit(origEvent) {
let shortEvent = {}
shortEvent.auctionId = origEvent.auctionId
shortEvent.timeout = origEvent.timeout
shortEvent.adUnits = origEvent.adUnits && origEvent.adUnits.map(convertAdUnit)
return shortEvent
}
function convertBidRequested(origEvent) {
let shortEvent = {}
shortEvent.bidderCode = origEvent.bidderCode
shortEvent.bids = origEvent.bids && origEvent.bids.map(convertBid)
shortEvent.timeout = origEvent.timeout
return shortEvent
}
function convertBidTimeout(origEvent) {
let shortEvent = {}
shortEvent.bids = origEvent && origEvent.map ? origEvent.map(convertBid) : origEvent
return shortEvent
}
function convertBidderError(origEvent) {
let shortEvent = {}
shortEvent.bids = origEvent.bidderRequest && origEvent.bidderRequest.bids && origEvent.bidderRequest.bids.map(convertBid)
return shortEvent
}
function convertAuctionEnd(origEvent) {
let shortEvent = {}
shortEvent.adUnitCodes = origEvent.adUnitCodes
shortEvent.bidsReceived = origEvent.bidsReceived && origEvent.bidsReceived.map(convertBid)
shortEvent.noBids = origEvent.noBids && origEvent.noBids.map(convertBid)
return shortEvent
}
function convertBidWon(origEvent) {
let shortEvent = {}
shortEvent.adUnitCode = origEvent.adUnitCode
shortEvent.bidderCode = origEvent.bidderCode
shortEvent.mediaType = origEvent.mediaType
shortEvent.netRevenue = origEvent.netRevenue
shortEvent.cpm = origEvent.cpm
shortEvent.size = origEvent.size
shortEvent.currency = origEvent.currency
return shortEvent
}
function handleEvent(eventType, origEvent) {
try {
origEvent = origEvent ? JSON.parse(JSON.stringify(origEvent)) : {}
} catch (e) {
}
let shortEvent
switch (eventType) {
case EVENTS.AUCTION_INIT: {
shortEvent = convertAuctionInit(origEvent)
break
}
case EVENTS.BID_REQUESTED: {
shortEvent = convertBidRequested(origEvent)
break
}
case EVENTS.BID_TIMEOUT: {
shortEvent = convertBidTimeout(origEvent)
break
}
case EVENTS.BIDDER_ERROR: {
shortEvent = convertBidderError(origEvent)
break
}
case EVENTS.AUCTION_END: {
shortEvent = convertAuctionEnd(origEvent)
break
}
case EVENTS.BID_WON: {
shortEvent = convertBidWon(origEvent)
break
}
default:
return
}
shortEvent.eventType = eventType
shortEvent.auctionId = origEvent.auctionId
shortEvent.timestamp = origEvent.timestamp || Date.now()
sendEvent(shortEvent)
}
function sendEvent(event) {
queue.push(event)
if (event.eventType === EVENTS.AUCTION_END) {
sendEvents()
}
}
advRedAnalytics.originEnableAnalytics = advRedAnalytics.enableAnalytics
advRedAnalytics.enableAnalytics = function (config) {
initOptions = config.options || {}
pwId = generateUUID()
flushInterval = setInterval(sendEvents, 1000)
advRedAnalytics.originEnableAnalytics(config)
}
advRedAnalytics.originDisableAnalytics = advRedAnalytics.disableAnalytics
advRedAnalytics.disableAnalytics = function () {
clearInterval(flushInterval)
sendEvents()
advRedAnalytics.originDisableAnalytics()
}
adapterManager.registerAnalyticsAdapter({
adapter: advRedAnalytics,
code: 'advRed'
})
advRedAnalytics.getOptions = function () {
return initOptions
}
advRedAnalytics.sendEvents = sendEvents
export default advRedAnalytics