Google Analytics is the standard when it comes to web analytics tools. But many WordPress users struggle when it comes time to actually add Google Analytics to WordPress and start tracking their site. To help with that, we’re going to show you how to add Google Analytics to WordPress using a few different methods.
First, we’ll show you a simple way to add Google Analytics to WordPress using a free plugin. Then, we’ll show you four more ways – how to manually add the Google Analytics tracking code directly to your theme and how to use a slightly more advanced Google Analytics plugin to both add the tracking code to your site and view basic stats about your site right inside your WordPress dashboard. You could also add Google Analytics to your site with the Google Tag Manager or sync it locally for those of you picky about performance.
We’ll show you the complete process for each method – the only thing we’ll assume is that you already have signed up for a Google Analytics account. If you haven’t, you can go ahead and do that here. Then, if one of these methods has already piqued your interest, you can click one of these links to jump straight there:
Analytics Cat is a lightweight Google Analytics plugin that literally only takes a couple of minutes to set up. While it is lightweight, the advantage of Analytics Cat over other lightweight tools is that it has some Google Analytics-specific functionality that lets you exclude certain logged-in users from being tracked to ensure the integrity of your Google Analytics data.
It currently over 4,000 active installs with a 5 out of 5-star rating. To use the plugin, first install and activate it like you would any other WordPress plugin. Because it’s listed at WordPress.org, you can do so directly from your WordPress dashboard by going to Plugins → Add New:
Once you activate the plugin, go to Settings → Google Analytics Manager to complete the setup.
On the first page, the plugin will ask to connect your site. That’s not necessary to use it. Feel free to click Skip This Step:
Now, all you need to do is enter your Google Analytics ID and choose which users to exclude from tracking:
We recommend leaving the Exclude Users option as the default.
To find your Google Analytics ID, head to your Google Analytics account. Once you’re logged in:
On the next screen, you should see your Tracking ID:
Copy that value. Then, head back to your WordPress site and paste your Tracking ID into Analytics Cat. Then, click Save to activate your tracking:
That’s it! As your site receives traffic, you should be able to view its stats in your Google Analytics dashboard.
If you’d prefer not to use a plugin, it’s also fairly simple to add the Google Analytics tracking code directly to your WordPress theme via your functions.php file.
If you go the manual route, though, it’s essential that you use a WordPress child theme. Otherwise, your tracking code will be overwritten every time you update your WordPress theme.
We have a guide on how to get started with WordPress child themes if you’re not sure what to do.
To do things manually, the easiest way is to paste your Google Analytics Tracking Code right before the closing </head> tag in your header.php file.
To find your Google Analytics Tracking Code, go to your Google Analytics account. Then:
On the next screen, look for the script inside the Website Tracking box:
This is the script you need to insert in your header.php file.
To add it, go to Appearance → Editor and select the header.php file for your child theme.
Paste the Google Analytics Tracking Code right before the </head> tag in the Editor:
Then, make sure to click Update File. Your Google Analytics tracking code is now active. Another option is you could utilize a free plugin like Header and Footer which allows you to easily add code to your WordPress header.
If you want more functionality than Analytics Cat, Google Analytics Dashboard for WP is a free plugin that:
It currently has over 1 million active installs with a 4.5 out of 5-star rating. To use the plugin, you can install it directly from your dashboard by going to Plugins → Add New and searching for it:
Once you activate the plugin, go to the new Google Analytics tab and click the button to Authorize Plugin:
On the next screen, click Get Access Code to get the code needed to authorize the plugin:
On the next screen, choose your Google account that you use to access Google Analytics:
Then, click Allow on the next screen:
On the next screen, you’ll see the access code that you need. Copy that value:
Then, paste that code into the Google Analytics Dashboard for WP interface and click Save Access Code:
And that’s it! Your Google Analytics tracking is now active.
If you want to exclude certain users from being tracked, you can go to Tracking Code → Exclude Tracking to further configure how your tracking code functions:
The Tracking Code area is also where you can set up more advanced options like Event Tracking.
You’ll also be able to view stats for any post on your site by going to the Posts tab in your dashboard:
Another method you can use to add Google Analytics to WordPress is by using the Google Tag Manager. This is really meant to be used for those of you with a lot of scripts, not just Analytics. Google Tag Manager is a tag management system that allows you to quickly and easily update tags and code snippets on your website or mobile app, such as those intended for traffic analysis and marketing optimization. You can add and update scripts from the Tag Manager user interface instead of editing site code in WordPress directory. This reduces errors and frees you from having to involve a developer when configuring tags.
The easiest way to do this is to use DuracellTomi’s free Google Tag Manager WordPress plugin. It currently has over 100,000 active installs with a 4.5 out of 5-star rating.
You can download the Google Tag Manager plugin from the WordPress repository or by searching for it under “Add New” plugins in your WordPress dashboard. You will then need to signup for a Google Tag Manager account if you haven’t already and setup Google Analytics. We recommend following the official documentation on how to install Google Analytics via Google Tag Manager.
Once you have completed the above you can copy your container ID from the Google Tag Manager dashboard.
Then return to your WordPress site and paste your Google Tag Manager ID in the plugin’s settings.
Any tag you have setup in Google Tag Manager will now work on your WordPress site using the plugin.
The final method you can use is to sync the Google Analytics script locally on your own server. Please be aware though that this isn’t supported by Google. But a couple reasons you might want to do this is that you can reduce your external HTTP requests to Google from 2 down to 1 and you now have full control over the caching of the file. This means you can utilize your own server’s cache headers.
You have also probably seen the leverage browser caching warning in Google PageSpeed Insights that comes from Google Analytics. This is kind of ironic seeing as this is Google’s own script. The issue is that they set a low 2 hour cache time on their asset, as seen in the screenshot below. They most likely do this because if for some reason they were to modify something on their end, they want all users to get the changes as fast as possible. However there is a way to get around this, and that is by hosting Google Analytics script on your own server.
There is a great free little plugin called the Complete Analytics Optimization Suite or CAOS for short, created and developed by Daan van den Bergh, which allows you to host Google Analytics locally on your WordPress website. It currently has over 10,000 active installs with a 5 out of 5-star rating.
Just install the plugin, enter your Google Analytics Tracking ID, and the plugin adds the necessary tracking code for Google Analytics to your WordPress website, downloads and saves the analytics.js file to your server and keeps it updated using a scheduled script in wp_cron(). We recommend also setting it to load in the footer. Note: This plugin won’t work with other Google Analytics WordPress plugins.
And that wraps up our guide on four different ways how to add Google Analytics to WordPress.
If you’re just a casual WordPress user, we recommend using one of the two plugin methods. But, again, if you’d prefer not to use a plugin, it’s also quite easy to add Google Analytics tracking code directly into your theme files. Just make sure to use a child theme so that your tracking code doesn’t get overwritten when you update your theme. And for those of you really pick about performance, you can also sync Google Analytics locally.
Bonus tip: If you have a WooCommerce store we recommend you to check out this guide for additional tips: WooCommerce & Google Analytics Pro Setup.
Have any thoughts or different methods you use to add Google Analytics to WordPress? If so, we would love to here them below.
Send this to a friend