File tree Expand file tree Collapse file tree 3 files changed +37
-5
lines changed
Expand file tree Collapse file tree 3 files changed +37
-5
lines changed Original file line number Diff line number Diff line change 11import React from 'react' ;
22import type { Metadata } from 'next' ;
33import { Inter } from 'next/font/google' ;
4+ import Script from 'next/script' ;
45import './globals.css' ;
56import { config } from '@/lib/config' ;
67import { suppressHydrationWarning } from '@/lib/utils/suppress-hydration' ;
@@ -91,6 +92,23 @@ export default function RootLayout({ children }: { children: React.ReactNode })
9192 return (
9293 < html lang = 'en' className = 'scroll-smooth' >
9394 < body className = { inter . className } suppressHydrationWarning = { true } >
95+ { /* Google Analytics */ }
96+ { config . analytics . enabled && config . analytics . id && (
97+ < >
98+ < Script
99+ src = { `https://www.googletagmanager.com/gtag/js?id=${ config . analytics . id } ` }
100+ strategy = 'afterInteractive'
101+ />
102+ < Script id = 'google-analytics' strategy = 'afterInteractive' >
103+ { `
104+ window.dataLayer = window.dataLayer || [];
105+ function gtag(){dataLayer.push(arguments);}
106+ gtag('js', new Date());
107+ gtag('config', '${ config . analytics . id } ');
108+ ` }
109+ </ Script >
110+ </ >
111+ ) }
94112 < RootProviders > { children } </ RootProviders >
95113 </ body >
96114 </ html >
Original file line number Diff line number Diff line change @@ -71,13 +71,13 @@ export const config = {
7171 sessionSecret : process . env . SESSION_SECRET ,
7272 } ,
7373
74- // Analytics & Monitoring (using defaults since not in client-env)
74+ // Analytics & Monitoring
7575 analytics : {
76- enabled : false ,
77- id : '' ,
76+ enabled : clientEnv . NEXT_PUBLIC_ANALYTICS_ENABLED ,
77+ id : clientEnv . NEXT_PUBLIC_ANALYTICS_ID ,
7878 errorReporting : {
79- enabled : false ,
80- endpoint : '' ,
79+ enabled : clientEnv . NEXT_PUBLIC_ERROR_REPORTING_ENABLED ,
80+ endpoint : clientEnv . NEXT_PUBLIC_ERROR_REPORTING_API_KEY ,
8181 } ,
8282 } ,
8383
Original file line number Diff line number Diff line change @@ -42,6 +42,20 @@ const clientEnvSchema = z.object({
4242 NEXT_PUBLIC_EMAILJS_SERVICE_ID : z . string ( ) . optional ( ) ,
4343 NEXT_PUBLIC_EMAILJS_TEMPLATE_ID : z . string ( ) . optional ( ) ,
4444 NEXT_PUBLIC_EMAILJS_PUBLIC_KEY : z . string ( ) . optional ( ) ,
45+
46+ // Analytics Configuration
47+ NEXT_PUBLIC_ANALYTICS_ENABLED : z
48+ . string ( )
49+ . optional ( )
50+ . transform ( val => val === 'true' )
51+ . default ( ( ) => false ) ,
52+ NEXT_PUBLIC_ANALYTICS_ID : z . string ( ) . optional ( ) . default ( '' ) ,
53+ NEXT_PUBLIC_ERROR_REPORTING_ENABLED : z
54+ . string ( )
55+ . optional ( )
56+ . transform ( val => val === 'true' )
57+ . default ( ( ) => false ) ,
58+ NEXT_PUBLIC_ERROR_REPORTING_API_KEY : z . string ( ) . optional ( ) . default ( '' ) ,
4559} ) ;
4660
4761// Parse and validate client-side environment variables
You can’t perform that action at this time.
0 commit comments