All files / src analytics.js

98.14% Statements 53/54
91.42% Branches 32/35
100% Functions 7/7
100% Lines 47/47

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99  2x     14x 13x 9x       14x       14x 14x 7x       14x 10x 10x 10x   1x         9x 9x 9x 9x       14x 14x 14x 1x 1x       1x   13x 4x 4x       4x   9x 9x   2x         15x 15x   9x 9x 9x   2x   9x   9x 3x     6x 7x 7x       7x           6x 6x 6x       6x    
// Vendored analytics beacon. Do not edit here; copy the upstream file again to update.
export const OPT_OUT_KEY = 'trazire-analytics-opt-out';
 
export function shouldTrack({ hostname, optedOut } = {}) {
  if (!hostname) return false;
  if (optedOut) return false;
  return true;
}
 
export function routeFromHash(hash) {
  let path = String(hash ?? '')
    .replace(/^#/, '')
    .split('?')[0]
    .split('#')[0];
  path = path.replace(/\/+$/, '');
  if (!path) return '/';
  return path.startsWith('/') ? path : `/${path}`;
}
 
export function referrerHost(referrer) {
  if (typeof referrer !== 'string' || !referrer) return null;
  try {
    const { hostname } = new URL(referrer);
    return hostname || null;
  } catch {
    return null;
  }
}
 
export function buildPayload(site, route, { referrer, automated } = {}) {
  const payload = { site, route, automated: automated === true };
  const ref = referrerHost(referrer);
  if (ref) payload.ref = ref;
  return payload;
}
 
export function readOptOut(search, storage) {
  const params = new URLSearchParams(search || '');
  const param = params.get('no-track');
  if (param === '0') {
    try {
      storage.removeItem(OPT_OUT_KEY);
    } catch {
      // Storage may be blocked; opting back in still applies for this pageview.
    }
    return false;
  }
  if (param === '1') {
    try {
      storage.setItem(OPT_OUT_KEY, '1');
    } catch {
      // Storage may be blocked; the opt-out still applies for this pageview.
    }
    return true;
  }
  try {
    return storage.getItem(OPT_OUT_KEY) === '1';
  } catch {
    return false;
  }
}
 
export function initAnalytics({ site, endpoint, allowedHost } = {}) {
  Iif (typeof window === 'undefined' || typeof navigator === 'undefined') return false;
  if (window.location.hostname !== allowedHost) return false;
 
  let storage = null;
  try {
    storage = window.localStorage;
  } catch {
    storage = null;
  }
  const optedOut = readOptOut(window.location.search, storage);
 
  if (!shouldTrack({ hostname: window.location.hostname, optedOut })) {
    return false;
  }
 
  const send = () => {
    try {
      const payload = buildPayload(site, routeFromHash(window.location.hash), {
        referrer: document.referrer,
        automated: navigator.webdriver === true,
      });
      navigator.sendBeacon(endpoint, JSON.stringify(payload));
    } catch {
      // Analytics must never affect the site.
    }
  };
 
  send();
  try {
    window.addEventListener('hashchange', send);
  } catch {
    // Analytics must never affect the site.
  }
  return true;
}