Analytics Configuration Update
Changes Made
1. Environment Detection for Analytics Tracking
The analytics module now automatically detects whether itβs running in a dev/local environment and disables tracking accordingly.
Tracked Environments:
- β Production
- β Deployed previews
- β localhost (tracking disabled)
- β 127.0.0.1 (tracking disabled)
- β *.local domains (tracking disabled)
2. Console Output with Status
Events now show tracking status in console:
β [Analytics Event] { ... } // Tracking enabled
β [Analytics Event] (disabled on dev) { ... } // Tracking disabled
3. Google Analytics Configuration
Added to .env.local:
NEXT_PUBLIC_GA_ID=G-K1F96PRZNS
The GA ID is automatically loaded from environment variables and configured at initialization.
4. Updated Analytics Module Features
New config option:
interface TrackingConfig {
enableLogging?: boolean; // Console logging (default: dev mode)
googleAnalyticsId?: string; // GA ID from env
customEndpoint?: string; // Custom tracking endpoint
enableTracking?: boolean; // Force enable/disable tracking
}
Auto-detection Logic:
- Dev environments: tracking disabled by default
- Production: tracking enabled
- Can be overridden with
analytics.configure({ enableTracking: true/false })
How It Works
- Analytics initializes with GA ID from
NEXT_PUBLIC_GA_ID - Environment detection checks if running on localhost or .local domain
- Tracking disabled on dev/local β events logged to console but NOT sent
- Production tracking β events sent to Google Analytics
Testing
Development Mode (Localhost)
Open DevTools β Console
Perform an action (bookmark, click image, etc.)
See: β [Analytics Event] (disabled on dev) { ... }
β No data sent to Google Analytics
Production Mode
Same actions will show: β [Analytics Event] { ... }
β Data sent to Google Analytics (G-K1F96PRZNS)
Override Tracking Status
If you need to enable/disable tracking manually:
import { analytics } from "@/lib/analytics";
// Force enable tracking in dev
analytics.configure({ enableTracking: true });
// Force disable tracking everywhere
analytics.configure({ enableTracking: false });
No Additional Setup Required
β GA ID configured in .env.local β Dev/local environment auto-detection active β Console logging shows status β Ready for production deployment