Google Analytics provides invaluable insights into your website’s traffic and user behavior. Follow these simple steps to integrate Google Analytics with your WordPress site.
Method 1: Using a plugin
MonsterInsights is the most popular Google Analytics plugin for WordPress. Over 1 million websites use it, including Bloomberg, PlayStation, Zillow, and more.
Install the plugin:
-
Go to your site admin area.
-
Navigate to Plugins -> Add New.
-
Search for “Google Analytics for WordPress“.
-
Install and activate the plugin.
Configure the plugin:
-
Go to your site admin area -> click the Insights menu item.
-
Launch the setup wizard to connect to Google Analytics.
Method 2: Adding code via theme hook
This method requires having the child theme installed and activated – learn how to install the child theme. Using a child theme’s functions.php file allows you to add Google Analytics code without directly modifying the theme. Here’s how to do it for our themes:
-
Go to your site admin area.
-
Navigate to Appearance > Theme File Editor.
-
Ensure that your active child theme is selected from the dropdown in the upper-right corner.
-
On the right side, find and click on Theme Functions (functions.php).
-
Scroll to the bottom of the functions.php file and add the following code snippet:
function add_google_analytics_code() {
?>
<script async src="https://www.googletagmanager.com/gtag/js?id=YOUR_ANALYTICS_ID"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'YOUR_ANALYTICS_ID');
</script>
<?php
}
add_action('wp_head', 'add_google_analytics_code');
Important: replace YOUR_ANALYTICS_ID with your actual Google Analytics Tracking ID.
Now, your WordPress site is integrated with Google Analytics, providing you with essential data to enhance your online presence.