forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhadronAnalyticsAdapter.js
202 lines (167 loc) · 4.62 KB
/
hadronAnalyticsAdapter.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
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 {MODULE_TYPE_ANALYTICS} from '../src/activities/modules.js';
/**
* hadronAnalyticsAdapter.js - Audigent Hadron Analytics Adapter
*/
const HADRON_ANALYTICS_URL = 'https://analytics.hadron.ad.gt/api/v1/analytics';
const HADRONID_ANALYTICS_VER = 'pbadgt0';
const DEFAULT_PARTNER_ID = 0;
const AU_GVLID = 561;
const MODULE_CODE = 'hadronAnalytics';
export const storage = getStorageManager({moduleType: MODULE_TYPE_ANALYTICS, moduleName: MODULE_CODE});
var viewId = utils.generateUUID();
var partnerId = DEFAULT_PARTNER_ID;
var eventsToTrack = [];
var w = window;
var d = document;
var e = d.documentElement;
var g = d.getElementsByTagName('body')[0];
var x = w.innerWidth || e.clientWidth || g.clientWidth;
var y = w.innerHeight || e.clientHeight || g.clientHeight;
var pageView = {
eventType: 'pageView',
userAgent: window.navigator.userAgent,
timestamp: Date.now(),
timezoneOffset: new Date().getTimezoneOffset(),
language: window.navigator.language,
vendor: window.navigator.vendor,
pageUrl: getRefererInfo().page,
screenWidth: x,
screenHeight: y
};
var eventQueue = [
pageView
];
var startAuction = 0;
var bidRequestTimeout = 0;
let analyticsType = 'endpoint';
let hadronAnalyticsAdapter = Object.assign(adapter({url: HADRON_ANALYTICS_URL, analyticsType}), {
track({eventType, args}) {
args = args ? utils.deepClone(args) : {};
var data = {};
if (!eventsToTrack.includes(eventType)) return;
switch (eventType) {
case EVENTS.AUCTION_INIT: {
data = args;
startAuction = data.timestamp;
bidRequestTimeout = data.timeout;
break;
}
case EVENTS.AUCTION_END: {
data = args;
data.start = startAuction;
data.end = Date.now();
break;
}
case EVENTS.BID_ADJUSTMENT: {
data.bidders = args;
break;
}
case EVENTS.BID_TIMEOUT: {
data.bidders = args;
data.duration = bidRequestTimeout;
break;
}
case EVENTS.BID_REQUESTED: {
data = args;
break;
}
case EVENTS.BID_RESPONSE: {
data = args;
delete data.ad;
break;
}
case EVENTS.BID_WON: {
data = args;
delete data.ad;
delete data.adUrl;
break;
}
case EVENTS.BIDDER_DONE: {
data = args;
break;
}
case EVENTS.SET_TARGETING: {
data.targetings = args;
break;
}
case EVENTS.REQUEST_BIDS: {
data = args;
break;
}
case EVENTS.ADD_AD_UNITS: {
data = args;
break;
}
case EVENTS.AD_RENDER_FAILED: {
data = args;
break;
}
default:
return;
}
data.eventType = eventType;
data.timestamp = data.timestamp || Date.now();
sendEvent(data);
}
});
hadronAnalyticsAdapter.originEnableAnalytics = hadronAnalyticsAdapter.enableAnalytics;
hadronAnalyticsAdapter.enableAnalytics = function(conf = {}) {
if (typeof conf.options === 'object') {
if (conf.options.partnerId) {
partnerId = conf.options.partnerId;
} else {
partnerId = DEFAULT_PARTNER_ID;
}
if (conf.options.eventsToTrack) {
eventsToTrack = conf.options.eventsToTrack;
}
} else {
utils.logError('HADRON_ANALYTICS_NO_CONFIG_ERROR');
return;
}
hadronAnalyticsAdapter.originEnableAnalytics(conf);
};
function flush() {
// Don't send anything if no partner id was declared
if (partnerId === DEFAULT_PARTNER_ID) return;
if (eventQueue.length > 1) {
var data = {
pageViewId: viewId,
ver: HADRONID_ANALYTICS_VER,
partnerId: partnerId,
events: eventQueue
};
ajax(HADRON_ANALYTICS_URL,
() => utils.logInfo('HADRON_ANALYTICS_BATCH_SEND'),
JSON.stringify(data),
{
contentType: 'application/json',
method: 'POST'
}
);
eventQueue = [
pageView
];
}
}
function sendEvent(event) {
eventQueue.push(event);
utils.logInfo(`HADRON_ANALYTICS_EVENT ${event.eventType} `, event);
if (event.eventType === EVENTS.AUCTION_END) {
flush();
}
}
adapterManager.registerAnalyticsAdapter({
adapter: hadronAnalyticsAdapter,
code: MODULE_CODE,
gvlid: AU_GVLID
});
hadronAnalyticsAdapter.flush = flush;
export default hadronAnalyticsAdapter;