Feature ID: 019
Category: enhancements
Source: ScriptHammer/docs/specs/008-google-analytics
Status: Implemented — Awaiting Measurement ID (2026-04-08) — src/lib/analytics/GoogleAnalytics/ has full 5-file pattern (component, test, stories, accessibility.test). GA4 script injection ready; activates when NEXT_PUBLIC_GA_MEASUREMENT_ID is set. Consent-gated via the cookie consent framework. Each template fork provides their own measurement ID.
A privacy-conscious Google Analytics 4 (GA4) integration with consent management that tracks user behavior, Web Vitals, and custom events. Only activates after explicit user consent.
Analytics tracking only begins after user explicitly grants consent via the cookie consent banner.
Acceptance Criteria:
- Given no consent provided, when page loads, then GA4 script is NOT loaded
- Given analytics consent granted, when page loads, then GA4 initializes
- Given consent withdrawn, when next page loads, then tracking stops
Core Web Vitals (CLS, FCP, LCP, TTFB) are automatically sent to GA4 for performance monitoring.
Acceptance Criteria:
- Given consent granted, when page renders, then Web Vitals are measured
- Given Web Vitals collected, when sent to GA4, then they appear in reports
- Given vitals measured, when in dev mode, then they also log to console
Key user interactions are tracked as custom events.
Acceptance Criteria:
- Given consent granted, when user changes theme, then event is tracked
- Given consent granted, when form submitted, then event is tracked
- Given consent granted, when PWA installed, then event is tracked
Navigation events are tracked as page views.
Acceptance Criteria:
- Given consent granted, when user navigates, then page_view event fires
- Given SPA navigation, when route changes, then new page_view is sent
- Given page view, when tracked, then page_path and page_title included
- FR-001: GA4 loads ONLY after cookie consent granted
- FR-002: Web Vitals automatically tracked to GA4
- FR-003: Custom events for theme change, form submit, PWA install, search, errors
- FR-004: Debug mode enabled in development environment
- FR-005: CSP headers updated for Google domains
- FR-006: Works with all 32 themes
- FR-007: No impact on Lighthouse scores
- FR-008: Privacy mode when consent denied (no tracking at all)
- NFR-001: Script loads async/afterInteractive
- NFR-002: Graceful degradation when ad blockers present
- NFR-003: No errors thrown when GA blocked
- GoogleAnalytics: Component that conditionally loads GA4
- analytics.ts: Utility functions for tracking
- useAnalytics: Hook with pre-built tracking methods
NEXT_PUBLIC_GA_MEASUREMENT_ID=G-XXXXXXXXXXscript-src: https://www.googletagmanager.com https://www.google-analytics.com
connect-src: https://www.google-analytics.com https://analytics.google.com
- Google Ads integration
- Enhanced ecommerce tracking
- Server-side tracking
- Google Tag Manager
- Universal Analytics (deprecated)
- SC-001: GA4 never loads without consent
- SC-002: Web Vitals visible in GA4 DebugView
- SC-003: Custom events appear in real-time reports
- SC-004: Page views tracked for all navigation
- SC-005: No Lighthouse score regression
- SC-006: Works with ad blockers (graceful degradation)