This document describes how to set up Analytics tracking for your website using the traditional ga.js
tracking
code snippet. If you are setting up tracking for the first time, we
recommend that you use the default tracking code snippet, described in Tracking
Sites.
There are two basic ways to set up tracking on your web property: standard setup and customized setup. This document covers those ways, along with related topics:
Once you correctly install the ga.js
tracking code on your site, you begin receiving report data.
Report data shows up in your reports within 24 hours of setting up the tracking code.
When you first begin implementing tracking in Google Analytics website, you need to install the tracking code on your website pages. The generic tracking code snippet consists of two parts: a script tag that references the ga.js tracking code, and another script that executes the tracking code.
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try{
var pageTracker = _gat._getTracker("UA-xxxxxx-x");
pageTracker._trackPageview();
} catch(err) {}</script>
This tracking code snippet should be included in your site's pages so
that it appears at the bottom of the page's HTML (or generated-HTML)
structure, before the closing <body>
tag. For more information, see the Functional Overview, which explains the logic behind the tracking code execution.
Note: If you are setting up tracking for a web property that spans multiple domains or multiple sub-directories, you will need to modify the tracking code in order to get accurate reporting data for your profile. Additionally, if you are setting up a profile specifically for a sub-directory of a larger domain, you should also modify the tracking code. See the Domains & Directories Guide for more information.
The first part of the script tag (represented by lines 1 - 4 of the code above), uses Javascript to dynamically determine whether the HTTP protocol for the requested page is either secure or standard. It then uses the appropriate protocol to reference the tracking code. So, if one page on your site is delivered over a standard HTTP protocol, the resultant string is:
<script src='http://www.google-analytics.com/ga.js' type='text/javascript'>
If another page is delivered over a secure connection, the resultant string is:
<script src='https://ssl.google-analytics.com/ga.js' type='text/javascript'>
If you have a mix of secure and non-secure pages, leave the initial script tag as indicated so that the appropriate connection can be determined. If all pages on your site are delivered over standard HTTP, you can replace the first tag with the simpler call to the Google Analytics tracking code.
The second set of Javascript tags encompass the methods necessary to execute the tracking call for the page data. This part of the tracking code should also contain any customized methods that you would want to apply to all pages on your site. Both the initialization and the methods are placed within a try/catch block so that any JavaScript errors are handled without impact to the visitor.
The order of the method calls provided in the generic tracking snippet is significant, and you should follow these general guidelines when altering the tracking code for your website's purposes:
var pageTracker = _gat._getTracker("UA-123456-1");The first line of the standard tracking code snippet initializes the default tracker object to the Google Analytics web property ID you provide as a parameter. Subsequent method calls then use that object.
_trackPageview()
method._trackPageview()
.// put in customized calls after pageTracker object and before_trackPageview() methods
pageTracker.setAllowLinker(true);
pageTracker._trackPageview();
The tracking code is designed to read data from your page after the content for the page has finished loading. For this reason, the snippet should be located just before the closing </body>
tag for your web page. Once the content for your page has loaded, the
tracking code, when executed, reads the content for your page following
the Document Object Model (DOM). All information relevant to tracking
is then used to establish page information, set/update cookies, and to
send the GIF request to the Google Analytics servers.
By placing the script at the end of the page body, you ensure that the tracking code is executed as the last element of the DOM. If a given page load is interrupted for some reason, it is possible that the GIF request for that page view will not be executed. However, should you place the tracking code at the top of the page, any load interruptions might result in incomplete or inaccurate reporting anyhow, since the tracking code relies on page data for its reports.
Additionally, the physical placement of the tracking code call at the bottom of the page is more effective than using an onLoad()
function to call the tracking code. If you use onLoad()
to execute the tracking code, execution relies on the event model for
the browser instead of the DOM. In such a situation, should a remote
image fail to load on a page, onLoad()
will not be called, whereas the DOM for the page could still load completely.
You can customize Google Analytics in a variety of ways to modify how reporting data is displayed. Most websites (except the most basic) can benefit from adjustments to the basic setup. In fact, if your site has any of the following characteristics, modifications to the tracking code are required in order for visitor behavior to be accurately reported.
In addition, you can make many other adjustments to the standard Google Analytics reporting behavior, such as adjusting the length of the visitor session, changing the length of a campaign session, or turning off collection of browser information.
You can customize reporting in two basic areas:
The administrative interface provides the following features you can use to refine your data display in the reports:
The GATC provides a number of methods that you can use to configure
the tracking code for your site's needs. Keep in mind that any one page
on your site should be using either the urchin.js
tracking code or the ga.js
tracking code and their related methods, but not both.
Using both tracking codes on a single page can cause reporting errors for those pages and is not advised.
Some of the most common scenarios that require tracking code configuration are described in this guide. Additionally, the Tracking API provides a list of all the ga.js
tracking methods you can use, and also groups those methods by basic reporting uses, such as ecommerce and event tracking methods.