forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscaleableAnalyticsAdapter.js
203 lines (167 loc) · 4.76 KB
/
scaleableAnalyticsAdapter.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
/* COPYRIGHT SCALEABLE LLC 2019 */
import { ajax } from '../src/ajax.js';
import { EVENTS } from '../src/constants.js';
import adapter from '../libraries/analyticsAdapter/AnalyticsAdapter.js';
import adapterManager from '../src/adapterManager.js';
import { logMessage } from '../src/utils.js';
// Object.entries polyfill
const entries = Object.entries || function(obj) {
const ownProps = Object.keys(obj);
let i = ownProps.length;
let resArray = new Array(i); // preallocate the Array
while (i--) { resArray[i] = [ownProps[i], obj[ownProps[i]]]; }
return resArray;
};
const BID_TIMEOUT = EVENTS.BID_TIMEOUT;
const AUCTION_INIT = EVENTS.AUCTION_INIT;
const BID_WON = EVENTS.BID_WON;
const AUCTION_END = EVENTS.AUCTION_END;
const URL = 'https://auction.scaleable.ai/';
const ANALYTICS_TYPE = 'endpoint';
let auctionData = {};
let scaleableAnalytics = Object.assign({},
adapter({
URL,
ANALYTICS_TYPE
}),
{
// Override AnalyticsAdapter functions by supplying custom methods
track({ eventType, args }) {
switch (eventType) {
case AUCTION_INIT:
onAuctionInit(args);
break;
case AUCTION_END:
onAuctionEnd(args);
break;
case BID_WON:
onBidWon(args);
break;
case BID_TIMEOUT:
onBidTimeout(args);
break;
default:
break;
}
}
}
);
scaleableAnalytics.config = {};
scaleableAnalytics.originEnableAnalytics = scaleableAnalytics.enableAnalytics;
scaleableAnalytics.enableAnalytics = config => {
scaleableAnalytics.config = config;
scaleableAnalytics.originEnableAnalytics(config);
scaleableAnalytics.enableAnalytics = function _enable() {
return logMessage(`Analytics adapter for "${global}" already enabled, unnecessary call to \`enableAnalytics\`.`);
};
}
scaleableAnalytics.getAuctionData = () => {
return auctionData;
};
const sendDataToServer = data => ajax(URL, () => {}, JSON.stringify(data));
// Track auction initiated
const onAuctionInit = args => {
const config = scaleableAnalytics.config || {options: {}};
let adunitObj = {};
let adunits = [];
// Loop through adunit codes first
args.adUnitCodes.forEach((code) => {
adunitObj[code] = [{
bidder: 'scaleable_adunit_request'
}]
});
// Loop through bidder requests and bids
args.bidderRequests.forEach((bidderObj) => {
bidderObj.bids.forEach((bidObj) => {
adunitObj[bidObj.adUnitCode].push({
bidder: bidObj.bidder,
params: bidObj.params
})
});
});
entries(adunitObj).forEach(([adunitCode, bidRequests]) => {
adunits.push({
code: adunitCode,
bidRequests: bidRequests
});
});
const data = {
event: 'request',
site: config.options.site,
adunits: adunits
}
sendDataToServer(data);
}
// Handle all events besides requests and wins
const onAuctionEnd = args => {
const config = scaleableAnalytics.config || {options: {}};
let adunitObj = {};
let adunits = [];
// Add Bids Received
args.bidsReceived.forEach((bidObj) => {
if (!adunitObj[bidObj.adUnitCode]) { adunitObj[bidObj.adUnitCode] = []; }
adunitObj[bidObj.adUnitCode].push({
bidder: bidObj.bidderCode || bidObj.bidder,
cpm: bidObj.cpm,
currency: bidObj.currency,
dealId: bidObj.dealId,
type: bidObj.mediaType,
ttr: bidObj.timeToRespond,
size: bidObj.size
});
});
// Add in other data (timeouts) as we push to adunits
entries(adunitObj).forEach(([adunitCode, bidsReceived]) => {
const bidData = bidsReceived.concat(auctionData[adunitCode] || []);
adunits.push({
code: adunitCode,
bidData: bidData
});
delete auctionData[adunitCode];
});
// Add in any missed auction data
entries(auctionData).forEach(([adunitCode, bidData]) => {
adunits.push({
code: adunitCode,
bidData: bidData
})
});
const data = {
event: 'bids',
site: config.options.site,
adunits: adunits
}
if (adunits.length) { sendDataToServer(data); }
// Reset auctionData
auctionData = {}
}
// Bid Win Events occur after auction end
const onBidWon = args => {
const config = scaleableAnalytics.config || {options: {}};
const data = {
event: 'win',
site: config.options.site,
adunit: args.adUnitCode,
code: args.bidderCode,
cpm: args.cpm,
ttr: args.timeToRespond,
params: args.params
};
sendDataToServer(data);
}
const onBidTimeout = args => {
args.forEach(currObj => {
if (!auctionData[currObj.adUnitCode]) {
auctionData[currObj.adUnitCode] = []
}
auctionData[currObj.adUnitCode].push({
timeouts: 1,
bidder: currObj.bidder
});
});
}
adapterManager.registerAnalyticsAdapter({
adapter: scaleableAnalytics,
code: 'scaleable'
})
export default scaleableAnalytics;