forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrowthCodeAnalyticsAdapter.js
185 lines (152 loc) · 4.42 KB
/
growthCodeAnalyticsAdapter.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
/**
* growthCodeAnalyticsAdapter.js - GrowthCode Analytics Adapter
*/
import { ajax } from '../src/ajax.js';
import adapter from '../libraries/analyticsAdapter/AnalyticsAdapter.js';
import adapterManager from '../src/adapterManager.js';
import * as utils from '../src/utils.js';
import { EVENTS } from '../src/constants.js';
import {getStorageManager} from '../src/storageManager.js';
import {getRefererInfo} from '../src/refererDetection.js';
import {logError, logInfo} from '../src/utils.js';
import {MODULE_TYPE_ANALYTICS} from '../src/activities/modules.js';
const MODULE_NAME = 'growthCodeAnalytics';
const DEFAULT_PID = 'INVALID_PID'
const ENDPOINT_URL = 'https://analytics.gcprivacy.com/v3/pb/analytics'
export const storage = getStorageManager({moduleType: MODULE_TYPE_ANALYTICS, moduleName: MODULE_NAME});
let sessionId = utils.generateUUID();
let trackEvents = [];
let pid = DEFAULT_PID;
let url = ENDPOINT_URL;
let eventQueue = [];
let startAuction = 0;
let bidRequestTimeout = 0;
let analyticsType = 'endpoint';
let growthCodeAnalyticsAdapter = Object.assign(adapter({url: url, analyticsType}), {
track({eventType, args}) {
let eventData = args ? utils.deepClone(args) : {};
let data = {};
if (!trackEvents.includes(eventType)) return;
switch (eventType) {
case EVENTS.AUCTION_INIT: {
data = eventData;
startAuction = data.timestamp;
bidRequestTimeout = data.timeout;
break;
}
case EVENTS.AUCTION_END: {
data = eventData;
data.start = startAuction;
data.end = Date.now();
break;
}
case EVENTS.BID_ADJUSTMENT: {
data.bidders = eventData;
break;
}
case EVENTS.BID_TIMEOUT: {
data.bidders = eventData;
data.duration = bidRequestTimeout;
break;
}
case EVENTS.BID_REQUESTED: {
data = eventData;
break;
}
case EVENTS.BID_RESPONSE: {
data = eventData;
delete data.ad;
break;
}
case EVENTS.BID_WON: {
data = eventData;
delete data.ad;
delete data.adUrl;
break;
}
case EVENTS.BIDDER_DONE: {
data = eventData;
break;
}
case EVENTS.SET_TARGETING: {
data.targetings = eventData;
break;
}
case EVENTS.REQUEST_BIDS: {
data = eventData;
break;
}
case EVENTS.ADD_AD_UNITS: {
data = eventData;
break;
}
case EVENTS.NO_BID: {
data = eventData
break;
}
default:
return;
}
data.eventType = eventType;
data.timestamp = data.timestamp || Date.now();
sendEvent(data);
}
});
growthCodeAnalyticsAdapter.originEnableAnalytics = growthCodeAnalyticsAdapter.enableAnalytics;
growthCodeAnalyticsAdapter.enableAnalytics = function(conf = {}) {
if (typeof conf.options === 'object') {
if (conf.options.pid) {
pid = conf.options.pid;
url = conf.options.url ? conf.options.url : ENDPOINT_URL;
} else {
logError(MODULE_NAME + ' Not a valid PartnerID')
return
}
if (conf.options.trackEvents) {
trackEvents = conf.options.trackEvents;
}
} else {
logError(MODULE_NAME + ' Invalid configuration');
return;
}
growthCodeAnalyticsAdapter.originEnableAnalytics(conf);
};
function logToServer() {
if (pid === DEFAULT_PID) return;
if (eventQueue.length >= 1) {
// Get the correct GCID
let gcid = storage.getDataFromLocalStorage('gcid');
let data = {
session: sessionId,
pid: pid,
gcid: gcid,
timestamp: Date.now(),
url: getRefererInfo().page,
referer: document.referrer,
events: eventQueue
};
ajax(url, {
success: response => {
logInfo(MODULE_NAME + ' Send Data to Server')
},
error: error => {
logInfo(MODULE_NAME + ' Problem Send Data to Server: ' + error)
}
}, JSON.stringify(data), {method: 'POST', withCredentials: true})
eventQueue = [
];
}
}
function sendEvent(event) {
eventQueue.push(event);
logInfo(MODULE_NAME + 'Analytics Event: ' + event);
if ((event.eventType === EVENTS.AUCTION_END) || (event.eventType === EVENTS.BID_WON)) {
logToServer();
}
}
adapterManager.registerAnalyticsAdapter({
adapter: growthCodeAnalyticsAdapter,
code: 'growthCodeAnalytics'
});
growthCodeAnalyticsAdapter.logToServer = logToServer;
export default growthCodeAnalyticsAdapter;