Adding Google Analytics to a website

Adding Google Analytics to a website

Updated: 06/06/2019.

How to create a Google Analytics account for your website and how to set it for tracking website statistics using the Google’s Analytics tools. Example will be made for a new WordPress website, but the principle is the same for any other. I’ll provide step by step instructions with pictures. A separate post explains how to set up another useful tool: Google Search Console.

Contents:

  1. What is Google Analytics for?
  2. Creating a Google analytics account
  3. Connecting Google Analytics with your website
  4. Testing if Google Analytics “works”


1. What is Google Analytics for?

If you are interested in knowing the average number of website visitors (per day, month, for the past six months…), where the visitors are coming from, average page load time, which pages are most visited and many other stats, Google Analytics is a very good free tool that for it.

In this post I won’t explain how to see and read those stats (there are many tutorials on the Internet and the user interface is quite easy and intuitive), but I’ll explain how to create an account and “connect” it with your website.


2. Creating a Google analytics account

Create a Google (gmail) account if you don’t have one, or want a separate one to be used with the website.

Go to analytics.google.com and log in using the Google (gmail) account of your choosing.

There you will get an option to sign up with Google analytics.

Sign up with Google Analytics Picture 1
Sign up with Google Analytics
Picture 1

After clicking the “Sign up” button, you’ll be taken to a page to fill in information for the account and your website.

Fill in the relevant fields (and choosing dropdown menu options) to create an account Picture 2
Fill in the relevant fields (and choosing dropdown menu options) to create an account.
I think the picture is self-explanatory. If you have any questions, use comment field below this post.
Picture 2

All that’s left is the “classic” “I Agree”.

After filling all the fields, click "Get Tracking ID" and choose "I Accept" in the window that pops up Picture 3
After filling all the fields, click “Get Tracking ID” and choose “I Accept” in the window that pops up
Picture 3

This concludes the account setup and you’ll be automatically redirected to Google Analytics control panel.


3. Connecting Google Analytics with your website

Now we need to get the statistics “tracking code” to be placed on the website. One analytics account allows setting up multiple websites (called “properties”).

Click on the sprocket icon to open the Admin menu (1) Choose the account name (2) Choose the "property" (your website) (3) Click on "Tracking Code" (4) Picture 4
Click on the sprocket icon to open the Admin menu (1)
Choose the account name (2)
Choose the “property” (your website) (3)
Click on “Tracking Code” (4)
Picture 4

Afer this you’ll get the code that should be added to the website.

Tracking ID and tracking code Picture 5
Tracking ID and tracking code
Picture 5

Now this code needs to be added to the website. With all the websites, generally, the code from picture 5 can be copied to the <HEAD> section of every page you want to track. For WordPress websites, this can be done in several ways:

  • Using a plugin, like Google Analytics Dashboard for WP for example.
  • Creating a child theme and adding the code to the header.php, or functions.php of the child theme.

The code can be copied as it is in Google Analytics if adding it to header.php. Just paste it below the line containing “wp_head()”. If using functions.php, then a slight alteration is needed:

// BEGIN Google Analytics

function ns_google_analytics() { ?>
   <script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXXX-X"></script>
   <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());

      gtag('config', 'UA-XXXXXXXXX-X');
   </script>
   <?php
}

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

// END Google Analytics


4. Testing if Google Analytics “works”

Now it is time to test if it all works. Open a page on your website (disable ad-blocker if you are using one).

In a separate browser tab open Google Analytics control panel and see if “Active Users right now” shows the website pages you are browsing. If yes – it’s working! 🙂

Testing if Google Analytics works. In this test I had opened the website home page - (1) pointing the root " / ". Picture 6
Testing if Google Analytics works.
In this test I had opened the website home page – (1) pointing the root ” / “.
Picture 6

In case it is not working:

  • Check if browser’s ad-blocker has been turned off when viewing website pages.
  • Have you correctly copied the Tracking ID (picture 5)?
  • Has the implementation code been copied correctly (brackets, semicolons etc.)?

Share...

Leave a Comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.