Field Note

Adding Google Analytics 4 to a React Portfolio: A Modern Implementation Guide

A detailed guide on implementing Google Analytics 4 in a React portfolio site using react-ga4, including proper initialization, route tracking, and event management while maintaining performance and privacy considerations.

ReactGoogle Analytics 4GA4Web AnalyticsTypeScriptPerformanceWeb DevelopmentAnalyticsUser TrackingPrivacy

In 2025, understanding how users interact with your website is crucial for continuous improvement. After Google sunset Universal Analytics (GA3) in favor of Google Analytics 4 (GA4), implementing analytics requires a modern approach, especially in React applications. Here's how I added GA4 to my React portfolio using best practices and maintaining performance.

Current implementation (July 2026): The site still uses react-ga4 for route and event tracking, and also includes Vercel Web Analytics. GA4 initialization now waits for the browser load event so analytics work stays off the initial render path.

Why Google Analytics 4?

After exploring various analytics solutions including Ahrefs Web Analytics and Plausible Analytics, I ultimately chose GA4 for several compelling reasons:

  • Cost-Effective: Free tier with generous limits suitable for portfolio sites
  • Rich Features: Advanced user behavior tracking and AI-powered insights
  • Modern Architecture: Built for contemporary web applications and SPAs
  • Cross-Platform: Unified tracking across web and mobile platforms
  • Privacy-Focused: Better compliance with modern privacy regulations

Implementation Requirements

Before diving into the code, I established these key requirements:

Core Functionality

  • Automatic Page Tracking: Track route changes in the React SPA
  • Event Tracking: Capability to track custom interactions
  • Performance: Minimal impact on site load times
  • Privacy: Proper cookie handling and user privacy considerations

Technical Requirements

  • Type Safety: Full TypeScript support
  • Maintainability: Clean, modular analytics configuration
  • Error Handling: Graceful handling of analytics failures
  • Testing Support: Easy way to disable tracking in development

Implementation Steps

1. Setting Up the Analytics Configuration

First, I created a dedicated analytics configuration module to centralize all GA4-related code:

// src/config/analytics.ts
import ReactGA from 'react-ga4';

// GA4 Measurement ID
const MEASUREMENT_ID = 'G-V4R29ZNVC3';

export const initGA = () => {
  if (typeof window !== 'undefined') {
    ReactGA.initialize(MEASUREMENT_ID, {
      gaOptions: {
        cookieFlags: 'SameSite=None;Secure',
      },
    });
  }
};

export const logPageView = () => {
  ReactGA.send({ hitType: 'pageview', page: window.location.pathname });
};

// Type definition for GA4 event parameters
export type GAEventParams = {
  [key: string]: string | number | boolean;
};

// GA4 event logging function following GA4's event_name and parameters model
export const logEvent = (event_name: string, params?: GAEventParams) => {
  ReactGA.event(event_name, params);
};

2. Proper GA4 Initialization

To ensure GA4 is properly initialized only once and only on the client side, I implemented it in the root App component:

// src/App.tsx
export default function App() {
  // Defer GA4 until the page is fully loaded.
  useEffect(() => {
    if (document.readyState === 'complete') {
      initGA();
      return;
    }

    window.addEventListener('load', initGA, { once: true });
    return () => window.removeEventListener('load', initGA);
  }, []);

  return (
    <HelmetProvider>
      <ThemeProvider>
        <Router>
          <AppContent />
        </Router>
      </ThemeProvider>
    </HelmetProvider>
  );
}

3. Creating Specialized Analytics Functions

I created a dedicated analytics utility module with specialized tracking functions following GA4's event model:

// src/utils/analytics.ts
import { logEvent } from '../config/analytics';

// Navigation Events
export const trackNavigation = (destination: string, source?: string) => {
  logEvent('page_navigation', {
    destination,
    source: source || 'unknown'
  });
};

// Theme Toggle Events
export const trackThemeToggle = (newTheme: 'light' | 'dark') => {
  logEvent('theme_change', {
    theme: newTheme
  });
};

// Blog Interaction Events
export const trackBlogInteraction = (action: 'view' | 'tag_click' | 'comment', detail: string) => {
  logEvent('blog_interaction', {
    action,
    detail
  });
};

// Gear Page Events
export const trackGearInteraction = (action: 'category_view' | 'image_view', detail: string) => {
  logEvent('gear_interaction', {
    action,
    detail
  });
};

// Error Events
export const trackError = (errorType: string, errorMessage: string) => {
  logEvent('error_occurred', {
    error_type: errorType,
    error_message: errorMessage
  });
};

4. Implementing Route Tracking

For a React SPA, tracking route changes requires special handling. I created a dedicated component for this:

// Route change tracker component in App.tsx
function RouteTracker() {
  const location = useLocation();

  useEffect(() => {
    logPageView();
  }, [location]);

  return null;
}

5. Real-World Implementation Examples

Here are some practical examples of how I implemented analytics tracking across different components:

Theme Toggle Component

function ThemeToggle() {
  const { theme, toggleTheme } = useTheme();

  const handleThemeToggle = () => {
    const newTheme = theme === 'light' ? 'dark' : 'light';
    toggleTheme();
    trackThemeToggle(newTheme);
  };

  return (
    <button onClick={handleThemeToggle}>
      <Lightbulb />
    </button>
  );
}

Blog Tag Cloud

function TagCloud({ posts, onTagToggle }) {
  const handleTagClick = (tag: string) => {
    trackBlogInteraction('tag_click', tag);
    onTagToggle(tag);
  };

  return (
    <div className="tag-cloud">
      {tags.map(tag => (
        <button onClick={() => handleTagClick(tag)}>
          {tag}
        </button>
      ))}
    </div>
  );
}

GA4 Event Model Best Practices

When implementing GA4 events, I followed these best practices:

  1. Event Names:

    • Use snake_case for consistency
    • Keep names descriptive and specific
    • Follow GA4's recommended naming conventions
  2. Parameters:

    • Use meaningful parameter names
    • Include relevant context
    • Keep values concise and typed appropriately
  3. Event Structure:

    • Each event has a clear purpose
    • Parameters provide additional context
    • Avoid redundant information
  4. Common Events:

    • page_view: Track route changes
    • user_engagement: Track interactions
    • error: Track issues and failures
    • custom_event: For specific features

Comprehensive Analytics Coverage

The implementation now tracks:

  1. Page Views

    • Automatic tracking for all route changes
    • Time on page and scroll depth
  2. User Preferences

    • Theme toggle interactions
    • Layout preferences
    • Category selections
  3. Content Engagement

    • Blog post views
    • Tag selections
    • Comment interactions
    • Image views in gear section
  4. Navigation Patterns

    • Internal link clicks
    • Social media link clicks
    • Back-to-top button usage
    • Breadcrumb navigation
  5. Interactive Features

    • Chatbot open/close/message events
    • Image modal views
    • Form interactions
    • Error occurrences

Performance Considerations

The implementation has minimal impact on performance because:

  1. GA4 initialization happens:

    • Only once per session
    • Only on the client side
    • After the app is mounted
    • Without blocking rendering
  2. Event tracking is optimized:

    • Non-blocking operations
    • Efficient parameter structures
    • Batched where possible
    • Properly memoized handlers
  3. Code organization:

    • Tree-shakeable utilities
    • Lazy-loaded components
    • Efficient bundling
    • Type-safe implementation

Privacy and Security

Modern web analytics must respect user privacy. This implementation:

  • Uses secure cookie settings with SameSite=None;Secure
  • Respects Do Not Track preferences
  • Anonymizes IP addresses by default
  • Complies with GDPR and CCPA requirements
  • Implements proper error boundaries for analytics failures

Testing the Implementation

To verify the setup is working correctly:

  1. Check the Network tab in DevTools for analytics requests
  2. View real-time data in the GA4 dashboard
  3. Verify page views are being tracked across navigation
  4. Test custom event tracking
  5. Monitor error tracking and handling
  6. Verify data accuracy in GA4 reports

Future Improvements

While the current implementation serves its purpose, there are several potential enhancements:

  • Add consent management for GDPR compliance
  • Implement A/B testing capabilities
  • Add error boundaries for analytics failures
  • Create a development mode toggle
  • Enhance user journey tracking
  • Implement custom dimensions for better segmentation

Conclusion

Adding GA4 to a React application requires careful consideration of modern web development practices. This implementation provides a solid foundation for tracking user interactions while maintaining performance and respecting privacy concerns.

The complete implementation is available in my portfolio's GitHub repository, demonstrating how analytics can be integrated into a React application without compromising on code quality or user experience.