Skip to content
Merged
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
15 changes: 15 additions & 0 deletions frontend/src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import type { Metadata } from "next";
import { Geist, Geist_Mono, Playfair_Display, Cinzel } from "next/font/google";
import Script from "next/script";
import "./globals.css";
import { AuthProvider } from "@/contexts/AuthContext";
import { AuthWrapper } from "@/components/AuthWrapper";
import { SiteHeader } from "@/components/SiteHeader";

const GA_ID = "G-65HJEVF1M4";

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The Google Analytics ID is hardcoded. It's a best practice to store configuration values like this in environment variables. This allows for different configurations across environments (development, staging, production) without code changes. For Next.js, you should use a public environment variable by prefixing it with NEXT_PUBLIC_ and adding it to a .env.local file.

Suggested change
const GA_ID = "G-65HJEVF1M4";
const GA_ID = process.env.NEXT_PUBLIC_GA_ID;


const geistSans = Geist({
variable: "--font-geist-sans",
subsets: ["latin"],
Expand Down Expand Up @@ -47,6 +50,18 @@ export default function RootLayout({
}>) {
return (
<html lang="en">
<Script
src={`https://www.googletagmanager.com/gtag/js?id=${GA_ID}`}
strategy="afterInteractive"
/>
<Script id="google-analytics" strategy="afterInteractive">
{`
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${GA_ID}');
`}
</Script>
Comment on lines +53 to +64

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

These analytics scripts will run in all environments, including local development. This can pollute your analytics data with development activity. It's recommended to only enable analytics in the production environment and to ensure the tracking ID is configured before attempting to load the scripts.

      {process.env.NODE_ENV === "production" && GA_ID && (
        <>
          <Script
            src={`https://www.googletagmanager.com/gtag/js?id=${GA_ID}`}
            strategy="afterInteractive"
          />
          <Script id="google-analytics" strategy="afterInteractive">
            {`
              window.dataLayer = window.dataLayer || [];
              function gtag(){dataLayer.push(arguments);}
              gtag('js', new Date());
              gtag('config', '${GA_ID}');
            `}
          </Script>
        </>
      )}

<body
className={`${geistSans.variable} ${geistMono.variable} ${playfair.variable} ${cinzel.variable} antialiased`}
>
Expand Down