import ReactGA from "react-ga";
      
      // This should go inside the App component on _app.tsx for Next projects
      const router = useRouter()
      useEffect(() => {
        const handleRouteChange = (url: string, _: any) => {
          ReactGA.set({ page: url })
          ReactGA.pageview(url)
        }
        ReactGA.initialize('CODE', { debug: false })
        ReactGA.set({ page: router.pathname })
        ReactGA.pageview(router.pathname)
        router.events.on('routeChangeComplete', handleRouteChange)
        return () => {
          router.events.off('routeChangeComplete', handleRouteChange)
        }
      }, []);
      

      Typescript language logo
      Add Google Analytics Next project with React-GA

      next
      typescript
      reactga

      This snippet uses ReactGA to create ReactGA in a next project

      0 Comments

        Add Comment

        Log in to add a comment

        Codiga - All rights reserved 2022.