WordPress

How to Add Google Analytics to WordPress

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 WordPress site.

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.

Here are four reasons why you should consider adding Google Analytics to your WordPress site:

  • Gain insights into your audience. 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 for optimal results.
  • Maintain quality content. As the tool allows you to see which pages and links get the most visitors, you can craft better content to cater to your audience’s needs and preferences.
  • Identify the most active time. Posting time is one of the factors that can affect a website’s traffic. Once you know the site’s peak times, you can adjust your posting time accordingly to get more traffic.

Signing up for Google Analytics

Before you get to add Google Analytics to WordPress site, you need to sign up for it first. Not to worry, the process is quite simple and only requires a Gmail account.

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

  1. Navigate to the Google Analytics Sign Up page. If you haven’t logged into your Gmail account, it will redirect you to a Login page. You can either proceed with an existing account or create a new one.
  2. Click on the Signup button to start the process.
  3. On the Create Account page, three sections require information. Enter your account name on the first section, then click Next. The second section determines how you want to measure your site. Click on the Web option, then proceed to the next section. Enter your website details (name, URL, industry category, and time zone) in the third section. Once you’re done, click on the Create button.
  4. Google Analytics will then present its Terms of Service. Tick all the boxes to accept them, then click the I Accept button.
  5. That’s it! You have successfully created a Google Analytics account.

Adding Google Analytics Through Plugins

This image shows MonsterInsights' logo banner. It's a great Google Analytics plugin that you can use for free.

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

Check out this article to know the steps to install a WordPress plugin.

As there are a ton of great Google Analytics plugins to choose from, we took the liberty of creating the tutorial using one of the best ones on the market — MonsterInsights.

Supporting over two million users and counting, MonsterInsights is a robust Google Analytics plugin worth considering. 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’ free Lite version offers great choices. Should you need more options, you can upgrade to the Pro version at a starting price of $99.50 per year.

Here’s how to add Google Analytics with MonsterInsights:

  1. From your WordPress dashboard, navigate to Insights -> Settings section.
  2. Click on the Connect MonsterInsights button on the Google Authentication section.
  3. Log into your Gmail account, then click Allow to grant MonsterInsights access to your Google Analytics data.
  4. Choose the website profile you want to connect with MonsterInsights, then click Complete Connection.
  5. Congratulations, you have completed the integration process! You can go to Insights -> Reports to see your site’s performance.

Adding Google Analytics to WordPress Manually

When you have signed up for the service, you need to get a code snippet and place in on your website.

In order to add the analytics code to functions.php file, you need to have a child theme installed. How so? 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 it.

First step: Getting the Google Analytics Tracking Code

After you completed the signup process, the service will redirect you to the area where you can obtain the code.

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. On the Admin section, 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 tracking code. Copy both codes as you will need them later.
    Google Analytics Tracking Code page where the user can access their tracking ID and tracking code. The tracking code will be used to add Google Analytics to WordPress site.Once you’ve obtained the tracking code, you can navigate to your WordPress to begin the configuration steps.

Option 1: Functions.php Code

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

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

Note: 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 functions.php file on the right column.
    WordPress Theme Editor page. The functions.php file can be found on 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-00000000-1'"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());
      gtag('config', 'UA-00000000-1');
    </script>
    <?php
    }
    
  4. Once you’re done, click on the Update File button to save the changes.

Option 2: header.php Code

You can also add the tracking code to the header.php file.

  1. On your WordPress dashboard, navigate to the Theme Editor section under the Appearance menu.
  2. Open the header.php file on the right column.
    WordPress Theme Editor page. The header.php file can be found on the right column.
  3. Paste the 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 the site in the most efficient way.

There are two ways to add Google Analytics to a WordPress site:

  • Using a plugin — we highly recommend using MonsterInsights to do the job.
  • Manually — adding a code snippet along with the tracking code to functions.php or header.php file.

Knowing the vast benefits of using Google Analytics for website growth in general, we can’t wait to see you try and prove it yourself. If not now, when?

About 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.

1 Comment

Click here to post a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  • Thank you for this post, it helped me choose the best method to add Google Analytics to my website. Option 1 – Using a Plugin seems most ideal, but I am curious about the disadvantages? They contradict the advantages for theme changes and updates.

Get Exclusive Content

Join thousands of webmasters who get our free newsletter with tips on how to drive more traffic and revenue to their websites!

Please wait...

Thank you for sign up!

More in WordPress
WooCommerce Tutorial: How to Set Up Ecommerce on WordPress

Close