How To add Google Analytics to WordPress

How to add Google Analytics to WordPress

Understanding how your customers are interacting with your website is a crucial step in achieving business success.

By adding Google Analytics to your WordPress account, you can track how your audience is finding your website, what they’re doing when they’re there, and which pages they’re dropping off at. This insight allows you to create a stronger, more functional website that is able to closely cater to your customer journey, providing a seamless user experience every step of the way. So let’s get started...
How to set up Google Analytics on WordPress

How to set up Google Analytics on WordPress

There are a few different ways to set up Google Analytics on your WordPress account.

If you don’t already have a Google Analytics account, you can find out how to start one in Google’s support guide.

Once you have an account, it’s time to set up your Google Analytics on WordPress. You can do this with either plugins, code, or Google Tag. We’ll explain these different methods below.

How to connect Google Analytics to WordPress with a plugin

The simplest way to set up Google Analytics on WordPress is with a plugin. This option is ideal if you have limited (or no) Analytics experience.

Which plugin you choose will depend on your needs, so make sure you look around to find the one that’s best for you before installing.

Some popular Google Analytics plugins for WordPress include:

  • Google Analytics by MonsterInsights. This is the most popular, with over 2 million active installs. This tracks site visitors, downloads, external links from your website, search results, and 404 error page hits.
  • Google Analytics Dashboard for WP by ExactMetrics. Formerly known as GADWP, this offers in-depth reports and presents your data on your WordPress dashboard. Overall, this functions very similarly to using the main Google Analytics platform.
  • Analytify. This plugin doesn’t require you to copy any code manually. Instead, it features a 1-click authentication process that automatically adds Google Analytics tracking code to your website. Easy.
  • Google Analytics WD by Web Dorado. This plugin boasts a beautiful user interface and can quickly add Google Analytics to your WordPress site, placing key analytics reporting features in your dashboard.
  • WP Statistics. This plugin is easy-to-use and lightweight. You can use it to monitor traffic for different search engines, understand the location of your users, and also track statistics of email. You can do all this from within your WordPress dashboard, but it’s equally simple to export in your chosen file format, too.

All of these plugins are available for free, though some are ‘freemium’, meaning they offer paid-for premium versions that give access to additional levels of reporting and data collection.

Google Analytic WordPress Plugins
How to install a plugin in WordPress

How to install a plugin in WordPress

Whichever plugin you choose, they are all installed in a similar way. Here’s how.

  1. Go to your WordPress dashboard.
  2. Click ‘Plugins’ on the sidebar, then select ‘Add New’ below.
  3. Search for your chosen plugin.
  4. Select the plugin you want, and press ‘Install Now’.
  5. Follow any instructions presented to you. You can seek out a guide specific to your plugin if you get stuck.
Add GA to WordPress

If you’re asked for your Google Analytics ID but don’t know where to find this, simply go back to your Google Analytics account, go to the ‘Admin’ screen, and then ‘Property Settings’. Once you’ve found your Google Analytics ID, copy and paste this where needed.

Once you’ve followed all the instructions and completed set-up, your plugin will be connected to WordPress and will automatically start tracking new visits.

Add GA to WordPress

How to add Google Analytics without a plugin

Without a plugin, you’ll need to add this code manually. This method is more likely to require some familiarity with Google Analytics and WordPress.

We recommend you add the Google Analytics code as a function to the functions.php file in your WordPress theme. Sound confusing? We’ll explain.

1. Create a child theme

First of all, its recommended that you use a child theme to add Google Analytics to your WordPress site. If you’re not sure how to create a child theme, you can follow this step-by-step WordPress guide to create a child theme.

2. Create a function

To add the Google Analytics Global Site Tag to your site, we need to add a function within your child themes functions.php This function will inject the Global Site Tag before your closing </head> tag on your site.

Open your function.php file in your child theme and add the following code:

// add Google Analytics to header
function ns_google_analytics() { ?>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=[YOURTRACKINGCODE]"></script>
	window.dataLayer = window.dataLayer || [];
	function gtag(){dataLayer.push(arguments);}
	gtag('js', new Date());
	gtag('config', '[YOURTRACKINGCODE]');

add_action( 'wp_head', 'ns_google_analytics', 10 );

3. Add your Tracking ID

In the above function we are using [YOURTRACKINGCODE] as a placeholder. This needs to be replaced with your Tracking ID.

You can find this Tracking ID in your Google Analytics admin dashboard at Admin > Tracking Info > Tracking Code

Add GA to WordPress

Once you have added your Tracking ID you can save and close functions.php

4. Check Google Analytics has been added

To verify your site now has the code snippet added, you can view the source code of your site.

Press CTRL+U in your prefered web browser, this will open up a new tab where you can view your source code. You can then search for your Tracking ID by pressing CTRL+F and entering your ID.

Your new Analytics tracking code should be located before the closing </head> as seen below.

Check GA is added to your website

You will now start to see data collected in your Google Analytics dashboard in the next 24 hours

How to install Google Analytics on WordPress via Google Tag Manager

If you’re confident with Analytics, you should consider Google Tag Manager.
Google Tag Manager allows you to easily manage tracking tags without editing code, even though setting this up often requires more advanced experience.

We break down the steps involved in installing Google Analytics on WordPress this way.

1. Set up Google Tag Manager

Visit Google Marketing Platform to set up a Google Tag Manager account.

Here, you’ll have two options: sign up for free or upgrade to the paid Tag Manager 360.

You can use the free version to install Google Analytics on your site, although Tag Manager 360 is a powerful tool that lets you oversee more complex tracking if required. The next step is to input the GTM tag onto your site.

2. Add Google Tag Manager to your WordPress Child Theme

Before we start creating tags and triggers, we need to add the Google Tag Manager code snippet to your child theme. If you do not have a child theme created its recommended that you follow this how to create a child theme guide.

3. Create 2 functions

To add the Google Tag Manager script to your site, we need to create 2 functions. The first function fires before the closing </head>. The second function will target users with JavaScript disabled and fires after the opening <body> tag.

Open your function.php file in your child theme and add the following code:

// add Google Tag Manager before closing head tag
function ns_google_tag_manager_head() { ?>
	<!-- Google Tag Manager -->
	new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
	<!-- End Google Tag Manager -->
add_action( 'wp_head', 'ns_google_tag_manager_head', 10 );

// add Google Tag Manager no script support to body tag
function ns_google_tag_manager_body() { ?>
	<!-- Google Tag Manager (noscript) -->
	<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=[YOURTRACKINGCODE]" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
	<!-- End Google Tag Manager (noscript) -->
add_action('wp_body_open', 'ns_google_tag_manager_body', 1);

Before we save the file we need to replace the 2 references to [YOURTRACKINGCODE]

Your unique GTM ID can be found in several places within your Google Tag Manager dashboard. Once logged in it is displayed on the top right of the screen and looks similar to this example GTM-NMDR3YU

Alternatively once in your dashboard you can browse to Admin > Install Google Tag Manager to preview the code snippet with your ID in place.

Copy and paste your ID into the 2 [YOURTRACKINGCODE] references and then save and close the file.

4. Create a new tag

Once you’ve added the above code snippets to your WordPress theme you can return to Google Tag Manager. Click on ‘Tags’ in the sidebar, then select ‘New’.

This will open a pop-up window. Here, we recommend naming your tag so it’s easy for you to find, plus makes sense to anyone else who may be using the same account. We suggest ‘Analytics Base Tag’.

Create a new Tag in Tag Manager

5. Configure this tag

Click the ‘Tag Configuration’ box. This will bring up a list where you can choose tag type. Select ‘Universal Analytics’.

Now, you’ll need to enter your Google Analytics ID. To find this, go back to your Google Analytics account, go to the ‘Admin’ screen, then ‘Property Settings’. In here, you’ll see your Google Analytics ID. Once you’ve got that, copy and paste this into Google Tag Manager.

To finish configuration, you also need to select ‘Track Type’. For a basic implementation, choose ‘Page View’ from the drop-down menu, though more advanced settings are available to choose from here, too.

Configure a Tag in Google Tag Manager

6. Set a trigger

Click the ‘Triggering’ area which will prompt you to select a trigger. Here, you will see the default trigger ‘All Pages’ which will fire whenever somebody views a page.

As we simply want to track page views, this is the one we want. Select this.

Set a Trigger in Google Tag Manager

7. Click ‘Save’

Now that you’ve created your tag, click ‘Save’. You will now see that your Analytics Base Tag has been implemented.

8. Debug your new tag

Although the risks associated with adding a basic Google Analytics tracking code via Google Tag Manager are minimal, you should still debug your new tag. This is to make sure the tag doesn’t harm your site and is firing correctly.

To make sure your code is working, select ‘Preview’ in the top right. Then we can enter the preview mode to see if your new tag is firing on your website.

You will now see an orange banner saying you’re working in Preview mode. This means that when you load the website in your browser, it will put the tag onto the site as if it’s live but only for you. It does not go live until you click ‘Publish’.

Debug a Tag in Google Tag Manager

When you visit your website, you’ll get an extra Google Tag Manager debugging menu at the bottom of the page. This will tell you when a tag has been fired on this page. You can also see more information by clicking on the tag. If you do this, you can check that the Google Analytics ID you inputted is correct and ensure data from your tags are feeding through correctly (particularly useful for a more complex tag).

Debugging your Tag in Google Tag Manager

9. Push your tag live

If your new tag is firing, go back to Google Tag Manager and press ‘Leave Preview Mode’ under the banner to place the tag live on your website.

To publish your new tag, click ‘Submit’in the top right. This will open a new pop up window where you can publish your changes. Prior to publishing and good practise, it’s worth entering a descriptive name and details of the changes.

After 24 hours, you will now be able to track page views. With Google Tag Manager, you can also track more complex events such as downloads and outbound links. There are different plugins that will help you do this automatically.

Push your Google Tag Manager Tag live

Learn more about Google Analytics

If you want to get the most out of your tracking, you can learn how to unlock its full potential with our analytics and optimisation courses - including Google Analytics training and Google Tag Manager training.

For further information about our private training sessions, call us to discuss your requirements and we’ll work with you to establish the right course for you.
We have trained thousands of people all over the world. Helping them to learn more and achieve more.