From dff791d9ddda4fcaed95b578359ec5ede1148322 Mon Sep 17 00:00:00 2001 From: Shen Zhang Date: Sun, 2 Nov 2025 23:20:32 -0600 Subject: [PATCH 1/3] google analytics fix --- src/components/GoogleAnalytics.tsx | 43 +++++++++++++++++------------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/src/components/GoogleAnalytics.tsx b/src/components/GoogleAnalytics.tsx index 77ff3d4..700b965 100644 --- a/src/components/GoogleAnalytics.tsx +++ b/src/components/GoogleAnalytics.tsx @@ -6,30 +6,37 @@ const GA_MEASUREMENT_ID = 'G-CFZYZLHP5X'; const GoogleAnalytics: React.FC = () => { const location = useLocation(); + // Load GA script once useEffect(() => { - // Load Google Analytics script - const script1 = document.createElement('script'); - script1.async = true; - script1.src = `https://www.googletagmanager.com/gtag/js?id=${GA_MEASUREMENT_ID}`; - document.head.appendChild(script1); - - // Initialize dataLayer and gtag function - window.dataLayer = window.dataLayer || []; - function gtag(...args: any[]) { - window.dataLayer.push(args); + // Only inject if not already present + if (!document.querySelector(`script[src*="googletagmanager.com/gtag/js?id=${GA_MEASUREMENT_ID}"]`)) { + const script = document.createElement('script'); + script.async = true; + script.src = `https://www.googletagmanager.com/gtag/js?id=${GA_MEASUREMENT_ID}`; + script.onload = () => { + // Initialize GA after script loads + window.dataLayer = window.dataLayer || []; + function gtag(...args: any[]) { + window.dataLayer.push(args); + } + window.gtag = gtag; + + window.gtag('js', new Date()); + window.gtag('config', GA_MEASUREMENT_ID, { send_page_view: false }); + + // Fire initial page_view + window.gtag('event', 'page_view', { + page_path: window.location.pathname + window.location.search, + }); + }; + document.head.appendChild(script); } - window.gtag = gtag; - - gtag('js', new Date()); - gtag('config', GA_MEASUREMENT_ID, { - page_path: window.location.pathname, - }); }, []); + // Track route changes useEffect(() => { - // Track page view on route change if (window.gtag) { - window.gtag('config', GA_MEASUREMENT_ID, { + window.gtag('event', 'page_view', { page_path: location.pathname + location.search, }); } From 61706fac3b6d97f2f482f3183932f39eaf03d95b Mon Sep 17 00:00:00 2001 From: Shen Zhang Date: Sun, 2 Nov 2025 23:27:13 -0600 Subject: [PATCH 2/3] simplified google analytics --- public/index.html | 8 +++++ src/App.tsx | 2 -- src/components/GoogleAnalytics.tsx | 48 ------------------------------ 3 files changed, 8 insertions(+), 50 deletions(-) delete mode 100644 src/components/GoogleAnalytics.tsx diff --git a/public/index.html b/public/index.html index aa069f2..62c145d 100644 --- a/public/index.html +++ b/public/index.html @@ -26,6 +26,14 @@ --> React App + + +
diff --git a/src/App.tsx b/src/App.tsx index a924740..0ae2848 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -5,13 +5,11 @@ import Header from './components/Header'; import HomePage from './pages/HomePage'; import LeaderboardPage from './pages/LeaderboardPage'; import SubmitPromptPage from './pages/SubmitPromptPage'; -import GoogleAnalytics from './components/GoogleAnalytics'; const App: React.FC = () => { return (
-
diff --git a/src/components/GoogleAnalytics.tsx b/src/components/GoogleAnalytics.tsx deleted file mode 100644 index 700b965..0000000 --- a/src/components/GoogleAnalytics.tsx +++ /dev/null @@ -1,48 +0,0 @@ -import { useEffect } from 'react'; -import { useLocation } from 'react-router-dom'; - -const GA_MEASUREMENT_ID = 'G-CFZYZLHP5X'; - -const GoogleAnalytics: React.FC = () => { - const location = useLocation(); - - // Load GA script once - useEffect(() => { - // Only inject if not already present - if (!document.querySelector(`script[src*="googletagmanager.com/gtag/js?id=${GA_MEASUREMENT_ID}"]`)) { - const script = document.createElement('script'); - script.async = true; - script.src = `https://www.googletagmanager.com/gtag/js?id=${GA_MEASUREMENT_ID}`; - script.onload = () => { - // Initialize GA after script loads - window.dataLayer = window.dataLayer || []; - function gtag(...args: any[]) { - window.dataLayer.push(args); - } - window.gtag = gtag; - - window.gtag('js', new Date()); - window.gtag('config', GA_MEASUREMENT_ID, { send_page_view: false }); - - // Fire initial page_view - window.gtag('event', 'page_view', { - page_path: window.location.pathname + window.location.search, - }); - }; - document.head.appendChild(script); - } - }, []); - - // Track route changes - useEffect(() => { - if (window.gtag) { - window.gtag('event', 'page_view', { - page_path: location.pathname + location.search, - }); - } - }, [location]); - - return null; -}; - -export default GoogleAnalytics; From b590d9473d522c02df5ea39f969d5e58dbd035fe Mon Sep 17 00:00:00 2001 From: Shen Zhang Date: Sun, 2 Nov 2025 23:36:33 -0600 Subject: [PATCH 3/3] Changed title --- public/index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/index.html b/public/index.html index 62c145d..12c9ff8 100644 --- a/public/index.html +++ b/public/index.html @@ -24,7 +24,7 @@ work correctly both with client-side routing and a non-root public URL. Learn how to configure a non-root public URL by running `npm run build`. --> - React App + RouterArena