Skip to content

Commit fbc0e14

Browse files
committed
fix(analytics): improve analytics configuration and environment variable parsing
- Make NEXT_PUBLIC_ANALYTICS_ENABLED parsing case-insensitive (accepts true/True/TRUE/1) - Improve Google Analytics component validation and error messages - Add debug logging to help troubleshoot analytics in production - Update env.example with clear documentation for GA4 Measurement ID format - Fix analytics boolean parsing to be more robust and user-friendly
1 parent 50b9ff6 commit fbc0e14

File tree

4 files changed

+30
-13
lines changed

4 files changed

+30
-13
lines changed

app/layout.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,16 @@ export default function RootLayout({ children }: { children: React.ReactNode })
8989
suppressHydrationWarning();
9090
}
9191

92+
// Debug analytics configuration
93+
if (config.development.isDevelopment) {
94+
console.log('[Analytics Config]', {
95+
enabled: config.analytics.enabled,
96+
id: config.analytics.id,
97+
hasId: !!config.analytics.id,
98+
willRender: config.analytics.enabled && !!config.analytics.id,
99+
});
100+
}
101+
92102
return (
93103
<html lang='en' className='scroll-smooth'>
94104
<body className={inter.className} suppressHydrationWarning={true}>

components/analytics/GoogleAnalytics.tsx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,25 @@ interface GoogleAnalyticsProps {
99

1010
export function GoogleAnalytics({ measurementId }: GoogleAnalyticsProps) {
1111
useEffect(() => {
12-
// Debug logging (only in development)
13-
if (process.env.NODE_ENV === 'development') {
14-
console.log('[Google Analytics] Initializing with ID:', measurementId);
15-
console.log('[Google Analytics] Script will load from:', `https://www.googletagmanager.com/gtag/js?id=${measurementId}`);
16-
}
12+
// Debug logging (always log for troubleshooting)
13+
console.log('[Google Analytics] Initializing with ID:', measurementId);
14+
console.log('[Google Analytics] Script will load from:', `https://www.googletagmanager.com/gtag/js?id=${measurementId}`);
1715
}, [measurementId]);
1816

1917
if (!measurementId || measurementId.trim() === '') {
20-
if (process.env.NODE_ENV === 'development') {
21-
console.warn('[Google Analytics] No measurement ID provided. Check your NEXT_PUBLIC_ANALYTICS_ID environment variable.');
22-
}
18+
console.warn('[Google Analytics] No measurement ID provided. Check your NEXT_PUBLIC_ANALYTICS_ID environment variable.');
2319
return null;
2420
}
2521

2622
// Validate GA4 ID format (should start with G-)
23+
// GA4 measurement IDs follow the format: G-XXXXXXXXXX (alphanumeric after G-)
24+
// Note: The validation is a warning only - Google Analytics will handle invalid IDs
2725
if (!measurementId.startsWith('G-')) {
2826
console.warn(
29-
'[Google Analytics] Invalid measurement ID format. GA4 IDs should start with "G-".',
30-
'Current ID:', measurementId
27+
'[Google Analytics] Warning: Measurement ID does not start with "G-".',
28+
'GA4 measurement IDs should start with "G-" (e.g., G-XXXXXXXXXX).',
29+
'Current ID:', measurementId,
30+
'If this is a valid GA4 ID from your Google Analytics dashboard, you can ignore this warning.'
3131
);
3232
}
3333

env.example

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,15 @@ NEXT_PUBLIC_EMAILJS_PUBLIC_KEY=your_public_key_here
120120
# ANALYTICS & MONITORING
121121
# ============================================================================
122122

123+
# Enable Google Analytics 4 (GA4) tracking
124+
# Set to "true" to enable analytics tracking
123125
NEXT_PUBLIC_ANALYTICS_ENABLED=false
124-
NEXT_PUBLIC_ANALYTICS_ID=G-nexus-55966
126+
127+
# Google Analytics 4 Measurement ID
128+
# Format: G-XXXXXXXXXX (e.g., G-ABC123XYZ)
129+
# Find this in: Google Analytics → Admin → Data Streams → Your Stream → Measurement ID
130+
# IMPORTANT: Use your actual GA4 Measurement ID from Google Analytics dashboard
131+
NEXT_PUBLIC_ANALYTICS_ID=G-XXXXXXXXXX
125132
NEXT_PUBLIC_ERROR_REPORTING_ENABLED=true
126133
NEXT_PUBLIC_ERROR_REPORTING_API_KEY=your_error_reporting_api_key_here
127134

lib/utils/client-env.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ const clientEnvSchema = z.object({
4747
NEXT_PUBLIC_ANALYTICS_ENABLED: z
4848
.string()
4949
.optional()
50-
.transform(val => val === 'true')
50+
.transform(val => val?.toLowerCase() === 'true' || val === '1')
5151
.default(() => false),
5252
NEXT_PUBLIC_ANALYTICS_ID: z.string().optional().default(''),
5353
NEXT_PUBLIC_ERROR_REPORTING_ENABLED: z
5454
.string()
5555
.optional()
56-
.transform(val => val === 'true')
56+
.transform(val => val?.toLowerCase() === 'true' || val === '1')
5757
.default(() => false),
5858
NEXT_PUBLIC_ERROR_REPORTING_API_KEY: z.string().optional().default(''),
5959
});

0 commit comments

Comments
 (0)