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 finds your website, what your audience is doing when they’re there, and where these people are dropping off. These insights allow you to create a solid, more functional website that closely caters to your customer journey, providing a seamless user experience every step of the way. 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 have a Google Analytics account, find out how to get started in Google’s help centre.

Once you have an account, it’s time to learn how to add Google Analytics to your WordPress website. You can do this easily with code, plugins, or Google Tag. We’ll explain these 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 do some research to find the best one for you before installing.

Best Google Analytics plugins for WordPress

  • Google Analytics by MonsterInsights: This is the most popular, with over three 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 data on your WordPress analytics dashboard. Overall, this plugin works very similarly to the Google Analytics platform.
  • Analytify: You don’t need to manually copy any code for this plugin. Instead, it features a one-click authentication process that automatically adds Google Analytics tracking code to your website.
  • Google Analytics WD by Web-Dorado: This plugin boasts a beautiful user interface and can quickly add Google Analytics to your WordPress site, displaying key analytics reporting features in your dashboard.
  • WP Statistics: This plugin is easy to use and lightweight. You can monitor traffic for different search engines, understand your users’ location, and track email statistics. All of this can be done directly from your WordPress dashboard, and it’s just as easy to export in your chosen file format.

These plugins are some of the best Google Analytics plugins for WordPress. They are free to install, although 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 can all be installed in a similar way. Here’s how to do it:

  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 all instructions (but remember to seek out a guide specific to your plugin if you get stuck)
Google Analytics plugins available on Wordpress

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

After you’ve followed 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 adding the Google Analytics code as a function to the functions.php file in your WordPress theme. We’ll guide you through this process below.

1. Create a child theme

When adding Google Analytics to your WordPress site, it’s recommended that you use a child theme. If you’re unsure 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 WordPress, we need to add a function within your child theme’s functions.php file. This function will insert the Global Site Tag before the closing </head> tag on your site. You’ll need to 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>
<script>
	window.dataLayer = window.dataLayer || [];
	function gtag(){dataLayer.push(arguments);}
	gtag('js', new Date());
	gtag('config', '[YOURTRACKINGCODE]');
</script> 
<?php 
} 

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

3. Add your Tracking ID

We're using [YOURTRACKINGCODE] as a placeholder in the above function. Make sure to replace this 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 appears in WordPress

You can view the source code of your site to check that the code snippet has been added.

To see your website’s source code, visit your website and press CTRL+U if you are a PC user or Command+Option+U if you use a Mac. This will open a new tab where you can view your source code. Next, search for your Tracking ID by pressing CTRL+F or Command+F if you’re a Mac user 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. This allows you to easily manage tracking tags without editing code, even though set up requires advanced experience. We’ll break down the steps involved in installing Google Analytics via Google Tag Manager on WordPress below.

1. Set up Google Tag Manager

Visit Google Marketing Platform to set up a Google Tag Manager (GTM) 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 don’t have a child theme, we recommend following this guide on creating a child theme.

3. Create two functions

To add the Google Tag Manager script to your site, we need to create two 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 -->
	<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
	new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
	j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
	'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
	})(window,document,'script','dataLayer','[YOURTRACKINGCODE]');</script>
	<!-- End Google Tag Manager -->
<?php 
} 
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) -->
<?php
}
add_action('wp_body_open', 'ns_google_tag_manager_body', 1);

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

You can find your unique GTM ID in several places within your Google Tag Manager dashboard. Once logged in, it’s displayed on the top right of the screen and looks similar to this example GTM-NMDR3YU.

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

Copy and paste your ID into both [YOURTRACKINGCODE] references, 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 Tags in the sidebar, then select New.

This will open a pop-up window. Here, we recommend naming your tag so it’s easy to find. We suggest ‘Analytics Base Tag’.

Create a new Tag in Tag Manager

5. Configure this tag

Click the Tag Configuration box. This will display 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 to your Google Analytics account, navigate to the Admin screen, then Property Settings. Next, copy and paste your Google Analytics ID into Google Tag Manager.

To finish the configuration, you also need to select Track Type. Choose Page View from the drop-down menu for a basic implementation. There are also advanced settings available to choose from.

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 want to track page views, select this option.

Set a Trigger in Google Tag Manager

7. Click ‘Save’

After you’ve created your tag, click Save. You’ll see that your Analytics Base Tag is now active.

8. Debug your new tag

Although the risks of an issue being caused by using Google Tag Manager in this way are low, you should still debug your new tag. This is to make sure the tag doesn’t cause any damage to your site and that it fires correctly.

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

You'll see an orange banner indicating that you're in preview mode. When you visit the website, the tag will be added to the site as if it were live, but just for you. It won't 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 fires 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 make sure the 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 working, return to Google Tag Manager and click the Leave Preview Mode button under the banner to enable the tag 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. Remember, it’s good practice to enter a descriptive name and details of the changes.

You’ll be able to monitor page views after 24 hours. With Google Tag Manager, you can also track more complex events such as downloads and outbound links. There are plugins available to help you do this automatically.

Push your Google Tag Manager Tag live
 

Learn more about Google Analytics

Feeling inspired to learn more about Google Analytics? Keep up to date with the latest version and find out how to use helpful features such as channels and cross-device remarketing.

GET IN TOUCH

We have trained thousands of people all over the world. Helping them to learn more and achieve more.
GoogleBrand
AirBanbBrand
AdidasBrand
SonyBrand
LorealBrand