// GA4 event helper. Page-level privacy gating (the HIPAA allowlist) lives in
// ./marketing-pages and is enforced by components/cookies/AnalyticsGate, which
// only loads GA on allowed marketing pages with analytics consent.

export const GA_MEASUREMENT_ID = process.env.NEXT_PUBLIC_GA_MEASUREMENT_ID || '';

type GtagFn = (
  command: 'event',
  eventName: string,
  params?: Record<string, unknown>
) => void;

/**
 * Fire a GA4 event (e.g. on successful signup). No-op unless a measurement ID
 * is configured and GA has actually loaded (`window.gtag` present), which only
 * happens on allowed marketing pages with analytics consent — so this is
 * automatically consent- and privacy-safe, mirroring trackLinkedInConversion.
 */
export function trackGAEvent(
  eventName: string,
  params?: Record<string, unknown>
): void {
  if (typeof window === 'undefined' || !GA_MEASUREMENT_ID) return;
  const w = window as unknown as { gtag?: GtagFn };
  if (typeof w.gtag === 'function') {
    w.gtag('event', eventName, params);
  }
}
