Overview
This portfolio implements Google Analytics with a privacy-first approach, including GDPR-compliant cookie consent and IP anonymization. Analytics are only loaded after user consent is obtained.Google Analytics Setup
Create Google Analytics Property
- Go to Google Analytics
- Create a new property for your portfolio
- Set up a GA4 data stream
- Copy your Measurement ID (format:
G-XXXXXXXXXX)
Implementation Details
Google Analytics Component
The analytics implementation is located insrc/components/google-analytics.tsx:
Key Features
- Consent-Based Loading: Scripts only load after user consent
- IP Anonymization:
anonymize_ip: truefor GDPR compliance - Optimized Loading: Uses
afterInteractiveandlazyOnloadstrategies - Event-Driven: Responds to consent changes in real-time
- No Tracking ID Fallback: Safely handles missing environment variables
The component uses Next.js
Script component with optimized loading strategies to minimize performance impact.Cookie Consent Integration
Cookie Consent Component
The cookie consent banner is implemented insrc/components/cookie-consent.tsx:
The code below includes a link to
/privacy-policy, which is a page in the portfolio application. You can customize this link to point to your own privacy policy page.Consent Flow
- Initial Load: Check if user has previously provided consent
- Show Banner: Display cookie consent if no preference exists
- User Action: User accepts or declines cookies
- Store Preference: Save choice to
localStorage - Trigger Event: Dispatch
cookieConsentUpdateevent - Load Analytics: Google Analytics loads if consent is accepted
GDPR Compliance
The implementation follows GDPR requirements:Privacy-First Features
- Opt-In Only: Analytics only load after explicit consent
- IP Anonymization: User IP addresses are anonymized
- Clear Communication: Privacy policy link in consent banner
- Easy Opt-Out: Users can decline cookies
- Persistent Preference: Consent choice is remembered
- Reopenable: Users can change their preference via custom event
Privacy Policy
Ensure your privacy policy (located at/privacy-policy) includes:
- What data is collected
- How data is used
- Third-party services (Google Analytics)
- User rights (access, deletion, opt-out)
- Cookie information
- Contact information
Custom Event Tracking
You can track custom events after the user has given consent.Track Custom Events
TypeScript Declaration
Add type definitions for gtag in a global declaration file:global.d.ts
Analytics Best Practices
Performance Optimization
- Lazy Loading: Analytics script uses
lazyOnloadstrategy - Consent-Based: No unnecessary script loading for users who decline
- Minimal Impact: Next.js Script component optimizes loading
Privacy Best Practices
- Transparency: Clear messaging about cookie usage
- User Control: Easy opt-in and opt-out mechanisms
- Data Minimization: Only collect necessary analytics data
- IP Anonymization: Enabled by default
Data Collection Guidelines
Collect Only What You NeedFor a portfolio site, focus on:
- Page views and navigation patterns
- Popular projects or sections
- Traffic sources
- Geographic distribution (anonymized)
Testing Analytics
Local Testing
Check Real-Time Reports
Open Google Analytics and navigate to Reports > Real-time to see your session.
Production Testing
- Deploy your site with analytics enabled
- Visit your production URL
- Accept cookies
- Verify tracking in Google Analytics Real-time reports
- Test from different devices and browsers
Reopening Cookie Consent
Users can reopen the cookie consent banner programmatically:Troubleshooting
Analytics Not Loading
- Verify
NEXT_PUBLIC_GA_IDis set correctly - Check that cookies have been accepted
- Open browser console and look for errors
- Verify network requests to
googletagmanager.com
Events Not Tracking
- Ensure
window.gtagexists before calling - Check Google Analytics Real-time reports
- Verify event names and parameters
- Use DebugView for detailed event inspection
Cookie Consent Not Showing
- Check localStorage for existing
cookieConsentvalue - Clear localStorage and refresh
- Verify the component is included in the layout