summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2020-07-08 22:25:16 +0200
committerAki <please@ignore.pl>2020-07-08 22:25:16 +0200
commit74a06dab4d6a4a05d7a4c0c384a826c0d2b1df92 (patch)
tree5cf5149248e7197ffa42ef06e0a42a3c51dc479f
parent61fa862c8e7054f13373317fc9a6a78cbb37e313 (diff)
downloadstats-74a06dab4d6a4a05d7a4c0c384a826c0d2b1df92.zip
stats-74a06dab4d6a4a05d7a4c0c384a826c0d2b1df92.tar.gz
stats-74a06dab4d6a4a05d7a4c0c384a826c0d2b1df92.tar.bz2
Added client side javascript for tracking
-rw-r--r--track.js26
1 files changed, 26 insertions, 0 deletions
diff --git a/track.js b/track.js
new file mode 100644
index 0000000..cb8ab72
--- /dev/null
+++ b/track.js
@@ -0,0 +1,26 @@
+(()=>{
+ 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", (event)=>{
+ scrolledTo = Math.max(scrolledTo, window.scrollY + window.innerHeight)
+ }, false)
+ window.addEventListener("beforeunload", ()=>{
+ let xhr = new XMLHttpRequest()
+ xhr.open("POST", "https://stats.ignore.pl/entry")
+ xhr.setRequestHeader("Content-Type", "application/json")
+ xhr.send(JSON.stringify({
+ "location": document.location.href,
+ "referrer": document.referrer,
+ "startedAt": startedAt,
+ "timeSpent": Math.floor((new Date() - startedAt) / 1000),
+ "scrolledTo": scrolledTo / document.scrollingElement.scrollHeight
+ }))
+ })
+})()