Prepared the application for Google Analytics 4 tracking with full GDPR/RODO compliance. GA only loads after user explicitly accepts cookies. Features: - Automatic page view tracking on route changes - Custom event tracking for key user actions - Privacy-first: GA loads only after cookie consent - Easy configuration via environment variable - Comprehensive tracking utilities for common events Implementation: - Created analytics.js with GA initialization and event tracking functions - Created usePageTracking hook for automatic page view tracking - Integrated GA into App.jsx with AnalyticsWrapper component - Updated CookieConsent to initialize GA after user consent - Added VITE_GA_MEASUREMENT_ID to .env.example Custom events tracked: - login, sign_up (authentication) - match_request, match_accepted (matching) - webrtc_connection, file_transfer (WebRTC) - event_join, recording_suggestion (events/recording) - search (search functionality) Setup: 1. Add VITE_GA_MEASUREMENT_ID=G-XXXXXXXXXX to .env 2. Restart frontend container 3. GA will auto-load after user accepts cookies Documentation: - Created comprehensive setup guide in docs/GOOGLE_ANALYTICS_SETUP.md - Includes troubleshooting, debugging tips, and usage examples
3.7 KiB
3.7 KiB
Google Analytics Setup Guide
This document explains how to configure Google Analytics 4 (GA4) for spotlight.cam.
Prerequisites
- Google Analytics account
- GA4 property created for spotlight.cam
Setup Instructions
1. Get your Measurement ID
- Go to Google Analytics
- Select your property
- Go to Admin (bottom left)
- Under Property, click Data Streams
- Select your web stream (or create one)
- Copy the Measurement ID (format:
G-XXXXXXXXXX)
2. Configure Environment Variable
Add your Measurement ID to the .env file in the project root:
VITE_GA_MEASUREMENT_ID=G-XXXXXXXXXX
Replace G-XXXXXXXXXX with your actual Measurement ID.
3. Restart the Application
For the changes to take effect:
# Rebuild and restart the frontend container
docker compose up -d --build frontend
How It Works
Automatic Tracking
The application automatically tracks:
- Page views: Every route change is tracked
- User consent: GA only loads if user accepts cookies
- Custom events: See below for list of tracked events
Privacy & GDPR Compliance
- GA script loads only after user accepts cookies
- If user declines cookies, no tracking occurs
- Consent is stored in
localStorage
Custom Events
The following custom events are tracked:
| Event | Description | Parameters |
|---|---|---|
login |
User logs in | method |
sign_up |
User registers | method |
match_request |
User sends match request | event_slug |
match_accepted |
User accepts match | match_slug |
webrtc_connection |
WebRTC connection status | status |
file_transfer |
File transfer via WebRTC | file_size, success |
event_join |
User joins event | event_slug |
recording_suggestion |
Recording suggestion action | action |
search |
User searches | search_term |
Using Analytics in Code
Import and use the tracking functions:
import { trackEvent, trackLogin, trackMatchRequest } from './utils/analytics';
// Track custom event
trackEvent('button_click', { button_name: 'register' });
// Track login
trackLogin('email');
// Track match request
trackMatchRequest('event-slug-123');
Debugging
Check if GA is loaded
Open browser console and run:
console.log(window.gtag); // Should be a function
console.log(window.dataLayer); // Should be an array
Check cookie consent
console.log(localStorage.getItem('cookieConsent')); // 'accepted' or 'declined'
View GA events in real-time
- Go to Google Analytics
- Navigate to Reports → Realtime
- You should see your page views and events as they happen
Troubleshooting
GA not loading
- Check that
VITE_GA_MEASUREMENT_IDis set in.env - Verify user accepted cookies (check browser localStorage)
- Check browser console for errors
- Ensure frontend container was rebuilt after adding env variable
Events not showing up
- Wait 24-48 hours for events to appear in standard reports
- Use Realtime view for immediate feedback
- Check that events are being sent:
console.log(window.dataLayer)
Ad blockers
Note: Users with ad blockers may block Google Analytics. This is expected behavior.
Production Deployment
When deploying to production:
- Set
VITE_GA_MEASUREMENT_IDin production environment - Verify tracking works in production domain
- Update GA4 property settings if domain changes
- Configure data retention and user deletion settings in GA