declare global {
  interface Window {
    gtag?: (...args: unknown[]) => void
  }
}

export const GA_MEASUREMENT_ID = "G-JQ2YB0VK4L"

export const isAnalyticsEnabled = Boolean(GA_MEASUREMENT_ID)

export const trackPageView = (url: string) => {
  if (!isAnalyticsEnabled || typeof window === "undefined" || typeof window.gtag !== "function") return

  window.gtag("event", "page_view", {
    page_location: window.location.href,
    page_path: url,
    page_title: document.title,
  })
}

export const trackEvent = (eventName: string, params?: Record<string, string | number | boolean | undefined>) => {
  if (!isAnalyticsEnabled || typeof window === "undefined" || typeof window.gtag !== "function") return
  window.gtag("event", eventName, params || {})
}
