dan1978@gmx.net | My favorites ▼ | English ▼ | Sign out
Skip to page content Skip to main navigation
e.g. "adwords" or "open source"

# Asynchronous Migration Examples

As a best practice, first copy and save your existing tracking code customizations to a text file (not a word processor document). With a backup copy, you can ensure that you've migrated all customizations for your site.

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.

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

    var pageTracker = _gat._getTracker('UA-XXXXX-X');
    pageTracker._trackPageview();

urchin.js Tracking

    _uacct = "UA-XXXXX-X";
    urchinTracker();

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

    var pageTracker = _gat._getTracker('UA-XXXXX-X');
    pageTracker._setClientInfo(false);
    pageTracker._setAllowHash(false);
    pageTracker._setDetectFlash(false);
    pageTracker._setDetectTitle(false);
    pageTracker._trackPageview();

urchin.js Tracking

    _uacct = "UA-XXXXX-X";
    _ufsc = 0;    // track browser info
    _uhash = off; // cookie integrity checking using hashes
    _uflash = 0;  // detect Flash version
    _utitle = 0;  // track title in reports
    urchinTracker();

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

    var pageTracker = _gat._getTracker('UA-XXXXX-X');
    pageTracker._setCampaignCookieTimeout(31536000000);
    pageTracker._trackPageview();

urchin.js Tracking

    _uacct = "UA-XXXXX-X";
    _ucto = "31536000"; // the number of seconds in 1 year
    urchinTracker();
    </script>

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

    var pageTracker = _gat._getTracker('UA-XXXXX-X');
    pageTracker._setCampNameKey('ga_campaign');       // name
    pageTracker._setCampMediumKey('ga_medium');       // medium
    pageTracker._setCampSourceKey('ga_source');       // source
    pageTracker._setCampTermKey('ga_term');           // term/keyword
    pageTracker._setCampContentKey('ga_content');     // content
    pageTracker._setCampNOKey('ga_nooverride');       // don't override
    pageTracker._trackPageview();

urchin.js Tracking

    _uacct = "UA-12345-1";
    _uccn = "ga_campaign"; // name [default: utm_campaign]
    _ucmd = "ga_medium"; // medium [default: utm_medium]
    _ucsr = "ga_source"; // source [default: utm_source]
    _uctr = "ga_term"; // term/keyword [default: utm_term]
    _ucct = "ga_content"; // content [default: utm_content]
    _ucid = "ga_id"; // id number [default: utm_id]
    _ucno = "ga_nooverride"; // don't override [default: utm_nooverride]
    urchinTracker();

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

    var pageTracker = _gat._getTracker('UA-XXXXX-X');
    pageTracker._setCampaignTrack(false);       // Turn off campaign tracking
    pageTracker._trackPageview();

urchin.js Tracking

    _uacct = "UA-12345-1";
    _uctm = "disable";
    urchinTracker();

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

    var pageTracker = _gat._getTracker('UA-XXXXX-X');
    pageTracker._setDomainName('none');
    pageTracker._setAllowLinker(true);
    pageTracker._trackPageview();
    ...
    <a href="http://example.com/test.html"
    onclick="pageTracker._link('http://example.com/test.html'); return false;">click me</a>

urchin.js Tracking

    _uacct = "UA-12345-1";
    _udn = "none";
    _ulink = 1;
    urchinTracker();

    ...
    <a href="http://newsite.com/test.html"
    onclick="__utmLinker('http://newsite.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>

Traditional (ga.js) Snippet

    var pageTracker = _gat._getTracker('UA-XXXXX-X');
    pageTracker._setDomainName('none');
    pageTracker._setAllowLinker(true);
    pageTracker._trackPageview();
    ...
    <form name="f" method="post" onsubmit="pageTracker._linkByPost(this);">
    ...
    </form>

urchin.js Tracking

    _uacct = "UA-12345-1";
    _udn = "none";
    _ulink = 1;
    urchinTracker();
    ...
    <form name="f" method="post" onsubmit="__utmLinkPost(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']);

Traditional (ga.js) Snippet

    var pageTracker = _gat._getTracker('UA-XXXXX-X');
    pageTracker._trackPageview();
    pageTracker._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
    );
    pageTracker._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
    );
    pageTracker._trackTrans();

urchin.js Tracking

    _uacct = "UA-12345-1";
    urchinTracker();
    </script>
    <form name="utmform" id="utmform">
    <textarea name="utmtrans" id="utmtrans" style="display:none">
    UTM:T|1234|Mountain View|11.99|1.29|5|San Jose|California|USA
    UTM:I|1234|DD44|T-Shirt|Green Medium|11.99|1
    </textarea>
    </form>
    <script type="text/javascript">
    __utmSetTrans();

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

    var pageTracker = _gat._getTracker('UA-XXXXX-X');
    pageTracker._trackPageview();
    ...
    <a onclick="pageTracker._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']);

Traditional (ga.js) Snippet

    var pageTracker = _gat._getTracker('UA-XXXXX-X');
    pageTracker._setSessionCookieTimeout(3600000);
    pageTracker._trackPageview();

urchin.js Tracking

    <script type="text/javascript">
    _uacct = "UA-12345-1";
    _utimeout = "3600"; // the number of seconds in 1 hour
    urchinTracker();
    </script>

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

    var pageTracker = _gat._getTracker('UA-XXXXX-X');
    pageTracker._addIgnoredOrganic('ignore');
    pageTracker._addIgnoredRef('urchin.com');
    pageTracker._addOrganic('new_search_engine','q');
    pageTracker._trackPageview();

urchin.js Tracking

    _uacct = "UA-12345-1";
    _uOno[0] = "ignore"; // keyword to treat as referral
    _uRno[0] = "urchin.com"; // referral to treat as direct
    _uOsr[41] = "new_search_engine"; // new search engine to treat as referral
    _uOkw[41] = "query"; // keyword query parameter for new search engine

    urchinTracker();

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

    var pageTracker = _gat._getTracker('UA-XXXXX-X');
    pageTracker._setCookiePath('/path/of/cookie/');
    pageTracker._trackPageview();

urchin.js Tracking

    <script type="text/javascript">
    _uacct = "UA-12345-1";
    _utcp = "/path/of/cookie/";
    urchinTracker();
    </script>

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

    var pageTracker = _gat._getTracker('UA-XXXXX-X');
    pageTracker._setLocalRemoteServerMode();
    pageTracker._trackPageview();

urchin.js Tracking

    _uacct = "UA-12345-1";
    _userv = 0;
    urchinTracker();

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

    var pageTracker = _gat._getTracker('UA-XXXXX-X');
    pageTracker._trackPageview('/downloads/pdfs/corporateBrief.pdf');

urchin.js Tracking

    urchinTracker('/downloads/pdfs/corporateBrief.pdf');

Back to Top

English Español 日本語한국어 Português (Brasil)Pусский 中文 (简体)中文 (繁體)
Suggestions

Find developer products...Find open source projects...