How to Add Google Analytics to WordPress (Ultimate Guide)

Updated on November 26, 2017

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.

Add Google Analytics to WordPress

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:

Method 1: Add Google Analytics to WordPress With Analytics Cat

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.

Analytics Cat WordPress plugin

Analytics Cat WordPress plugin

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:

How to install Analytics Cat

How to install Analytics Cat

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:

Skip this step

Skip this step

Now, all you need to do is enter your Google Analytics ID and choose which users to exclude from tracking:

Analytics Cat configuration interface

Analytics Cat configuration interface

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:

  • Click the Gear icon to access your administration page
  • Select Tracking Info to expand the list
  • Select Tracking Code
How to find Google Analytics Tracking ID

How to find Google Analytics Tracking ID

On the next screen, you should see your Tracking ID:

Your Google Analytics Tracking ID

Your Google Analytics 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:

Add Google Analytics to WordPress With Analytics Cat

Finish the configuration

That’s it! As your site receives traffic, you should be able to view its stats in your Google Analytics dashboard.

Method 2: Manually Add Google Analytics to WordPress Child Theme

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:

  • Click the Gear icon to access your administration page
  • Select Tracking Info to expand the list
  • Select Tracking Code
How to find Google Analytics Tracking ID

How to find Google Analytics Tracking ID

On the next screen, look for the script inside the Website Tracking box:

Google Analytics Website Tracking code

Google Analytics Website Tracking code

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:

Add Google Analytics Tracking Code to header.php

Add Google Analytics Tracking Code to header.php

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.

Method 3: Add Google Analytics to WordPress With Google Analytics Dashboard for WP

If you want more functionality than Analytics Cat, Google Analytics Dashboard for WP is a free plugin that:

  • Helps you add Google Analytics to WordPress
  • Lets you view basic stats about your site right from your WordPress dashboard
  • Gives you access to advanced features like outbound link tracking, other Event tracking options, and custom dimensions
Google Analytics Dashboard for WP plugin

Google Analytics Dashboard for WP plugin

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:

Install Google Analytics Dashboard for WP

Install Google Analytics Dashboard for WP

Once you activate the plugin, go to the new Google Analytics tab and click the button to Authorize Plugin:

Authorize Google Analytics Dashboard for WP

Authorize Google Analytics Dashboard for WP

On the next screen, click Get Access Code to get the code needed to authorize the plugin:

Get Google Analytics access code

Get Google Analytics access code

On the next screen, choose your Google account that you use to access Google Analytics:

Select your Google Analytics account

Select your Google Analytics account

Then, click Allow on the next screen:

Allow access to Google Analytics Dashboard for WP

Allow access to Google Analytics Dashboard for WP

On the next screen, you’ll see the access code that you need. Copy that value:

Your access code

Your access code

Then, paste that code into the Google Analytics Dashboard for WP interface and click Save Access Code:

Save access code

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:

Exclude certain user roles from tracking

Exclude certain user roles from tracking

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:

View stats in your dashboard

View stats in your dashboard

Method 4: Add Google Analytics With Google Tag Manager

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.

DuracellTomi's Google Tag Manager for WordPress plugin

DuracellTomi’s Google Tag Manager for WordPress plugin

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.

container ID Google Tag Manager

container ID Google Tag Manager

Then return to your WordPress site and paste your Google Tag Manager ID in the plugin’s settings.

Google Tag Manager ID

Google Tag Manager ID

Any tag you have setup in Google Tag Manager will now work on your WordPress site using the plugin.

Method 5: Sync Google Analytics Locally for Performance Reasons

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.

leverage browser caching-pagespeed insights analytics
Google Analytics caching

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 9,000 active installs with a 5 out of 5-star rating.

Complete Analytics Optimization Suite

Complete Analytics Optimization Suite

You can download Complete Analytics Optimization Suite from the WordPress repository or by searching for it under “Add New” plugins in your WordPress dashboard. The plugin allows you to host your Google Analytics JavaScript file (analytics.js) locally and keep it updated using wp_cron(). Other features include being able to easily anonymize the IP address of your visitors, set an adjusted bounce rate, and placement of the script (header or footer).

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.

Settings to sync Google Analytics locally

Settings to sync Google Analytics locally

Summary

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.

This article was written by Brian Jackson

Brian focuses on our inbound marketing efforts; everything from developing new online growth strategies, content creation, technical SEO, and outreach within the WordPress community. He has a huge passion for WordPress, has been using it for 8+ years, and even develops a couple premium plugins. Brian enjoys blogging, movies, bike rides, and flipping websites.

Hand-picked related articles

  1. Gravatar for this comment's author
    Visualmodo WordPress Themes October 7, 2017 at 9:43 am

    Very complete guide, awesome!

Leave a Reply

Use WordPress?

Join 20,000+ others who get our FREE weekly newsletter with WordPress tips on how to drive more traffic and revenue to your business!

You have Successfully Subscribed!

Send this to a friend