English | Site Directory

Google Analytics Tracking Code

Tracking Sites

The visitor tracking information that you can get in the Google Analytics reports depends on Javascript code that you include in your website pages, referred to as the Google Analytics Tracking Code (GATC). Initial releases of the GATC used a Javascript file called urchin.js.   In late 2007, a new version of the GATC was released using file called ga.js.  This updated tracking code is the platform on which all new Google Analytics features will be deployed.  If you are using the urchin.js tracking code and have not yet migrated to ga.js, you can refer to the GA Help Center for tracking customizations using urchin.js.

Once you install the ga.js tracking code on your site, you begin receiving report data. However, if your site has any of the following characteristics, modifications to the tracking code are essential in order for visitor behavior to be accurately reported.

  • You have an ecommerce site or a shopping cart and you want to track visitor activity related to purchases.
  • Your website presence spans multiple host names or domains, and you want to track visitor activity (including shopping cart activity) across those properties.
  • 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.
  • 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.

Generic 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:

<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">
var pageTracker = _gat._getTracker("UA-xxxxxx-x");
pageTracker._trackPageview();
</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 anything other than a top-level domain, such as a sub-directory or a sub-domain, you will need to modify the tracking code in order to get reporting data for your account. This also applies to data for websites that use ecommerce or customized ad campaign variables.  See the Domains & Directories Guide for more information.

Part 1: The Tracking Code Script Reference

The first part of the script tag (represented by lines 1 - 5 of the code above), uses Javascript to dynamically determine whether the HTTP protocol for the requested page is either secure or standard. It uses the appropriate protocol to call 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.

Part 2: The Tracking Code Execution

The second set of Javascript tags encompass the methods necessary to execute the tracking call for the page data (described above). This part of the tracking code should also contain any customized methods that you would want to apply to all pages on your site.

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();
Back to Top

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.  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

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 or 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.
  • Profiles and filters
    You can set up 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 profiles are used to exclude certain sections of a website, they restrict the data to those included pages. The report metrics then calculate "site" for that profile as only the part of the data received via the profile and its filter.  See the Help Center for useful information on profiles and filters.

Using 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.

Back to Top