Code Snippet to Add Google Analytics Tracking Code in WordPress

Mateusz K Jan 4, 2025 Analytics Integration
How can I track how many people visit my website?
What PHP code snippet can I use to safely insert the Google Analytics tracking code into the header of my WordPress theme?
Andy answered Jan 4, 2025

Solution 1: Custom Code Implementation

Add this code to your theme's functions.php file or a site-specific plugin. This code uses the wp_head hook to insert Google Analytics tracking code in the correct location:

function add_google_analytics() {
    // Replace UA-XXXXX-Y with your actual Google Analytics tracking ID
    ?>
    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXX-Y"></script>
    <script>
        window.dataLayer = window.dataLayer || [];
        function gtag(){dataLayer.push(arguments);}
        gtag('js', new Date());
        gtag('config', 'UA-XXXXX-Y');
    </script>
    <?php
}
add_action('wp_head', 'add_google_analytics');

Solution 2: Modern GA4 Implementation

For GA4 (latest version of Google Analytics), use this code:

function add_google_analytics_ga4() {
    // Replace G-XXXXXXXXXX with your actual GA4 measurement ID
    ?>
    <!-- Google tag (gtag.js) -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>
    <script>
        window.dataLayer = window.dataLayer || [];
        function gtag(){dataLayer.push(arguments);}
        gtag('js', new Date());
        gtag('config', 'G-XXXXXXXXXX');
    </script>
    <?php
}
add_action('wp_head', 'add_google_analytics_ga4');

Plugin Alternatives

If you prefer using a plugin, here are reliable options:

  1. MonsterInsights - Most popular analytics plugin
  2. GA Google Analytics - Lightweight option
  3. Site Kit by Google - Official Google plugin

Best Practices

  • Always replace the tracking ID (UA-XXXXX-Y or G-XXXXXXXXXX) with your actual Google Analytics ID
  • Consider using a child theme if modifying functions.php
  • For better performance, consider loading analytics code with the async or defer attribute
  • Test tracking implementation using Google Analytics real-time reports after setup

Advanced Implementation (with Privacy Controls)

This version includes basic privacy controls and loads analytics only when necessary:

function add_google_analytics_with_privacy() {
    // Don't track admin users
    if (current_user_can('manage_options')) {
        return;
    }
    
    // Replace G-XXXXXXXXXX with your GA4 measurement ID
    ?>
    <script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>
    <script>
        window.dataLayer = window.dataLayer || [];
        function gtag(){dataLayer.push(arguments);}
        gtag('js', new Date());
        gtag('config', 'G-XXXXXXXXXX', {
            'anonymize_ip': true,
            'allow_display_features': false
        });
    </script>
    <?php
}
add_action('wp_head', 'add_google_analytics_with_privacy');

This code excludes admin users from tracking and includes basic privacy-enhancing settings.