Google Analytics

Traditional Syntax

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:

  1. Standard Setup
    1. The Tracking Code Script Part One
    2. The Tracking Code Script Part Two
    3. Where to Place the Tracking Code
  2. Customized Setup
    1. Customizing Using the Administrative Interface
    2. Customizing with Additional Tracking Code Methods

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.

Standard Setup

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 view (profile). Additionally, if you are setting up a view (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 Tracking Code Script—Part One

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 Tracking Code Script—Part Two

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:

  • The first line of the tracking script should always initialize the page tracker object. 
  • 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.
  • The final lines of your tracking code snippet should call the _trackPageview() method.

  • Any custom method calls that set or initialize a value should be inserted before _trackPageview().
      // put in customized calls after pageTracker object and before_trackPageview() methods
      pageTracker.setAllowLinker(true);
      pageTracker._trackPageview();

Where to Place the Tracking Code

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.

Customizing Your Setup

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.

  • Ecommerce—You have an ecommerce site or a shopping cart and you want to track visitor activity related to purchases.
  • Multiple domains—Your website presence spans multiple host names or domains, and you want to track visitor activity (including shopping cart activity) across those properties.
  • 3rd-Party Shopping Carts—Your website is hosted by a provider on a sub-directory and you want to track visitor activity across a part of the website (such as a shopping cart) located in another directory of the host's site.
  • Custom Campaign Tracking Variables—You already have links to your site that contain custom campaign tracking variables that you want to use.

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 of the Google Analytics reports
  • The use of additional tracking code methods in your web pages

Customizing Using the Administrative Interface

The administrative interface provides the following features you can use to refine your data display in the reports:

  • Goals
    Set up goals for key pages on your site that you expect users to visit.  Most commonly, goals are used in ecommerce sites to look at statistics for how customers come to the final purchase page in a set of related pages, such as shopping cart, order form, and order acknowledgment.  But, you can also use goals for any other scenario, such as a step-by-step guide on a how-to process, or a "more" link on a blog.  See the Help Center articles for useful information on goals.
  • Views (Profiles) and filters
    You can set up views (profiles) for individual report users to include or exclude certain content from those reports. This is useful when you have a very large website and want to determine page statistics for one section of the site independently from the other.  For example, you might have one section of a website dedicated entirely to clothing and another section of the website dedicated entirely to electronics, and you want to analyze your clothing page statistics in relationship to all clothing, but independently of electronics. When views (profiles) are used to exclude certain sections of a website, they restrict content reporting for only allowed pages. See the Help Center for useful information on views (profiles) and filters.

Customizing with Additional Tracking Code Methods

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.

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.