Syntax Comparisons

This document shows tracking code illustrations for a variety of common tracking customizations. If your Google Analytics tracking snippet contains the urchin.js markup or the traditional ga.js syntax, use this guide to migrate your tracking to the improved Asynchronous snippet.

Migrating to Async Tracking

To convert your pages to use the asynchronous snippet:

  1. Remove your existing tracking code snippet and any customizations you have made.

    To reduce errors, we recommend that you remove the existing tracking code snippet from the include file or mechanism that injects it into your web pages. You can paste your existing tracking code snippet in a text file to keep track of any customized methods you use.

  2. Insert the asynchronous snippet at the bottom of the <head> section of your pages, after any other scripts your page or template might use.

    One of the main advantages of the asynchronous snippet is that you can position it at the top of the HTML document. This increases the likelihood that the tracking beacon will be sent before the user leaves the page. We've determined that on most pages, the optimal location for the asynchronous snippet is at the bottom of the <head> section, just before the closing </head> tag.

  3. Modify the _setAccount method with your web property ID.

    For more information on web property IDs, see the "Web Property" section in the Accounts and Views (Profiles) document.

  4. Add your customizations back in using the asynchronous syntax. Use the customizations described in the rest of this guide. See the Asynchronous Tracking Usage Guide for detailed information about the asynchronous tracking code.

Note: To ensure the most streamlined operation of the asynchronous snippet with respect to other scripts, we recommend you place other scripts in your site in one of these ways:

  • Before the tracking code snippet in the <head> section of your HTML
  • After both the tracking code snippet and all page content (e.g. at the bottom of the HTML body)

If that isn't an option, you can still put the asynchronous snippet at the bottom of the page. You can also split your snippet to retain some of the benefits of asynchronous tracking.

Basic Page Tracking

Basic page tracking is the best place to familiarize yourself with the asynchronous syntax. These examples set up the tracking object with the correct account and call the page tracking method.

Async Snippet (recommended)

_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_trackPageview']);

Back to Top

Browser Settings Detection

These examples illustrate browser setting customizations such as disabling Flash detection, turning off browser name/version detection, and so forth.

Async Snippet (recommended)

_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_setClientInfo', false]);
_gaq.push(['_setAllowHash', false]);
_gaq.push(['_setDetectFlash', false]);
_gaq.push(['_setDetectTitle', false]);
_gaq.push(['_trackPageview']);

Back to Top

Campaign Duration

By default, the duration of an Google Ads campaign is set for 6 months. You can adjust the duration of campaigns by using the _setCampaignCookieTimeout() method.

Async Snippet (recommended)

_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_setCampaignCookieTimeout', 31536000000]);
_gaq.push(['_trackPageview']);

Back to Top

Campaign Fields — Custom

Google Analytics automatically collects your Google Ads data if you have linked your Google Ads account to your Analytics account. To track keyword links from other advertising sources, or from email campaigns or similar sources, you can create custom campaigns fields using the methods below. For more details, see "Campaign Tracking" in the Traffic Sources guide.

Async Snippet (recommended)

_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_setCampNameKey', 'ga_campaign']);    // name
_gaq.push(['_setCampMediumKey', 'ga_medium']);    // medium
_gaq.push(['_setCampSourceKey', 'ga_source']);    // source
_gaq.push(['_setCampTermKey', 'ga_term']);        // term/keyword
_gaq.push(['_setCampContentKey', 'ga_content']);  // content
_gaq.push(['_setCampNOKey', 'ga_nooverride']);    // don't override
_gaq.push(['_trackPageview']);

Back to Top

Campaign Tracking — Disabled

Google Analytics automatically enables campaign tracking, but you can disable it using the _setCampaignTrack() method.

Async Snippet (recommended)

_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_setCampaignTrack', false]);    // Turn off campaign tracking
_gaq.push(['_trackPageview']);

Back to Top

Cross Domain Linking

To track traffic across domains, you can use the _setAllowLinker() function to track user clicks between two domains. For details on setting up cross-domain tracking, see Cross-Domain Tracking.

Async Snippet (recommended)

_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_setDomainName', 'none']);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_trackPageview']);
...
<a href="http://example.com/test.html"
onclick="_gaq.push(['_link', 'http://example.com/test.html']); return false;">click me</a>

Back to Top

Cross Domain Form Data

You can use the _linkByPost() method to pass user data from one domain to another where cross domain tracking is enabled for both domains. For details on setting up cross-domain tracking, see Cross-Domain Tracking.

Async Snippet (recommended)

_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_setDomainName', 'none']);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_trackPageview']);
...
<form name="f" method="post" onsubmit="_gaq.push(['_linkByPost', this]);">
...
</form>

Back to Top

Ecommerce Tracking

Ecommerce tracking involves calling three key methods in your tracking setup. See Ecommerce Tracking for details.

Async Snippet (recommended)

_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_trackPageview']);
_gaq.push(['_addTrans',
  '1234',           // order ID - required
  'Mountain View',  // affiliation or store name
  '11.99',          // total - required
  '1.29',           // tax
  '5',              // shipping
  'San Jose',       // city
  'California',     // state or province
  'USA'             // country
]);
_gaq.push(['_addItem',
  '1234',           // order ID - required
  'DD44',           // SKU/code
  'T-Shirt',        // product name
  'Green Medium',   // category or variation
  '11.99',          // unit price - required
  '1'               // quantity - required
]);
_gaq.push(['_trackTrans']);

Back to Top

Event Tracking

Using Event Tracking involves making an event call in the appropriate place in your pages, such as in an onclick handler. For more information on Event Tracking, see the Event Tracking Guide. Note: Event Tracking is not available with the urchin.js tracking code.

Async Snippet (recommended)

This section shows two ways to set up tracking using the Asynchronous syntax. With Event Tracking, the onclick handler uses the exact same syntax.

_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_trackPageview']);
...
<a onclick="_gaq.push(['_trackEvent', 'category', 'action', 'opt_label', opt_value]);">click me</a> 

Back to Top

Session Timeout

By default, user sessions are timed out after 30 minutes of inactivity on your site. These examples show how this can be modified using the _setSessionCookieTimeout() method.

Async Snippet (recommended)

_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_setSessionCookieTimeout', 3600000]);
_gaq.push(['_trackPageview']);

Back to Top

Sources — Search Engines, Keywords, Referrers

By default, Google Analytics identifies a list of websites as search engine referrals in your reports. You can use these methods to alter the search engine list. For more information, see "Search Engines" in the Traffic Sources guide. You can also configure Google Analytics to ignore referrals from certain domains, or to treat specific keyword searches as direct traffic.

Async Snippet (recommended)

_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_addIgnoredOrganic', 'ignore']);
_gaq.push(['_addIgnoredRef', 'urchin.com']);
_gaq.push(['_addOrganic', 'new_search_engine', 'q']);
_gaq.push(['_trackPageview']);

Back to Top

Tracking Limited to a Sub-Directory

Use the _setCookiePath() method to set a sub-directory as the default path for all tracking. You would do this to confine all tracking to a sub-directory of a site.

Async Snippet (recommended)

_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_setCookiePath', '/path/of/cookie/']);
_gaq.push(['_trackPageview']);

Back to Top

Using a Local Server

Use these methods if you are tracking your website using the standalone Urchin software and using Google Analytics as well. For details see the Urchin Server section of the Tracking API reference.

Async Snippet

_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_setLocalRemoteServerMode']);
_gaq.push(['_trackPageview']);

Back to Top

Virtual Pageviews

Use the _trackPageview() method along with a URL you fabricate in order to track clicks from users that do not lead to actual website pages on your site. In general, we recommend you use Event Tracking for tracking downloads, outbound links, PDFs or similar kinds of user interactions. This is because virtual pageviews will add to your total pageview count.

Async Snippet

_gaq.push(['_trackPageview', '/downloads/pdfs/corporateBrief.pdf']);

Back to Top