/* * Reports a page view of the public site to the statistics of the manager, and how long the page was in view. * No cookies, no storage, nothing about the visitor: the server decides what it keeps (see the README). A browser that * asks not to be followed (Do Not Track, Global Privacy Control) is not counted, here and on the server. * * * * `data-spa` on the script tag: also report the pages a client-side router moves to (history.pushState). */ (function () { 'use strict'; var script = document.currentScript; var base = script && script.src ? script.src.replace(/\/pulse\.js(\?.*)?$/, '') : '/pulse'; if (navigator.doNotTrack === '1' || navigator.globalPrivacyControl === true) { return; } var token = null; var visible = 0; var shownAt = document.visibilityState === 'visible' ? Date.now() : null; var sent = false; function params() { return new URLSearchParams(location.search); } function other(referrer) { try { return new URL(referrer).host !== location.host ? referrer : ''; } catch (e) { return ''; } } function report(path, referrer) { var query = params(); fetch(base + '/view', { method: 'POST', headers: { 'Content-Type': 'application/json', Accept: 'application/json' }, credentials: 'omit', keepalive: true, body: JSON.stringify({ path: path, referrer: referrer, via: query.get('via') || query.get('utm_source') || '', campaign: query.get('utm_campaign') || '', }), }) .then(function (response) { return response.ok ? response.json() : null; }) .then(function (data) { token = data && data.token ? data.token : null; }) .catch(function () {}); } function leave() { if (shownAt !== null) { visible += Date.now() - shownAt; shownAt = null; } if (token && !sent && visible > 0) { sent = true; var body = JSON.stringify({ token: token, seconds: Math.min(Math.round(visible / 1000), 7200) }); if (!(navigator.sendBeacon && navigator.sendBeacon(base + '/leave', new Blob([body], { type: 'application/json' })))) { fetch(base + '/leave', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: body, keepalive: true, credentials: 'omit' }).catch(function () {}); } } } function resume() { shownAt = shownAt === null && document.visibilityState === 'visible' ? Date.now() : shownAt; sent = false; } document.addEventListener('visibilitychange', function () { document.visibilityState === 'hidden' ? leave() : resume(); }); window.addEventListener('pagehide', leave); report(location.pathname, other(document.referrer)); if (script && script.hasAttribute('data-spa')) { var previous = location.pathname; var push = history.pushState; history.pushState = function () { var result = push.apply(this, arguments); if (location.pathname !== previous) { leave(); visible = 0; token = null; shownAt = Date.now(); sent = false; report(location.pathname, location.origin + previous); previous = location.pathname; } return result; }; } })();