Adding Google Analytics to WordPress - Custom Solution
Custom Code Solution
Add this code to your theme's functions.php
file or preferably in a site-specific plugin. This solution uses Google Analytics 4 (GA4) tracking.
Code to add GA4 tracking code to your WordPress site header:
function add_google_analytics() {
// Replace G-XXXXXXXXXX with your GA4 Measurement ID
$tracking_id = 'G-XXXXXXXXXX';
?>
<!-- Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=<?php echo esc_attr($tracking_id); ?>"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '<?php echo esc_attr($tracking_id); ?>');
</script>
<?php
}
add_action('wp_head', 'add_google_analytics');
Alternative Method (for older Universal Analytics):
function add_universal_analytics() {
// Replace UA-XXXXXXXXX-X with your Universal Analytics tracking ID
$tracking_id = 'UA-XXXXXXXXX-X';
?>
<!-- Google Analytics (Universal) -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', '<?php echo esc_attr($tracking_id); ?>', 'auto');
ga('send', 'pageview');
</script>
<?php
}
add_action('wp_head', 'add_universal_analytics');
Best Practice Tips
- Replace the tracking ID with your actual Google Analytics tracking code
- Test in debug mode to ensure the code loads correctly
- Consider using conditions to load analytics only on production environments
- Make sure to comply with privacy laws (GDPR, CCPA) by implementing proper consent mechanisms
Plugin Solutions
If you prefer using a plugin, here are reliable options:
-
MonsterInsights - Popular solution with detailed reporting
-
Google Site Kit - Official Google plugin for WordPress
-
GA Google Analytics - Lightweight plugin for basic tracking
Additional Considerations
- GA4 is now the standard tracking method as Universal Analytics was discontinued in July 2023
- Consider implementing cookie consent if serving EU visitors
- Test tracking implementation using Google Analytics Real-Time reports
- Use Google Tag Manager for more advanced tracking needs
Privacy-Conscious Implementation
Code to load analytics only after user consent (requires cookie consent implementation):
function add_google_analytics_with_consent() {
// Replace G-XXXXXXXXXX with your GA4 Measurement ID
$tracking_id = 'G-XXXXXXXXXX';
?>
<script>
function loadAnalytics() {
var script = document.createElement('script');
script.src = 'https://www.googletagmanager.com/gtag/js?id=<?php echo esc_attr($tracking_id); ?>';
script.async = true;
document.head.appendChild(script);
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '<?php echo esc_attr($tracking_id); ?>');
}
// Load analytics if consent is given
if (document.cookie.indexOf('analytics_consent=true') > -1) {
loadAnalytics();
}
</script>
<?php
}
add_action('wp_head', 'add_google_analytics_with_consent');