-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
72 lines (61 loc) · 1.7 KB
/
index.mjs
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
let _accountId;
let _anonymize;
let _userId;
function id() {
/* Something like 1234.1234 */
return Math.random() * 10 ** 8;
}
export function setup(accountId, anonymize) {
_accountId = accountId;
if (anonymize) {
_anonymize = 1;
} else {
_anonymize = 0;
}
try {
if (_anonymize) throw _anonymize;
_userId = localStorage.getItem("#ak") || id();
localStorage.setItem("#ak", _userId);
} catch (localStorageBlocked) {
_userId = _userId || id();
}
}
export function trackPageView(page = location.pathname) {
send("pageview", serialize("dp", page));
}
export function trackEvent(eventCategory, eventAction, eventLabel, eventValue) {
send("event", [
serialize("ec", eventCategory),
serialize("ea", eventAction),
serialize("el", eventLabel),
serialize("ev", eventValue),
]);
}
function send(hitType, params) {
if (!_accountId) return;
const url = `https://www.google-analytics.com/collect?${[
`v=1`,
`de=UTF-8`,
serialize("aip", _anonymize),
serialize("tid", _accountId),
serialize("cid", _userId),
serialize("dl", document.location.href),
serialize("ul", navigator.language.toLowerCase()),
serialize("dt", document.title),
serialize("dr", document.referrer),
serialize("sd", screen.colorDepth + "-bit"),
serialize("sr", `${screen.availWidth}-x-${screen.availHeight}`),
serialize("vp", `${innerWidth}-x-${innerHeight}`),
serialize("z", Date.now()),
serialize("t", hitType),
]
.concat(params)
.filter(Boolean)
.join("&")}`;
fetch(url, {
credentials: !_anonymize && "include",
}).catch(() => {});
}
function serialize(key, value) {
return value && `${key}=${encodeURIComponent(value)}`;
}