summaryrefslogtreecommitdiff
path: root/track.js
blob: cc2df6fa7b93d47f0cbf7c9ad64caaac24cd8f2f (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/entry", JSON.stringify({
			"location": document.location.href,
			"referrer": document.referrer,
			"startedAt": startedAt,
			"timeSpent": Math.floor((new Date() - startedAt) / 1000),
			"scrolledTo": (scrolledTo || 0) / document.scrollingElement.scrollHeight
		}))
	})
})()