ga.js:
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.
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.
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.
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.
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:
var pageTracker = _gat._getTracker("UA-123456-1");
_trackPageview() method._trackPageview().// put in customized calls after pageTracker object and before_trackPageview() methods pageTracker.setAllowLinker(true); pageTracker._trackPageview();
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 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.