aboutsummaryrefslogtreecommitdiff
path: root/static/js/pageview_timer.js
blob: 276139645f5563d05f31dbd0e0d88d7a0f822fe2 (plain)
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
/*
  Hi,

  If you are reading this, you have found this script in the referenced scripts
  for pages on this site. I know you're gonna have to take me at my word on this,
  but I'm literally using this to collect how much time people spend reading my
  webpages. See metrics here: https://christine.website/metrics

  If you have the "do not track" setting enabled in your browser, this code will
  be ineffectual.
*/

(function() {
    let dnt = navigator.doNotTrack;
    if (dnt === "1") {
        return;
    }

    let startTime = new Date();

    function logTime() {
        let stopTime = new Date();
        window.navigator.sendBeacon("/api/pageview-timer", JSON.stringify({
            "path": window.location.pathname,
            "start_time": startTime.toISOString(),
            "end_time": stopTime.toISOString()
        }));
    }

    window.addEventListener("pagehide", logTime, false);
})();