-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
analytics.js
130 lines (118 loc) · 3.25 KB
/
analytics.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
function log() {
if (window.DEBUG) {
console.log(Date.now(), ...arguments);
}
}
/* global ga */
// eslint-disable-next-line max-params
export function trackEvent(category, action, label, value) {
if (window.DEBUG) {
log('trackevent', category, action, label, value);
return;
}
sendTrackMessage('event', category, action, label, value);
}
async function sendTrackMessage(type, category, action, label, value) {
if (location.href.indexOf('chrome-extension://') !== -1) {
await postAnalyticsDataByApi(type, category, action, label);
} else {
if (window.ga) {
ga('send', type, category, action, label, value);
} else {
setTimeout(
() => sendTrackMessage(type, category, action, label, value),
100,
);
}
}
}
function getCustomerId() {
let cid = localStorage.getItem('cid');
if (cid) {
return cid;
}
cid =
Math.floor(Math.random() * 0x7fffffff) +
'.' +
Math.floor(Date.now() / 1000);
localStorage.setItem('cid', cid);
return cid;
}
async function postAnalyticsDataByApi(type, category, action, label) {
const url = 'https://www.google-analytics.com/collect';
const gaParams = new URLSearchParams();
gaParams.append('v', 1);
gaParams.append('tid', 'UA-1211588-20');
gaParams.append('cid', getCustomerId());
gaParams.append('dl', window.location.host);
gaParams.append('ul', navigator.language);
gaParams.append('de', document.characterSet);
gaParams.append('dt', document.title);
gaParams.append('el', label ? label : null);
gaParams.append('t', type);
gaParams.append('ec', category);
gaParams.append('ea', action);
await fetch(url, {
method: 'POST',
mode: 'no-cors',
cache: 'no-cache',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
redirect: 'follow',
referrerPolicy: 'no-referrer',
body: gaParams,
});
}
export function trackPageView(pageName = null) {
if (window.DEBUG) {
log('trackPageView', pageName);
return;
}
sendTrackMessage('pageview', pageName);
}
export function trackGaSetField(fieldName, fieldValue) {
if (window.DEBUG) {
log('trackGaSetField', fieldName, fieldValue);
return;
}
if (window.ga) {
ga('set', fieldName, fieldValue);
} else {
setTimeout(() => trackGaSetField(fieldName, fieldValue), 100);
}
}
// if online, load after sometime
if (
navigator.onLine &&
!window.DEBUG &&
location.href.indexOf('chrome-extension://') === -1
) {
/* eslint-disable */
// prettier-ignore
setTimeout(function () {
(function (i, s, o, g, r, a, m) {
i['GoogleAnalyticsObject'] = r;
i[r] = i[r] || function () {
(i[r].q = i[r].q || []).push(arguments);
}, i[r].l = 1 * new Date();
a = s.createElement(o),
m = s.getElementsByTagName(o)[0];
a.async = 1;
a.src = g;
m.parentNode.insertBefore(a, m);
})(window, document, 'script', 'https://www.google-analytics.com/analytics.js', 'ga');
if (location.href.indexOf('chrome-extension://') === -1) {
ga('create', 'UA-1211588-20');
} else {
ga('create', 'UA-1211588-20', {
'cookieDomain': 'none'
});
// required for chrome extension protocol
ga('set', 'checkProtocolTask', function () { /* nothing */
});
}
}, 100);
/* eslint-enable */
}