/** Fire-and-forget event/pageview beacon for self-hosted analytics; must
 *  never affect the app (network errors are swallowed). player_id is only
 *  ever attached server-side to the "login" event -- ordinary beacons don't
 *  carry it. */
export function trackEvent(path: string, referrer = '') {
  fetch('/api/track', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ path, referrer }),
    keepalive: true,
  }).catch(() => {});
}

/** One "landing" event per full page load — the visitor's first touch,
 *  carrying document.referrer (e.g. facebook.com). Without it a visit that
 *  starts on "/" leaves no trace at all: "/" is in the beacon's skip list and
 *  the auth-gate redirect to /auth is a REPLACE navigation (see App.tsx), so
 *  both hops are skipped. Not sent on /admin — the dashboard doesn't track
 *  itself. */
export function trackLanding() {
  if (window.location.pathname.startsWith('/admin')) return;
  trackEvent('landing', document.referrer);
}
