-
Notifications
You must be signed in to change notification settings - Fork 0
/
googleTagManagerAnalyticsAdapter.js
266 lines (228 loc) · 8.81 KB
/
googleTagManagerAnalyticsAdapter.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
/**
* googleTagManagerAnalyticsAdapter.js - analytics adapter for analytics through Google Tag Manager
*/
var events = require('src/events');
var utils = require('src/utils');
var CONSTANTS = require('src/constants.json');
var adaptermanager = require('src/adaptermanager');
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 _disableInteraction = { nonInteraction: true };
var _analyticsQueue = [];
var _datalayerGlobal = null;
var _enableCheck = true;
var _category = 'Prebid.js Bids';
var _eventCount = 0;
var _enableDistribution = false;
var _eventName = null;
var _sampled = true;
/**
* This will enable sending data to google analytics. Only call once, or duplicate data will be sent!
* @param {object} provider use to set GA global (if renamed);
* @param {object} options use to configure adapter;
* @return {[type]} [description]
*/
exports.enableAnalytics = function ({ provider, options }) {
_datalayerGlobal = 'dataLayer';
_eventName = options && options.eventName ? options.eventName : 'GAEvent';
_sampled = typeof options === 'undefined' || typeof options.sampling === 'undefined' ||
Math.random() < parseFloat(options.sampling);
if (options && typeof options.global !== 'undefined') {
_datalayerGlobal = options.global;
}
if (options && typeof options.enableDistribution !== 'undefined') {
_enableDistribution = options.enableDistribution;
}
var bid = null;
if (_sampled) {
// first send all events fired before enableAnalytics called
var existingEvents = events.getEvents();
utils._each(existingEvents, function (eventObj) {
if (typeof eventObj !== 'object') {
return;
}
var args = eventObj.args;
if (eventObj.eventType === BID_REQUESTED) {
bid = args;
sendBidRequestToGa(bid);
} else if (eventObj.eventType === BID_RESPONSE) {
// bid is 2nd args
bid = args;
sendBidResponseToGa(bid);
} else if (eventObj.eventType === BID_TIMEOUT) {
const bidderArray = args;
sendBidTimeouts(bidderArray);
} else if (eventObj.eventType === BID_WON) {
bid = args;
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 (bid) {
sendBidResponseToGa(bid);
});
// bidTimeouts
events.on(BID_TIMEOUT, function (bidderArray) {
sendBidTimeouts(bidderArray);
});
// wins
events.on(BID_WON, function (bid) {
sendBidWonToGa(bid);
});
} else {
utils.logMessage('Prebid.js google analytics disabled by sampling');
}
// finally set this function to return log message, prevents multiple adapter listeners
this.enableAnalytics = function _enable() {
return utils.logMessage(`Analytics adapter already enabled, unnecessary call to \`enableAnalytics\`.`);
};
};
exports.getTrackerSend = function getTrackerSend() {
return _eventName;
};
/**
* Check if gaGlobal or window.ga is defined on page. If defined execute all the GA commands
*/
function checkAnalytics() {
if (_enableCheck && Array.isArray(window[_datalayerGlobal]) {
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 = '0200-300ms';
} else if (time >= 300 && time < 400) {
distribution = '0300-400ms';
} else if (time >= 400 && time < 500) {
distribution = '0400-500ms';
} else if (time >= 500 && time < 600) {
distribution = '0500-600ms';
} else if (time >= 600 && time < 800) {
distribution = '0600-800ms';
} else if (time >= 800 && time < 1000) {
distribution = '0800-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[_datalayerGlobal].push({event: _eventName, eventCategory: _category, eventAction: 'Requests', eventLabel: bid.bidderCode, eventValue: 1, eventNonInteraction: _disableInteraction});
//_eventName, 'event', _category, 'Requests', bid.bidderCode, 1, _disableInteraction);
});
}
// 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[_datalayerGlobal](_eventName, 'event', 'Prebid.js Load Time Distribution', dis, bidder, 1, _disableInteraction);
window[_datalayerGlobal].push({event: _eventName, eventCategory: 'Prebid.js Load Time Distribution', eventAction: dis, eventLabel: bidder, eventValue: 1, eventNonInteraction: _disableInteraction});
}
if (bid.cpm > 0) {
_eventCount = _eventCount + 2;
var cpmDis = getCpmDistribution(bid.cpm);
if (_enableDistribution) {
_eventCount++;
//window[_datalayerGlobal](_eventName, 'event', 'Prebid.js CPM Distribution', cpmDis, bidder, 1, _disableInteraction);
window[_datalayerGlobal].push({event: _eventName, eventCategory: 'Prebid.js CPM Distribution', eventAction: cpmDis, eventLabel: bidder, eventValue: 1, eventNonInteraction: _disableInteraction});
}
//window[_datalayerGlobal](_eventName, 'event', _category, 'Bids', bidder, cpmCents, _disableInteraction);
window[_datalayerGlobal].push({event: _eventName, eventCategory: _category, eventAction: 'Bids', eventLabel: bidder, eventValue: cpmCents, eventNonInteraction: _disableInteraction});
//window[_datalayerGlobal](_eventName, 'event', _category, 'Bid Load Time', bidder, bid.timeToRespond, _disableInteraction);
window[_datalayerGlobal].push({event: _eventName, eventCategory: _category, eventAction: 'Bid Load Time', eventLabel: bidder, eventValue: bid.timeToRespond, eventNonInteraction: _disableInteraction});
}
});
}
// check the queue
checkAnalytics();
}
function sendBidTimeouts(timedOutBidders) {
_analyticsQueue.push(function () {
utils._each(timedOutBidders, function (bidderCode) {
_eventCount++;
var bidderName = bidderCode.bidder;
//window[_datalayerGlobal](_eventName, 'event', _category, 'Timeouts', bidderName, _disableInteraction);
window[_datalayerGlobal].push({event: _eventName, eventCategory: _category, eventAction: 'Timeouts', eventLabel: bidderName, eventNonInteraction: _disableInteraction});
});
});
checkAnalytics();
}
function sendBidWonToGa(bid) {
var cpmCents = convertToCents(bid.cpm);
_analyticsQueue.push(function () {
_eventCount++;
//window[_datalayerGlobal](_eventName, 'event', _category, 'Wins', bid.bidderCode, cpmCents, _disableInteraction);
window[_datalayerGlobal].push({event: _eventName, eventCategory: _category, eventAction: 'Wins', eventLabel: bid.bidderCode, eventValue: cpmCents, eventNonInteraction: _disableInteraction});
});
checkAnalytics();
}
adaptermanager.registerAnalyticsAdapter({
adapter: exports,
code: 'gtm'
});