Google Analytics Tracking Codes

How To Add Google Analytics Tracking Code (GATC) To Any Website?

Gordon Choi
Gordon Choi
Author


JavaScript

To track websites’ data, Google Analytics uses a JavaScript based tracking code. This tracking code must be inserted onto each web page that requires data tracking.

Create a New Google Analytics Account

First you will need a new Google Analytics account.

But where or how to get your Google Analytics tracking code?

When you register a new email address on Gmail, you are automatically assigned with a new login to your Google Account. With this login, sign in to Google Analytics through:

http://www.google.com/analytics/

And your Google Analytics account will be created.

Once you’ve successfully created your new Google Analytics account, fill in the details of your website (including website’s URL, etc). Then in the setup process, you’ll be taken to a page that you can simply copy your standard Google Analytics tracking code.

This is the tracking code that you must add to all the pages of your website.

Account Structure

Google Analytics has a three-level structure:

  • Accounts
  • Properties
  • Views

Under an account, you can have multiple properties.

Properties

In your Google Analytics account, each website should be set up under a new property.

Assume you have:

  • www.example.com which is your website for desktop users (or desktop website).
  • m.example.com which is your website for mobile users (or mobile website).

You have three options to set up your websites with Google Analytics. The most straightforward and easiest is to create two new properties and set up:

  • www.example.com to the first new property.
  • m.example.com to the second new property.

At this point, each property (or website) will get a unique Google Analytics property ID (e.g. UA-XXXXXXXX-2 and UA-XXXXXXXX-2).

Views

A property can contain multiple views. Once you have created your first new property, you should already have a new view under your property. By default, this first new view contains all the data for the property.

You can create multiple views in which the second view can contain only a subset of data of this property by applying data filters. For example, your second view can be restricted to only contain users who visited your website from mobile devices.

Account, Property & View

Let’s look at a visual example of all the views and properties under your Google Analytics account:

Tracking Code Setup

The unique Google Analytics IDs are on the property level. In this case, they are UA-XXXXXXXX-1 and UA-XXXXXXXX-2.

Access Rights

You can assign one of the four access rights type to Google Account users in order for them to access Google Analytics reports.

  • Read & Analyze: The lowest level of access in which a user is able to view reports, and create custom reports, segments, or alerts.
  • Collaborate: A user is allowed to share customization and do whatever the “Read & Analyze” can do.
  • Edit.
  • Manage Users.

If you only need a user to read the reports, then assign him with Read & Analyze. If you need a user to edit the Admin section, then assign him/her with the Edit access. If you need a user to add or delete other users’ access rights, then assign him/her with Manage Users.

For each Google Analytics user, you can grant access at either the property level or the view level. For example, if a user is given Edit access right to property m.example.com (UA-XXXXXXXX-2), then he / she already has Edit access right to View 2, View 2a and View 2b.

Google Analytics Tracking Code Setup

You are required to get the JavaScript based tracking code (Google Analytics tracking code) from your Google Analytics property, and place this tracking code on to every pages of your website.

To get your Google Analytics tracking code, log on to your Google Analytics account, select your website under Property, and go to:

Admin -> Tracking Info -> Tracking Code

By default, your Google Analytics tracking code should look something like this. It is the latest version of Google Analytics tracking code and is called the Universal Analytics version. This default Universal Analytics tracking code logs a page view each time a page loads.


Let’s look at only the first part of the tracking code. The main objective of the first part of this code is to fetch file analytics.js which contains the Google Analytics tracking library from Google’s servers and inserts the library into your web page.

(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

The analytics.js JavaScript library uses first-party cookies.

A cookie is a small text file that is stored in a user’s web browser and has:

  • A name-value pair containing the actual data.
  • An expiry date after which the cookie is no longer valid.
  • The domain and path of the server it should be sent to.

While on your website, a user navigates from page to page. Google Analytics uses cookies from the analytics.js JavaScript library to remember what the user has done from page to page.

The analytics.js file sets two cookies.

Cookie Name Expiration Time Objective
_ga 2 years Distinguish users
_gat 10 minutes Throttle request rate

The second part of the tracking code has two lines of commands which are the backbones of the Google Analytics tracking code. When a web pages (with Google Analytics tracking code installed) loads,

  • The first line initiates a Google Analytics tracker that sends data to your property i.e. in this case the property ID is initiated in the format UA-XXXXXXXX-Y with the scope of Google Analytics cookies of your website defined.
  • The second line sends a page view to Google Analytics.
ga('create', 'UA-XXXXXXXX-Y', 'auto');
ga('send', 'pageview');

Note for the tracking code to properly work, the first command line (‘create’) must always be placed before the (‘send’) command line and any other additional command line. The simple reason is a tracker must always be initiated before anything else can happen.

The third parameter ‘auto’ in the first command line defines the scope of cookies. You are allowed to set other values to the third parameter for specific requirements.

Examples:

  • If it is set to ‘auto’ and the tracking code is placed on all pages of domain example.com, then the cookies will cover both www.example.com and m.example.com.
  • If it is left blank and the tracking code is placed on all pages of domain example.com, then the cookies will cover both www.example.com and m.example.com.
  • If it is set to ‘example.com’ and the tracking code is placed on all pages of domain example.com, then the cookies will cover both www.example.com and m.example.com.
  • If it is set to ‘www.example.com’ and the tracking code is placed on all pages of domain example.com, then the cookies will cover only www.example.com.
  • If it is set to ‘m.example.com’ and the tracking code is placed on all pages of domain example.com, then the cookies will cover only m.example.com.

Where to Place the Tracking Code?

Google Analytics tracking code is asynchronous in which the browser does not have to wait for the entire code to completely load to continue rendering the rest of the web page.

  • Google recommends placing the tracking code in thesection of your web pages in order to take the asynchronous advantage.
  • It is okay to place the tracking code anywhere in thesection of your web pages.
  • If your website was created by using one of the many open-source or paid content management systems (CMS), you will need to have edit access to your CMS in order to add / edit the Google Analytics tracking code.
  • If your website was coded from scratch, then it may be more complicated and your web developer will have to add / edit the Google Analytics tracking code for you.

How to Setup the Tracking Code?

Assume you actually have two websites: One for desktop and tablet users (www.example.com) and the other for mobile users (m.example.com) in which both sites are under top level domain example.com. Consider the different setup options.

  • The single property ID setup
  • The two properties ID setup
  • The advanced setup

The Single Property ID Setup

The setup:

Tracking Code Setup

  • Create one Google Analytics property with one property ID (e.g. UA-XXXXXXXX-1).
  • Place the tracking code with property ID UA-XXXXXXXX-1 on to all pages of your websites (including both www.example.com and m.example.com).

The pros:

  • It is very simple to manage one website under one property in which maintaining the correct tracking code with the correct property ID is straight forward.
  • In your reports, you already have the counts of total sessions and total users per the two websites aggregated.

The cons:

  • One property has to store two websites’ data. If data size grows quickly, the property can quickly run into data sampling.
  • Both sites’ data are mixed in all the reports, and it will be very difficult to split the data when you require analyzing the data in details.

The Two Property IDs Setup

The setup:

Tracking Code Setup

  • Create your two websites where each is under a unique property IDs. For example, one ID for www.example.com (UA-XXXXXXXX-1) and another one ID for m.example.com (UA-XXXXXXXX-2).
  • Place the tracking code of UA-XXXXXXXX-1 on to all pages of www.example.com and the tracking code of UA-XXXXXXXX-2 on to all pages of m.example.com.

The pros:

  • The two property setup makes it much easier when separately analyzing each of the website’s data.
  • By using two property IDs, you have less risk to run into data sampling.

The cons:

  • You will have to maintain two separate tracking codes with two different property IDs on your two websites.
  • You cannot easily work out the counts of total sessions and total users per single website.

The Advanced Setup

The setup:

Tracking Code Setup

  • Create one Google Analytics property with one property ID (e.g. UA-XXXXXXXX-1).
  • Create two more properties each for one website. You should have one ID for www.example.com (UA-XXXXXXXX-2) and another one ID for m.example.com (UA-XXXXXXXX-3).
  • Place the tracking code with property ID UA-XXXXXXXX-1 on to all pages of your websites (including both www.example.com and m.example.com).
  • Place the tracking code of UA-XXXXXXXX-2 on to all pages of www.example.com and the tracking code of UA-XXXXXXXX-3 on to all pages of m.example.com.
  • Then make the following changes to your tracking codes.

On all the pages of www.example.com:

ga('create', 'UA-XXXXXXXX-1', 'auto');
ga('send', 'pageview');
ga('create', 'UA-XXXXXXXX-2', 'auto', 'wwwexample');
ga('wwwexample.send', 'pageview');

On all the pages of m.example.com:

ga('create', 'UA-XXXXXXXX-1', 'auto');
ga('send', 'pageview');
ga('create', 'UA-XXXXXXXX-3', 'auto', 'mexample');
ga('mexample.send', 'pageview');

The additional lines simply mean:

  • Initiate an additional Google Analytics tracker for wwwexample or mexample. You can use tracker names other than wwwexample or mexample.
  • Send a page view to tracker wwwexample or mexample.

The pros:

  • The setup enables very simple data aggregation and analysis. The count of total sessions and users per of the largest property with both websites (UA-XXXXXXXX-1) have already been calculated for you. The same metrics for each of the properties (UA-XXXXXXXX-2 and UA-XXXXXXXX-2) as separate sets of data have also been calculated for you.
  • Even when your largest property (UA-XXXXXXXX-1) runs into data sampling, the other smaller properties (UA-XXXXXXXX-2 and UA-XXXXXXXX-3) will still have no problem with data sampling.

The cons:

  • It is complicated to maintain the tracking codes, with two different property IDs per website and three unique property IDs on two websites.
  • The largest property (UA-XXXXXXXX-1) contains data of both the websites, and it is the property with the highest risk of running into data sampling.

The Legacy Tracking Codes

Prior to the asynchronous tracking codes, Google Analytics tracking code used to be synchronous, and this is the synchronous tracking code.


There were used to be older versions of Google Analytics tracking codes. This one was the first version of asynchronous tracking code:


Both the older versions of tracking codes do the same thing as the latest version: Initiate the Google Analytics tracker, and send a page view to Google Analytics.

If you are still using one of the older versions of Google Analytics tracking codes, you should upgrade to the latest version (i.e. Universal Analytics) which is also the second version of asynchronous tracking code.

The Universal Analytics tracking code covers whatever the older codes do (i.e. initiates a tracker and sends a page view). On top of that:

  • The Universal Analytics tracking code uses a single and unique cookie and is able to assign a visitor ID to the users’ browsers.
  • Universal Analytics has the ability to track users across multiple devices, such as using a user login which is a unique identifier on all devices that allows all the activities of a user to be tied together.

Previous Chapters

Next Chapters



Content on Gordon Choi’s Analytics Book is licensed under the CC Attribution-Noncommercial 4.0 International license.

Gordon Choi’s Other Books:
The China Mobile SEO Book
Mobile Website Book

[elementor-template id=”764″]

Copyright 2016-2020 www.AnalyticsBook.org