{"id":684,"date":"2019-05-14T08:18:23","date_gmt":"2019-05-14T08:18:23","guid":{"rendered":"http:\/\/www.analyticsbook.org\/?page_id=684"},"modified":"2020-09-22T05:00:33","modified_gmt":"2020-09-22T05:00:33","slug":"google-analytics-tracking-codes","status":"publish","type":"page","link":"https:\/\/www.analyticsbook.org\/google-analytics-tracking-codes\/","title":{"rendered":"Google Analytics Tracking Codes"},"content":{"rendered":"
\t\t\t\t\t\t\t
\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tGordon Choi
\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tAuthor<\/p>\n
<\/p>\n
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.<\/p>\n
First you will need a new Google Analytics account.<\/p>\n
But where or how to get your Google Analytics tracking code?<\/p>\n
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:<\/p>\n
http:\/\/www.google.com\/analytics\/<\/pre>\nAnd your Google Analytics account will be created.<\/p>\n
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.<\/p>\n
This is the tracking code that you must add to all the pages of your website.<\/p>\n
Account Structure<\/h2>\n
Google Analytics has a three-level structure:<\/p>\n
Under an account, you can have multiple properties.<\/p>\n
In your Google Analytics account, each website should be set up under a new property.<\/p>\n
Assume you have:<\/p>\n
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:<\/p>\n
At this point, each property (or website) will get a unique Google Analytics property ID (e.g. UA-XXXXXXXX-2 and UA-XXXXXXXX-2).<\/p>\n
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.<\/p>\n
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.<\/p>\n
Let’s look at a visual example of all the views and properties under your Google Analytics account:<\/p>\n
<\/p>\n
The unique Google Analytics IDs are on the property level. In this case, they are UA-XXXXXXXX-1 and UA-XXXXXXXX-2.<\/p>\n
You can assign one of the four access rights type to Google Account users in order for them to access Google Analytics reports.<\/p>\n
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\u2019 access rights, then assign him\/her with Manage Users.<\/p>\n
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.<\/p>\n
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.<\/p>\n
To get your Google Analytics tracking code, log on to your Google Analytics account, select your website under Property, and go to:<\/p>\n
Admin -> Tracking Info -> Tracking Code<\/pre>\nBy 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.<\/p>\n
\n<\/pre>\nLet’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\u2019s servers and inserts the library into your web page.<\/p>\n
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){\n(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),\nm=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)\n})(window,document,'script','\/\/www.google-analytics.com\/analytics.js','ga');<\/pre>\nThe analytics.js JavaScript library uses first-party cookies.<\/p>\n
A cookie is a small text file that is stored in a user’s web browser and has:<\/p>\n
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.<\/p>\n
The analytics.js file sets two cookies.<\/p>\n