forked from umami-software/umami
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
184 lines (149 loc) · 4.4 KB
/
index.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
import { doNotTrack, hook } from '../lib/web';
import { removeTrailingSlash } from '../lib/url';
(window => {
const {
screen: { width, height },
navigator: { language },
location: { hostname, pathname, search },
localStorage,
sessionStorage,
document,
history,
} = window;
const script = document.querySelector('script[data-website-id]');
if (!script) return;
const attr = script.getAttribute.bind(script);
const website = attr('data-website-id');
const hostUrl = attr('data-host-url');
const autoTrack = attr('data-auto-track') !== 'false';
const dnt = attr('data-do-not-track');
const useCache = attr('data-cache');
const domain = attr('data-domains') || '';
const domains = domain.split(',').map(n => n.trim());
const eventClass = /^umami--([a-z]+)--([\w]+[\w-]*)$/;
const eventSelect = "[class*='umami--']";
const cacheKey = 'umami.cache';
const disableTracking = () =>
(localStorage && localStorage.getItem('umami.disabled')) ||
(dnt && doNotTrack()) ||
(domain && !domains.includes(hostname));
const root = hostUrl
? removeTrailingSlash(hostUrl)
: script.src.split('/').slice(0, -1).join('/');
const screen = `${width}x${height}`;
const listeners = {};
let currentUrl = `${pathname}${search}`;
let currentRef = document.referrer;
/* Collect metrics */
const post = (url, data, callback) => {
const req = new XMLHttpRequest();
req.open('POST', url, true);
req.setRequestHeader('Content-Type', 'application/json');
req.onreadystatechange = () => {
if (req.readyState === 4) {
callback(req.response);
}
};
req.send(JSON.stringify(data));
};
const collect = (type, params, uuid) => {
if (disableTracking()) return;
const payload = {
website: uuid,
hostname,
screen,
language,
cache: useCache && sessionStorage.getItem(cacheKey),
};
Object.keys(params).forEach(key => {
payload[key] = params[key];
});
post(
`${root}/api/collect`,
{
type,
payload,
},
res => useCache && sessionStorage.setItem(cacheKey, res),
);
};
const trackView = (url = currentUrl, referrer = currentRef, uuid = website) => {
collect(
'pageview',
{
url,
referrer,
},
uuid,
);
};
const trackEvent = (event_value, event_type = 'custom', url = currentUrl, uuid = website) => {
collect(
'event',
{
event_type,
event_value,
url,
},
uuid,
);
};
/* Handle events */
const addEvents = node => {
const elements = node.querySelectorAll(eventSelect);
Array.prototype.forEach.call(elements, addEvent);
};
const addEvent = element => {
(element.getAttribute('class') || '').split(' ').forEach(className => {
if (!eventClass.test(className)) return;
const [, type, value] = className.split('--');
const listener = listeners[className]
? listeners[className]
: (listeners[className] = () => trackEvent(value, type));
element.addEventListener(type, listener, true);
});
};
const monitorMutate = mutations => {
mutations.forEach(mutation => {
const element = mutation.target;
addEvent(element);
addEvents(element);
});
};
/* Handle history changes */
const handlePush = (state, title, url) => {
if (!url) return;
currentRef = currentUrl;
const newUrl = url.toString();
if (newUrl.substring(0, 4) === 'http') {
currentUrl = '/' + newUrl.split('/').splice(3).join('/');
} else {
currentUrl = newUrl;
}
if (currentUrl !== currentRef) {
trackView();
}
};
/* Global */
if (!window.umami) {
const umami = eventValue => trackEvent(eventValue);
umami.trackView = trackView;
umami.trackEvent = trackEvent;
window.umami = umami;
}
/* Start */
if (autoTrack && !disableTracking()) {
history.pushState = hook(history, 'pushState', handlePush);
history.replaceState = hook(history, 'replaceState', handlePush);
const update = () => {
if (document.readyState === 'complete') {
addEvents(document);
trackView();
const observer = new MutationObserver(monitorMutate);
observer.observe(document, { childList: true, subtree: true });
}
};
document.addEventListener('readystatechange', update, true);
update();
}
})(window);