Skip to content

Commit 36e00b5

Browse files
committed
feat(web): add Google Analytics via @next/third-parties
Root layout conditionally renders <GoogleAnalytics gaId> when NEXT_PUBLIC_GA_ID is non-empty — when unset (e.g. local dev, testnet) no script tag is emitted. Layout calls headers() to opt out of static prerender so the env read happens per-request, which lets the value flow through ECS task env (driven by terraform web_ga_id) without rebuilding the bundle for each environment.
1 parent 43b18d3 commit 36e00b5

4 files changed

Lines changed: 32 additions & 1 deletion

File tree

‎CLAUDE.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ The provider detail page (`/providers/:addr`) is expensive — it queries **ever
300300
| `COINGECKO_API_URL` | `https://api.coingecko.com/api/v3` | API |
301301
| `KEYBASE_API_URL` | `https://keybase.io/_/api/1.0` | API |
302302
| `NEXT_PUBLIC_API_URL` | `http://localhost:8080` | Web |
303+
| `NEXT_PUBLIC_GA_ID` | (unset, GA disabled) | Web — Google Analytics 4 measurement ID. When set, the root layout renders `<GoogleAnalytics gaId>` from `@next/third-parties/google`. Layout calls `headers()` so the read happens per-request — wired via ECS task env from terraform `web_ga_id`, not baked into the bundle |
303304
| `NODE_ENV` | `production` | Web |
304305

305306
## Not Covered (external data sources needed)

‎apps/web/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
},
1414
"dependencies": {
1515
"@info/shared": "workspace:*",
16+
"@next/third-parties": "^16.2.4",
1617
"@radix-ui/react-slot": "^1.2.4",
1718
"@radix-ui/react-tabs": "^1.1.13",
1819
"@tailwindcss/postcss": "^4.0.0",

‎apps/web/src/app/layout.tsx‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import type { Metadata, Viewport } from "next";
22
import { Inter } from "next/font/google";
3+
import { headers } from "next/headers";
4+
import { GoogleAnalytics } from "@next/third-parties/google";
35
import { Header } from "@/components/layout/Header";
46
import { Footer } from "@/components/layout/Footer";
57
import { ErrorBoundary } from "@/components/data/ErrorBoundary";
@@ -53,7 +55,13 @@ export const viewport: Viewport = {
5355
userScalable: true,
5456
};
5557

56-
export default function RootLayout({ children }: { children: React.ReactNode }) {
58+
export default async function RootLayout({ children }: { children: React.ReactNode }) {
59+
// headers() opts the layout out of static prerender so process.env is read
60+
// per-request — needed because NEXT_PUBLIC_GA_ID is wired through ECS task
61+
// env (terraform), not baked into the bundle at build time.
62+
await headers();
63+
const gaId = process.env.NEXT_PUBLIC_GA_ID;
64+
5765
return (
5866
<html lang="en" className="dark" suppressHydrationWarning>
5967
<body className={`min-h-screen bg-background antialiased ${inter.variable} ${inter.className}`}>
@@ -72,6 +80,7 @@ export default function RootLayout({ children }: { children: React.ReactNode })
7280
<Footer />
7381
</div>
7482
</SwrProvider>
83+
{gaId && <GoogleAnalytics gaId={gaId} />}
7584
</body>
7685
</html>
7786
);

‎pnpm-lock.yaml‎

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)