This repository has been archived by the owner on Sep 19, 2023. It is now read-only.
forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathga.js
244 lines (206 loc) · 6.5 KB
/
ga.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
/**
* ga.js - analytics adapter for google analytics
*/
var events = require('./events');
var utils = require('./utils');
var CONSTANTS = require('./constants.json');
var BID_REQUESTED = CONSTANTS.EVENTS.BID_REQUESTED;
var BID_TIMEOUT = CONSTANTS.EVENTS.BID_TIMEOUT;
var BID_RESPONSE = CONSTANTS.EVENTS.BID_RESPONSE;
var BID_WON = CONSTANTS.EVENTS.BID_WON;
var _disibleInteraction = { nonInteraction: true };
var _analyticsQueue = [];
var _gaGlobal = null;
var _enableCheck = true;
var _category = 'Prebid.js Bids';
var _eventCount = 0;
var _enableDistribution = false;
var _timedOutBidders = [];
/**
* This will enable sending data to google analytics. Only call once, or duplicate data will be sent!
* @param {object} gaOptions to set distribution and GA global (if renamed);
* @return {[type]} [description]
*/
exports.enableAnalytics = function (gaOptions) {
if (typeof gaOptions.global !== 'undefined') {
_gaGlobal = gaOptions.global;
} else {
//default global is window.ga
_gaGlobal = 'ga';
}
if (typeof gaOptions.enableDistribution !== 'undefined') {
_enableDistribution = gaOptions.enableDistribution;
}
var bid = null;
//first send all events fired before enableAnalytics called
var existingEvents = events.getEvents();
utils._each(existingEvents, function (eventObj) {
var args = eventObj.args;
if (!eventObj) {
return;
}
if (eventObj.eventType === BID_REQUESTED) {
//bid is 1st args
bid = args[0];
sendBidRequestToGa(bid);
} else if (eventObj.eventType === BID_RESPONSE) {
//bid is 2nd args
bid = args[1];
sendBidResponseToGa(bid);
} else if (eventObj.eventType === BID_TIMEOUT) {
_timedOutBidders = args[0];
} else if (eventObj.eventType === BID_WON) {
bid = args[0];
sendBidWonToGa(bid);
}
});
//Next register event listeners to send data immediately
//bidRequests
events.on(BID_REQUESTED, function (bidRequestObj) {
sendBidRequestToGa(bidRequestObj);
});
//bidResponses
events.on(BID_RESPONSE, function (adunit, bid) {
sendBidResponseToGa(bid);
sendBidTimeouts(bid);
});
//bidTimeouts
events.on(BID_TIMEOUT, function (bidderArray) {
_timedOutBidders = bidderArray;
});
//wins
events.on(BID_WON, function (bid) {
sendBidWonToGa(bid);
});
};
/**
* Check if gaGlobal or window.ga is defined on page. If defined execute all the GA commands
*/
function checkAnalytics() {
if (_enableCheck && typeof window[_gaGlobal] === 'function') {
for (var i = 0; i < _analyticsQueue.length; i++) {
_analyticsQueue[i].call();
}
//override push to execute the command immediately from now on
_analyticsQueue.push = function (fn) {
fn.call();
};
//turn check into NOOP
_enableCheck = false;
}
utils.logMessage('event count sent to GA: ' + _eventCount);
}
function convertToCents(dollars) {
if (dollars) {
return Math.floor(dollars * 100);
}
return 0;
}
function getLoadTimeDistribution(time) {
var distribution;
if (time >= 0 && time < 200) {
distribution = '0-200ms';
} else if (time >= 200 && time < 300) {
distribution = '200-300ms';
} else if (time >= 300 && time < 400) {
distribution = '300-400ms';
} else if (time >= 400 && time < 500) {
distribution = '400-500ms';
} else if (time >= 500 && time < 600) {
distribution = '500-600ms';
} else if (time >= 600 && time < 800) {
distribution = '600-800ms';
} else if (time >= 800 && time < 1000) {
distribution = '800-1000ms';
} else if (time >= 1000 && time < 1200) {
distribution = '1000-1200ms';
} else if (time >= 1200 && time < 1500) {
distribution = '1200-1500ms';
} else if (time >= 1500 && time < 2000) {
distribution = '1500-2000ms';
} else if (time >= 2000) {
distribution = '2000ms above';
}
return distribution;
}
function getCpmDistribution(cpm) {
var distribution;
if (cpm >= 0 && cpm < 0.5) {
distribution = '$0-0.5';
} else if (cpm >= 0.5 && cpm < 1) {
distribution = '$0.5-1';
} else if (cpm >= 1 && cpm < 1.5) {
distribution = '$1-1.5';
} else if (cpm >= 1.5 && cpm < 2) {
distribution = '$1.5-2';
} else if (cpm >= 2 && cpm < 2.5) {
distribution = '$2-2.5';
} else if (cpm >= 2.5 && cpm < 3) {
distribution = '$2.5-3';
} else if (cpm >= 3 && cpm < 4) {
distribution = '$3-4';
} else if (cpm >= 4 && cpm < 6) {
distribution = '$4-6';
} else if (cpm >= 6 && cpm < 8) {
distribution = '$6-8';
} else if (cpm >= 8) {
distribution = '$8 above';
}
return distribution;
}
function sendBidRequestToGa(bid) {
if (bid && bid.bidderCode) {
_analyticsQueue.push(function () {
_eventCount++;
window[_gaGlobal]('send', 'event', _category, 'Requests', bid.bidderCode, 1, _disibleInteraction);
});
}
//check the queue
checkAnalytics();
}
function sendBidResponseToGa(bid) {
if (bid && bid.bidderCode) {
_analyticsQueue.push(function () {
var cpmCents = convertToCents(bid.cpm);
var bidder = bid.bidderCode;
if (typeof bid.timeToRespond !== 'undefined' && _enableDistribution) {
_eventCount++;
var dis = getLoadTimeDistribution(bid.timeToRespond);
window[_gaGlobal]('send', 'event', 'Prebid.js Load Time Distribution', dis, bidder, 1, _disibleInteraction);
}
if (bid.cpm > 0) {
_eventCount = _eventCount + 2;
var cpmDis = getCpmDistribution(bid.cpm);
if (_enableDistribution) {
_eventCount++;
window[_gaGlobal]('send', 'event', 'Prebid.js CPM Distribution', cpmDis, bidder, 1, _disibleInteraction);
}
window[_gaGlobal]('send', 'event', _category, 'Bids', bidder, cpmCents, _disibleInteraction);
window[_gaGlobal]('send', 'event', _category, 'Bid Load Time', bidder, bid.timeToRespond, _disibleInteraction);
}
});
}
//check the queue
checkAnalytics();
}
function sendBidTimeouts(bid) {
if (bid && bid.bidder) {
_analyticsQueue.push(function () {
utils._each(_timedOutBidders, function (bidderCode) {
if (bid.bidder === bidderCode) {
_eventCount++;
window[_gaGlobal]('send', 'event', _category, 'Timeouts', bidderCode, bid.timeToRespond, _disibleInteraction);
}
});
});
}
checkAnalytics();
}
function sendBidWonToGa(bid) {
var cpmCents = convertToCents(bid.cpm);
_analyticsQueue.push(function () {
_eventCount++;
window[_gaGlobal]('send', 'event', _category, 'Wins', bid.bidderCode, cpmCents, _disibleInteraction);
});
checkAnalytics();
}