From a6c66568c8b59563b64f3ad3d2d4f4a36ec53004 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Mon, 19 Aug 2019 13:48:21 -0400 Subject: Pageview times experiment (#69) * experiment: track pageview times * strictly respect do not track * oops * asdfasdfasdf * add blogpost * fix typos oops --- static/img/pageview_flowchart.png | Bin 0 -> 39421 bytes static/js/pageview_timer.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 static/img/pageview_flowchart.png create mode 100644 static/js/pageview_timer.js (limited to 'static') diff --git a/static/img/pageview_flowchart.png b/static/img/pageview_flowchart.png new file mode 100644 index 0000000..99b1ee0 Binary files /dev/null and b/static/img/pageview_flowchart.png differ diff --git a/static/js/pageview_timer.js b/static/js/pageview_timer.js new file mode 100644 index 0000000..2761396 --- /dev/null +++ b/static/js/pageview_timer.js @@ -0,0 +1,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); +})(); -- cgit v1.2.3