Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions web/console/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ export const metadata: Metadata = {
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>
<head>
<GoogleAnalytics />
{children}
</body>
</head>
<body>{children}</body>
</html>
);
}
25 changes: 14 additions & 11 deletions web/console/components/GoogleAnalytics.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import Script from "next/script";

/**
* GA4 loader. Measurement ID is baked in at build time via
* NEXT_PUBLIC_GA_MEASUREMENT_ID (Next inlines public env on `next build`).
*
* Rendered as plain <script> tags in <head> so Google's tag detector (and
* other non-JS crawlers) see the snippet in the initial HTML. next/script
* afterInteractive only injects post-hydration, which fails GA "Retest".
*/
export function GoogleAnalytics() {
const measurementId = process.env.NEXT_PUBLIC_GA_MEASUREMENT_ID?.trim();
Expand All @@ -11,15 +13,16 @@ export function GoogleAnalytics() {
}
return (
<>
<Script src={`https://www.googletagmanager.com/gtag/js?id=${measurementId}`} strategy="afterInteractive" />
<Script id="google-analytics" strategy="afterInteractive">
{`
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${measurementId}');
`}
</Script>
<script async src={`https://www.googletagmanager.com/gtag/js?id=${measurementId}`} />
<script
id="google-analytics"
dangerouslySetInnerHTML={{
__html: `window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${measurementId}');`
}}
/>
</>
);
}