/* Recording which section a call to action was pressed from. * * One listener on the document rather than one per button, so a CTA added in * Backstage tomorrow is counted without anybody remembering to wire it up. */ (function () { 'use strict'; /* A label for THIS browser, for THIS session, and nothing more. It exists only so one person pressing a button nine times does not read as nine people. sessionStorage means it dies with the tab — there is deliberately no way to recognise the same visitor tomorrow, and no cookie to consent to. */ var visit = ''; try { visit = sessionStorage.getItem('neu-visit') || ''; if (!visit) { visit = Math.random().toString(36).slice(2, 10); sessionStorage.setItem('neu-visit', visit); } } catch (e) { /* private browsing: the count is still right, just less tidy */ } document.addEventListener('click', function (e) { var a = e.target.closest && e.target.closest('[data-cta]'); if (!a) return; var body = JSON.stringify({ section: a.getAttribute('data-cta-from') || 'unknown', label: a.getAttribute('data-cta-label') || (a.textContent || '').trim().slice(0, 80), href: a.getAttribute('href') || '', page: location.pathname, visit: visit, }); /* sendBeacon, because a CTA is usually a link and the page is about to be replaced. An ordinary fetch is cancelled when that happens, which would lose exactly the presses that mattered most — the ones people acted on. The beacon is handed to the browser and survives the navigation. */ var sent = false; try { if (navigator.sendBeacon) { sent = navigator.sendBeacon('/api/click', new Blob([body], { type: 'application/json' })); } } catch (err) { sent = false; } if (!sent) { try { fetch('/api/click', { method: 'POST', headers: { 'content-type': 'application/json' }, body: body, keepalive: true }); } catch (err2) { /* counting must never break the link itself */ } } }, true); })();