-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.js
44 lines (41 loc) · 1.12 KB
/
client.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
;(function () {
if (/(localhost|127\.0\.0\.1|0\.0\.0\.0|github\.dev)/.test(window.location.host)) return
if (localStorage && localStorage.getItem && localStorage.getItem('analytics') === 'false') return
setTimeout(function () {
track({
r: document.referrer,
p: window.location.pathname,
w: window.innerWidth
})
}, 1000)
heartbeat()
function heartbeat (i = 0) {
if (i > 30) return
setTimeout(heartbeat, 10000, ++i)
if (document.hidden) { return }
track({
t: 'heartbeat',
r: document.referrer,
p: window.location.pathname,
w: window.innerWidth
})
}
function track (data) {
const baseUrl = '{{STATS_BASE_URL}}/p'
try {
window.fetch(baseUrl, {
body: JSON.stringify(data),
headers: {
'content-type': 'text/plain'
},
method: 'POST'
})
} catch (err) {
const req = new window.XMLHttpRequest()
req.open('POST', baseUrl, true)
req.setRequestHeader('Content-Type', 'text/plain')
req.send(JSON.stringify(data))
req.onreadystatechange = Function.prototype
}
}
})()