WordPress Tutorial

How to Add Google Analytics to WordPress

If you want to run a successful website, it is essential to know your audience and what they expect from your site. The best way to achieve this is by analyzing your traffic. That’s exactly what Google Analytics does. This free software from Google allows you to track your website’s visitors, analyze their habits, traffic sources, social metrics and a lot more different statistics. By analyzing statistics, you can plan and create better content for your readers. This tutorial will show you how to sign up for and add Google Analytics to your WordPress website.

Why Do You Need Google Analytics in Your WordPress?

Things are pretty easy when you know what your users want from you, your website and your services, aren’t they? The only way to make your visitors stick to your website time and again is by providing them what they need. This is exactly what Google Analytics does for free! Here are a few reasons why we would suggest you to add Google Analytics to WordPress and feel that every WordPress blog must have it:

  1. Helps you in determining your users’ behavior.
    Yes, you can virtually keep track of your visitors’ behavior with Google Analytics. You can easily check the most viewed pages, the average time each user spent on a certain page, the top performing pages, as well as detect the page that causes your visitors to exit your website and a lot more. It goes without saying that having a quick look at such information would help you in better optimizing your website. If a certain blog post is driving the most views, you could write more blog posts on related topics and attract more traffic.
  2. Helps you in analyzing the most active time.
    You can easily determine the hour at which you get the most traffic. If most of the users are active at night, it is obvious that in order to catch more views, you should schedule your posts to go live at the late hours.
  3. Helps you in exploring the crucial details.
    Google Analytics can provide the tiniest details – from the geographical location of any user to the language they speak, the browser they use, the screen resolution and the likes. Thus, it helps you understand your audiences better.

Although there are quite a few other web analytics services you can use, Google Analytics provides a range of exclusive features. Let’s have a quick look at each of them:

  1. Generate Custom Reports easily.
    The best way to analyze your users’ behavior is by making use of facts and figures. This could be easily done by generating custom reports with an easy to understand interface. You can add various parameters and metrics in order to generate an insightful report.
  2. Set up Custom Alerts.
    This is a truly awesome part of Google Analytics with which you can send a custom alert in the form of a text message or an email each time there is a sudden dip in traffic or a drastic change in the bounce rate. Thus, you no longer need to keep your eyes glued to your dashboard to catch important updates.
  3. Integrate Search Console.
    Keeping a sharp eye on SEO related stats and figures is important. Search Console is one such tool which would provide you the information related to search engine optimization. Integrating Search Console with Google Analytics would make the information easily accessible under one roof for quick analysis.

These are just a few of many Google Analytics features which make the entire process of handling a WordPress website easy-breezy!

How Does Google Analytics Work?

You may already think that Google Analytics is amazing. But how does it do it all? Well, we understand that having a basic knowledge of how the software works helps in making the most out of it. Speaking broadly, Google Analytics consists of:

  1. JavaScript code
    When a user lands on your website, JavaScript code is executed to collect all important information about the website itself, the information about the visitor like the browser used to find your website, the operating system, the screen resolution and so on.
  2. Data collection service
    Once all this information is collected by the JavaScript code, it is sent to Google servers for processing in the form of small packages called hits. These hits are sent every time a visitor opens your website.
  3. Data processing
    During data processing, Google Analytics servers turn raw data into useful information. For example, categorize your visitors based on their location, language, screen resolution, device type and so on.

This is how it works. It might seem like an easy process at first, but a whole lot of functionalities and processing is going on behind the scene.

We hope this surely strengthened the decision to add Google Analytics to your WordPress website. Without beating around the bush any further, here is a quick tutorial which will show you how to sign up for and add Google Analytics to your WordPress website.

What you’ll need

Before you begin this guide, you’ll need the following:

NOTE: if you don’t have WordPress account just yet, check out our hosting packages. It’s a fast and secure solution for the lowest price.

Step 1 – Creating a Google Analytics Account

The first thing you need to do is create a Google Analytics account and obtain the tracking code. The steps below show how to sign up for Google Analytics:

  1. Start by navigating to Google Analytics home page. Press the Sign In button, which is located at the top of the page and select Google Analytics.
  2. You will be redirected to the Login page. Enter your Google email address and press the Next button. If you don’t have a Google account yet, create one by pressing the Create Account button.
    Google Analytics Login Form
  3. On the next page, enter your password and press the Sign In button.
    Google Analytics Sing In Button
  4. Press the Sign Up button in order to proceed.
    Google Analytics Sing Up
  5. In order to create a new account, you will have to enter the following information:
    1. Account Name.
    2. Website Name.
    3. Website URL.
    4. Industry Category.
    5. Reporting Time Zone.

Google Analytics Create New Account

Fill all the required details and press the Get Tracking ID button (6). On the next page, you will be asked to accept the Terms of Service, so click the I Accept button.

That’s it! You have successfully created a Google Analytics account. On the next page, you will see the Google Analytics Universal tracking code:

This JavaScript code is identical for all websites. Only the tracking ID (UA-00000000-1) is unique. You should see yours on the same page. Tracking ID starts with UA which stands for Universal Analytics, the first set of numbers represent the account number (00000000) and the last number(s) represent Google Analytics property ID (1).

Step 2 – Adding Google Analytics to a WordPress Blog

There is more than one way to add Google Analytics tracking code to WordPress: by using a Plugin, by adding tracking script directly to the header.php file or by creating a new action in functions.php file.

Option 1 – Using a Plugin

If you are not familiar with WordPress file structure and PHP, it’s recommended to use the Plugin method. One of the best plugins for this is Analytics Code Integration. This plugin will insert the Universal Analytics tracking code automatically – you will only have to enter your tracking ID.

The steps below show how to install the plugin and insert the Google Analytics tracking ID:

  1. Log in to WordPress Dashboard and install the Google Analytics plugin. Follow this tutorial if you are not sure about how to install new plugins on WordPress.
  2. Once the installation is complete, navigate to the Settings section and press on Google Analytics.
  3. Scroll down to Plugin Settings and insert your tracking ID.Add Google Analytics to WordPress
  4. Press Save Settings and that’s it, the tracking code has been successfully added to your WordPress blog.

Of course there are many other WordPress plugins you can use to. All of them serves the same purpose and can further enhance your experience.

Be advised that it can take 12-24 hours for Google Analytics to update and start showing first results. Afterward, statistics about your audience will be provided in the Google Analytics Reporting tab.

Google Analytics Reporting Tab

Option 2 – Inserting Code to header.php

As we said, there are a few ways to insert the Google Analytics code to a WordPress blog. For example, the code can be included in your theme’s header.php file:

  1. Access the WordPress administrator area and navigate to Appearance -> Editor.
  2. Open header.php for editing.
  3. Insert the Analytics tracking code you acquired in Step 1 before the closing</head> tag and press the Update File button.
    Google Analytics Code Inserted in header.php

Note: Analytics code will be inserted only to your active theme. You will have to add it again if you change the theme.

Option 3 – Creating a New Function in functions.php File

This method requires some basic coding knowledge and we would highly recommend you to follow it only when you are confident in what you’re doing. Moreover, it’s strongly recommended to create a backup of functions.php file or even your whole WordPress installation before proceeding:

  1. While in WordPress dashboard, navigate to Appearance – > Editor section.
  2. Select functions.php from the list of files on the right.
  3. The following code snippet will create a new function and insert the Analytics tracking code just before the closing </head> tag in your WordPress site. Paste this code at the bottom of the functions.php file, but be sure to change your tracking ID.
  4. Save the changes you’ve made by clicking the Update File button.
    Google Analytics Added Code in functions.php

Note: The code will be inserted to your active theme only. If you change your theme, you will have to re-add the code again.


Needless to say, Google Analytics is essential for your WordPress website in order to understand your audiences better. By finishing this tutorial, you have learned how to create a Google Analytics account and three different ways to add Google Analytics to your WordPress blog.

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 Tutorial
WooCommerce Tutorial: How to Set Up Ecommerce on WordPress