repository

BlowfishCMS

A small content management system for OpenBSD

version 0.45.6 · ISC · 2026-12-03 · releases

25 lines · 1.7 KB · download this file

// BlowfishCMS Analytics: counts this page view. Sends the page's address,
// where the visitor came from (the referrer, and the tag of a tagged link:
// ?ref=, ?utm_source= or ?from=) and the window's width; no cookies, no
// identifiers. A browser used for the site's admin isn't counted.
(() => {
  const query = new URLSearchParams(location.search);
  const tag = (query.get('ref') || query.get('utm_source') || query.get('from') || '').slice(0, 40);
  // The tag has done its job once it's read: it comes out of the address, so
  // someone who copies the address and passes it on isn't counted as that link.
  if (tag) {
    for (const k of [...query.keys()]) if (k === 'ref' || k === 'from' || k.startsWith('utm_')) query.delete(k);
    const rest = query.toString();
    try { history.replaceState(history.state, '', location.pathname + (rest ? '?' + rest : '') + location.hash); } catch (e) {}
  }
  try { if (localStorage.getItem('bf-analytics-ignore') === '1') return; } catch (e) {}
  // Automated browsers (navigator.webdriver) aren't counted.
  if (navigator.webdriver) return;
  const post = query.get('post');
  const data = new URLSearchParams({p: location.pathname + (post ? '?post=' + post : ''), r: document.referrer || '', t: tag, w: String(window.innerWidth || 0)});
  const body = new Blob([data.toString()], {type: 'application/x-www-form-urlencoded'});
  // sendBeacon sends the view even if the visitor is already leaving; fetch is
  // the fallback. credentials: 'omit' sends no cookies.
  try { if (navigator.sendBeacon && navigator.sendBeacon('/stats/hit', body)) return; } catch (e) {}
  fetch('/stats/hit', {method: 'POST', body: data, keepalive: true, credentials: 'omit'}).catch(() => {});
})();