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

  1. Analytics initializes with GA ID from NEXT_PUBLIC_GA_ID
  2. Environment detection checks if running on localhost or .local domain
  3. Tracking disabled on dev/local β†’ events logged to console but NOT sent
  4. 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