Analytics script cleanup (closes #54)

This commit is contained in:
R. Miles McCain 2020-06-28 03:31:10 +00:00
parent bb0dc2e90f
commit 88f25b6743
No known key found for this signature in database
GPG Key ID: F1053629E2905557

View File

@ -1,8 +1,14 @@
window.onload = function () { // This is a lightweight and privacy-friendly analytics script from Shynet, a self-hosted
var idempotency = // analytics tool. To give you full visibility into how your data is being monitored, this
Math.random().toString(36).substring(2, 15) + // file is intentionally not minified or obfuscated. To learn more about Shynet (and to view
Math.random().toString(36).substring(2, 15); // its source code), visit <https://github.com/milesmcc/shynet>.
function sendUpdate() { //
// This script only sends the current URL, the referrer URL, and the page load time. That's it!
var Shynet = {
idempotency: null,
heartbeatTaskId: null,
sendHeartbeat: function () {
try { try {
if (document.hidden) { if (document.hidden) {
return; return;
@ -16,7 +22,7 @@ window.onload = function () {
xhr.setRequestHeader("Content-Type", "application/json"); xhr.setRequestHeader("Content-Type", "application/json");
xhr.send( xhr.send(
JSON.stringify({ JSON.stringify({
idempotency: idempotency, idempotency: Shynet.idempotency,
referrer: document.referrer, referrer: document.referrer,
location: window.location.href, location: window.location.href,
loadTime: loadTime:
@ -25,7 +31,15 @@ window.onload = function () {
}) })
); );
} catch { } } catch { }
},
newPageLoad: function () {
if (Shynet.heartbeatTaskId != null) {
clearInterval(Shynet.heartbeatTaskId);
}
Shynet.idempotency = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
Shynet.heartbeatTaskId = setInterval(Shynet.sendHeartbeat, parseInt("{{heartbeat_frequency}}"));
Shynet.sendHeartbeat();
} }
setInterval(sendUpdate, parseInt("{{heartbeat_frequency}}"));
sendUpdate();
}; };
window.addEventListener("load", Shynet.newPageLoad);