summaryrefslogtreecommitdiff
path: root/track.js
blob: f0dd16709f21c3d5c8da57aca75996a5d6ab6c71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
(()=>{
	let startedAt = null
	let scrolledTo = null
	window.addEventListener("load", ()=>{
		startedAt = new Date()
		scrolledTo = window.scrollY
		if (window.innerHeight >= document.scrollingElement.scrollHeight) {
			scrolledTo = document.scrollingElement.scrollHeight
		}
	})
	window.addEventListener("scroll", ()=>{
		scrolledTo = Math.max(scrolledTo, window.scrollY + window.innerHeight)
	}, false)
	window.addEventListener("beforeunload", ()=>{
		navigator.sendBeacon("https://stats.ignore.pl/entries", JSON.stringify({
			"location": document.location.href,
			"referrer": document.referrer,
			"startedAt": startedAt,
			"timeSpent": Math.floor((new Date() - startedAt) / 1000),
			"scrolledTo": (scrolledTo || 0) / document.scrollingElement.scrollHeight
		}))
	})
})()