From 661cfcfdb2745330b5f47a2c7b82ad7d206f1bae Mon Sep 17 00:00:00 2001 From: Guilherme Biff Zarelli Date: Sun, 25 Oct 2026 14:47:59 -0300 Subject: [PATCH] fix(analytics): use automatic pageview detection --- src/app/components/GoogleAnalytics.tsx | 44 -------------------------- 1 file changed, 44 deletions(-) diff --git a/src/app/components/GoogleAnalytics.tsx b/src/app/components/GoogleAnalytics.tsx index ca36b5f..15c4c84 100644 --- a/src/app/components/GoogleAnalytics.tsx +++ b/src/app/components/GoogleAnalytics.tsx @@ -1,57 +1,13 @@ -"use client"; - -import { useEffect, useRef, useState } from "react"; -import { usePathname } from "next/navigation"; import Script from "next/script"; const measurementId = process.env.NEXT_PUBLIC_GA_ID || "G-VWDH18RX25"; -declare global { - interface Window { - dataLayer: unknown[][]; - gtag: (...args: unknown[]) => void; - } -} - export default function GoogleAnalytics() { - const pathname = usePathname(); - const lastTrackedPath = useRef(null); - const hasTrackedInitialPage = useRef(false); - const pendingPageViews = useRef>([]); - const [gtagReady, setGtagReady] = useState(false); - - useEffect(() => { - if (!pathname) return; - - if (!hasTrackedInitialPage.current) { - hasTrackedInitialPage.current = true; - lastTrackedPath.current = pathname; - return; - } - - if (lastTrackedPath.current !== pathname) { - lastTrackedPath.current = pathname; - pendingPageViews.current.push({ pathname, pageTitle: document.title }); - } - - if (!gtagReady || typeof window.gtag !== "function") return; - - for (const pageView of pendingPageViews.current) { - window.gtag("event", "page_view", { - page_path: pageView.pathname, - page_location: `${window.location.origin}${pageView.pathname}`, - page_title: pageView.pageTitle, - }); - } - pendingPageViews.current = []; - }, [gtagReady, pathname]); - return ( <>