dan1978@gmx.net | My favorites | English | Sign out

Google Analytics

Asynchronous Migration Examples

This document illustrates common tracking customizations for Google Analytics. 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.

  1. Basic Page Tracking
  2. Browser Settings Detection
  3. Campaign Duration
  4. Campaign Fields — Custom
  5. Campaign Tracking Disabled
  6. Cross-Domain Linking
  7. Cross-Domain Form Data
  1. Ecommerce Tracking
  2. Event Tracking
  3. Sources — Search Engines, Keywords, Referrers
  4. Session Timeout
  5. Tracking Limited to a Sub-directory
  6. Using a Local Server
  7. Virtual Pageviews

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']);

Traditional (ga.js) Snippet

urchin.js Tracking

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']);

Traditional (ga.js) Snippet

urchin.js Tracking

Back to Top

Campaign Duration

By default, the duration of an AdWords 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']);

Traditional (ga.js) Snippet

urchin.js Tracking

Back to Top

Campaign Fields — Custom

Google Analytics automatically collects your Google AdWords data if you have linked your Adwords 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']);

Traditional (ga.js) Snippet

urchin.js Tracking

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']);

Traditional (ga.js) Snippet

urchin.js Tracking

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>

Traditional (ga.js) Snippet

urchin.js Tracking

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>

Traditional (ga.js) Snippet

urchin.js Tracking

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']);

Traditional (ga.js) Snippet

urchin.js Tracking

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>

Traditional (ga.js) Snippet

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']);

Traditional (ga.js) Snippet

urchin.js Tracking

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']);

Traditional (ga.js) Snippet

urchin.js Tracking

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']);

Traditional (ga.js) Snippet

urchin.js Tracking

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']);

Traditional (ga.js) Snippet

urchin.js Tracking

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. However, if you want to configure goals based on clicks to PDFs or downloads, you need to use this method (but be aware that these clicks will be tallied as part of your overall pageview count). For tracking Flash or Silverlight content, we recommend that you use the Adobe Flash Tracking library or the Microsoft Silverlight component.

Async Snippet

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

Traditional (ga.js) Snippet

urchin.js Tracking

Back to Top