From 2aa2de93b17980e1ef34f99a7b8dc34dc54db550 Mon Sep 17 00:00:00 2001 From: Val Neekman Date: Fri, 5 Sep 2025 19:04:38 -0400 Subject: [PATCH] feat: make Google Analytics configurable via environment variables MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Google Analytics only loads if NEXT_PUBLIC_GA_MEASUREMENT_ID is set - Added GoogleAnalytics component using Next.js Script component - Moved analytics to _app.tsx for proper client-side environment variable access - Added .env.example with optional GA configuration - Keeps tracking ID private and out of open source code 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- .env.example | 4 ++-- src/components/GoogleAnalytics.tsx | 28 ++++++++++++++++++++++++++++ src/pages/_app.tsx | 2 ++ 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 src/components/GoogleAnalytics.tsx diff --git a/.env.example b/.env.example index c539b6a..dded42e 100644 --- a/.env.example +++ b/.env.example @@ -8,5 +8,5 @@ NEXT_PUBLIC_APP_URL=https://btc.eh-aye.net # API endpoints (if needed) # NEXT_PUBLIC_API_URL=https://api.eh-aye.net -# Analytics (optional) -# NEXT_PUBLIC_GA_ID=G-XXXXXXXXXX \ No newline at end of file +# Google Analytics (optional) +# NEXT_PUBLIC_GA_MEASUREMENT_ID=G-XXXXXXXXXX \ No newline at end of file diff --git a/src/components/GoogleAnalytics.tsx b/src/components/GoogleAnalytics.tsx new file mode 100644 index 0000000..ed22c78 --- /dev/null +++ b/src/components/GoogleAnalytics.tsx @@ -0,0 +1,28 @@ +import Script from "next/script"; + +export function GoogleAnalytics() { + const gaMeasurementId = process.env.NEXT_PUBLIC_GA_MEASUREMENT_ID; + + if (!gaMeasurementId) { + return null; + } + + return ( + <> + {/* Google tag (gtag.js) */} + + + ); +} diff --git a/src/pages/_app.tsx b/src/pages/_app.tsx index d04ba06..c552952 100644 --- a/src/pages/_app.tsx +++ b/src/pages/_app.tsx @@ -3,6 +3,7 @@ import type { AppProps } from "next/app"; import "../styles/globals.css"; import { Toaster } from "@/components/ui/toaster"; import { ThemeProvider } from "@/providers/theme-provider"; +import { GoogleAnalytics } from "@/components/GoogleAnalytics"; export default function App({ Component, pageProps }: AppProps) { useEffect(() => { @@ -35,6 +36,7 @@ export default function App({ Component, pageProps }: AppProps) { enableSystem disableTransitionOnChange > +