How to Add Google Analytics to WordPress: A Beginner’s Guide

Are you looking for a way to improve your WordPress website’s performance? Why don’t you try using Google Analytics? In this article, we’ll uncover the functionality of this tool and all the perks you can expect from using it. To top it off, we’ll also explain how to add Google Analytics to your WordPress website.

How to Add Google Analytics to WordPress Blog – Video Tutorial

Learn how to set up Google Analytics for WordPress and improve your website performance in our step by step video guide.

Subscribe For more educational videos! Hostinger Academy

If you prefer reading, check out the instructions below.

What Is Google Analytics

Google Analytics is one of the most popular web analytics tools on the market. This Google-powered free service can give you valuable data regarding website traffic and audience behavior. Thus, you get to make data-driven decisions to plan the growth of your site far ahead.

If you want to learn more about Google Analytics, sign up for free Analytics Academy Courses. These include beginners, advanced, power users, and more.

Why Is Google Analytics Important

Often, when you start a blog, your main goal is to get more followers and more traffic. Google Analytics is one of the ways that can help you do that.

Here are four reasons why you should consider the option to add Google Analytics to your WordPress site:

  • Gain insights into your audience. See who visits your website and where your audience is located. You can also see what users are doing and how long they stay on your site. By segmenting your audience, you’ll get to develop a business growth plan in a calculated manner.
  • Optimize your marketing strategies. Having insights into your market segments will make it easier for you to create the right marketing campaign decisions in order to achieve optimal results.
  • Maintain quality content. As the tool allows you to see which pages and links receive most of the clicks, you can craft better content to provide to your audience’s needs and preferences.
  • Identify the most active time. Posting time is one of the most important factors that can affect your website’s traffic. Once you know those peak times, you can adjust your posting time accordingly.

Suggested Reading

Discover Google Analytics 4’s AI features and learn how to use them in our tutorial.

How to Sign up for Google Analytics

Before you can connect Google Analytics to WordPress site, you need to sign up first. Not to worry, the process is quite simple and only requires a Google account.

Check out the steps to signing up for Google Analytics below:

  1. If you haven’t logged into your Google account or simply don’t have one yet, navigate to the registration page.Register for a Google account
  2. Now, go to Google Analytics, click on the Set up for free button to start the process.
  3. On the Create Account page, enter your account name, set your Account Data Sharing Settings, and then click Next.
  4. In the following section select how you want to measure your site. Click on the Web option, then proceed to the next page.
  5. Enter your website details (i.e., name, URL, industry category, time zone, etc.) in the third section. Once you’re done, click on the Create button.
  6. Google Analytics will then present its Terms of Service. Tick all the necessary boxes, then click the I Accept button.
  7. That’s it! You have successfully created a Google Analytics account.

How to Get Google Analytics Tracking Code

After you complete the signup process, the service will redirect you to the area where you can obtain the Google Analytics code. However, if you want to retrace the steps, here’s how to get your tracking code manually:

  1. Log into your Google Analytics account, then navigate to the Admin menu on the bottom left.
  2. Click on the Tracking Info menu. Then, click on the Tracking Code submenu.
  3. You should now be able to see your Tracking ID and Global Site Tag. Save both of these codes as we will need them later.
Google Analytics Tracking Code page that will be used to add Google Analytics to your WordPress website manually


Once you’ve obtained the tracking code, you can navigate to your WordPress to begin the configuration steps.

How to Install Google Analytics on WordPress

There are two methods to install Google Analytics onto your WordPress site. The first is by using a plugin and the second is doing it manually. Check it out below.

How to Add Google Analytics With a WordPress Plugin

If you don’t want to mess with your theme, you can use a third-party plugin to do the job for you. Not only is it easier, but it reduces the risk of messing up the code.

Check out this article to learn how to install a WordPress plugin.

Since there are a ton of great Google Analytics plugins to choose from, we took the liberty to create a guide using one of the best plugin options on the market — MonsterInsights.

Supporting over two million users and counting, MonsterInsights is a robust Google Analytics dashboard plugin for all of your marketing needs. The plugin’s biggest advantage lies in its ability to create customized reports based on your choice of parameters.

If you only need the basic features of a web analytics tool, MonsterInsights offers a free Lite version. Should you need more options, you can upgrade to the Plus version at a starting price of $99.50 per year and is a great choice for small businesses.

However, more options are available should you need it. For instance, the Pro version is suitable for online store owners since it offers an advanced eCommerce analysis feature for connecting WooCommerce with Google Analytics.

Here’s how to add Google Analytics with MonsterInsights:

  1. From your WordPress dashboard, navigate to Plugins -> Add New.
  2. Search for Google Analytics Dashboard Plugin for WordPress by MonsterInsights plugin. Download and activate it.
    Downloading the MonsterInsights plugin for WordPress
  3. Go back to your WordPress dashboard. A new Insights option should pop up.
  4. Scroll down a bit and you’ll see Launch the Wizard! button. Click on it. Follow the instructions to complete your initial setup. This is where you’ll need to connect MonsterInsights to your Google Analytics account.
    Connecting MonterInsights with your Google Analytics account
  5. Continue following the instructions and filling in the necessary information. Once everything is done, you’ll see the Finish Setup & Exit Wizard button.
  6. Congratulations, you have completed the integration process! You can go to Insights -> Reports to see your site’s performance.

How to add Google Analytics to WordPress Using Google Tag Manager

Another method you can use to set up Google Analytics on WordPress is Google’s Tag Manager. This is used to increase your agility. Meaning you can update your own tags for conversion tracking, look at site analytics, and more.

Easily add or change your tags as you please, or include third-party tags for web and mobile apps. The best part is, you can update or add more scripts from the Tag Manager user interface instead of in WordPress.

Google Tag Manager for WordPress

After signing up, download the Tag Manager WordPress plugin. On your WordPress site, go to Plugins -> Add New -> search for Google Tag Manager -> Activate.

Once that is complete you will need to enter your Google Tag ID into the plugin. To do this go to your Tag Manager site and click on Admin. Here you will find an ID starting with GTM, copy it for now.

Code for Google tag manager and wordpress

Then on your WordPress site go to Settings -> Google Tag Manager, and paste it into the Google Tag Manager ID box.

options for setting up google tag manager for wordpress

How to add Google Analytics to WordPress Using functions.php Code

There are a couple of ways to add Google Analytics to WordPress using the code snippet.

The first location where you can add the Google Analytics tracking code is the WordPress functions.php file. You create a new function that contains the code snippet.

Pro Tip

For precautionary reasons, it’s best to create a backup copy of the file before you modify it.

  1. On your WordPress dashboard, navigate to the Theme Editor section under the Appearance menu.
  2. Open the Theme Functions(functions.php) file from the right column.
  3. Add the following code snippet to the file. Don’t forget to replace the Tracking ID with your own.
add_action('wp_head','my_analytics', 20);
function my_analytics() {
?>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-26575989-48"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  gtag('config', 'UA-26575989-48');
</script>
<?php
}
  1. Once you’re done, click on the Update File button to save the changes.

Before adding the Google Analytics code to the functions.php or any other theme file, you need to have a child theme installed. Aside from being relatively easier to modify, the child theme preserves all the changes you’ve made even when you update the parent theme.

You can check out this article to learn more about WordPress child theme creation.

How to add Google Analytics to WordPress With header.php Code

You can also add the Google Analytics tracking code to the Theme Header (header.php) file.

  1. Navigate to the Theme Editor once again.
  2. Open the Theme Header (header.php) file on the right column.
    Editing the header.php file using the WordPress Theme Editor
  3. Paste the Google Analytics code snippet in the header area or, more specifically, above the </head> tag.
  4. Once you’re done, click on the Update File button to save the changes.

Conclusion

Google Analytics is a robust free web analytics tool that collects crucial information regarding your web performance and target audience. By gaining web analytics insights, you get to make data-driven decisions to grow your WordPress website in the most efficient way.

There are two ways for setting up Google Analytics on WordPress:

  • Using a WordPress Google Analytics plugin — we highly recommend using MonsterInsights for this job.
  • Adding it manually — adding a code snippet along with the tracking code to functions.php or header.php file.

We hope you found this guide useful on how to add Google Analytics to WordPress. If you have any more questions, feel free to leave a comment down below.

Author
The author

Domantas G.

Domantas leads the content and SEO teams forward with fresh ideas and out of the box approaches. Armed with extensive SEO and marketing knowledge, he aims to spread the word of Hostinger to every corner of the world. During his free time, Domantas likes to hone his web development skills and travel to exotic places.