diff options
-rw-r--r-- | track.js | 26 |
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 + })) + }) +})() |