// assets/analytics.js
// Lightweight, privacy-friendly pageview beacon.
// Posts to /api/analytics/pageview (see server/src/routes/analytics.js).
// Never throws and never blocks the page if the request fails.
(function () {
  try {
    var payload = JSON.stringify({
      path: window.location.pathname,
      referrer: document.referrer || null
    });

    if (navigator.sendBeacon) {
      var blob = new Blob([payload], { type: 'application/json' });
      navigator.sendBeacon('/api/analytics/pageview', blob);
    } else {
      fetch('/api/analytics/pageview', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: payload,
        keepalive: true
      }).catch(function () {
        // silently ignore — analytics must never surface an error to visitors
      });
    }
  } catch (e) {
    // silently ignore
  }
})();
