-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.js
32 lines (25 loc) · 920 Bytes
/
stats.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
;(() => {
const events = ['click', 'scroll', 'keydown', 'mousemove', 'touchstart']
const eventOptions = { once: true, passive: true }
let statsAreSend = false
const addEventListeners = () =>
events.forEach((e) => document.addEventListener(e, sendStats, eventOptions))
const removeEventListeners = () =>
events.forEach((e) =>
document.removeEventListener(e, sendStats, eventOptions),
)
const sendStats = () => {
if (statsAreSend) return
const data = new FormData()
data.append('path', location.pathname)
const { referrer } = document
if (referrer) data.append('referrer', new URL(referrer).host)
navigator.sendBeacon('/kirby-stats/hit', data)
removeEventListeners()
statsAreSend = true
}
const isReload = window.performance
.getEntriesByType('navigation')
.some((entry) => entry.type === 'reload')
if (!isReload) addEventListeners()
})()