
export const initGA = (gaId: string) => {
  if (typeof window === 'undefined' || !gaId || (window as any)._ga_initialized) return;

  const script1 = document.createElement('script');
  script1.async = true;
  script1.src = `https://www.googletagmanager.com/gtag/js?id=${gaId}`;
  document.head.appendChild(script1);

  const script2 = document.createElement('script');
  script2.innerHTML = `
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date());
    gtag('config', '${gaId}', { 'send_page_view': true });
  `;
  document.head.appendChild(script2);
  (window as any)._ga_initialized = true;
  console.log(`[GA4] Initialized with ID: ${gaId}`);
};

export const trackGAEvent = (action: string, params?: any) => {
  if (typeof window !== 'undefined' && (window as any).gtag) {
    (window as any).gtag('event', action, params);
  }
};
