'use client';
import Script from 'next/script';

declare global {
  interface Window {
    dataLayer: any[]
  }
}
const GA_MEASUREMENT_ID = 'G-0731LCNQ0Z'

const GoogleAnalytics = () => {
  return (
    <>
      {/* Load GA script */}
      <Script
        src={`https://www.googletagmanager.com/gtag/js?id=${GA_MEASUREMENT_ID}`}
        strategy="afterInteractive"
        onLoad={() => {
          // Initialize dataLayer safely
          window.dataLayer = window.dataLayer || []

          // Use rest parameters instead of 'arguments'
          function gtag(...args: any[]) {
            window.dataLayer.push(args)
          }

          gtag('js', new Date()) // new Date() is fine in TypeScript
          gtag('config', GA_MEASUREMENT_ID)
        }}
      />
    </>
  )
}

export default GoogleAnalytics
