Skip to content

Commit 20b3a07

Browse files
committed
fix(analytics): configure Google Analytics from environment variables
- Add analytics environment variables to client-env schema - Update config.ts to read analytics settings from environment variables - Add Google Analytics script to layout when enabled - Fix analytics config to properly use NEXT_PUBLIC_ANALYTICS_ENABLED and NEXT_PUBLIC_ANALYTICS_ID
1 parent 37bc127 commit 20b3a07

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

app/layout.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22
import type { Metadata } from 'next';
33
import { Inter } from 'next/font/google';
4+
import Script from 'next/script';
45
import './globals.css';
56
import { config } from '@/lib/config';
67
import { 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>

lib/config.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff 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

lib/utils/client-env.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)