Sign in

Analytics Help

Google Analytics Tracking Code Migration Guide

Print

Although transitioning to use the new Google Analytics tracking code (ga.js) from the legacy tracking code (urchin.js) is optional, many people find that they are eager to switch to ga.js to take advantage of its many benefits. You can learn more about the difference between the two versions by browsing through our common questions on this topic.

Once you decide to make the switch to ga.js, it's important to remember that not only will the basic tracking code snippet change, but the other Google Analytics function calls will also change. The chart below shows, by topic, how the old function calls and variable assignments map to the new function calls. To find out how these functions are used, click on the function name.

Although this article should serve as a good guide for basic implementations of the code, we suggest you reference our Google Analytics Tracking Code site at Google Code for a full list of ga.js customizations and content relating to collecting your data. Please note that at this time the Google Analytics Custom Tracking site is only available in English. We thank you for your patience as we work on meeting user demands and translating this site into the additional languages where it's needed.


Initialization and page tracking

Back to Top

Initialize page tracking with the functions below.

Function Old Style New Style
Account to track.
Identifies the account number in which the page should be tracked.
_uacct _gat._getTracker(urchinAccount)

Track the page.
Sends the page tracking data to the Google servers.

urchinTracker(); _trackPageView(opt_pageURL)

Together, the Initialization and Page Tracking functions should look like the code shown below.

Old Style New Style
...

<script type="text/javascript">
_uacct = "UA-XXXXX-X";
urchinTracker();
</script>
...

<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-XXXXX-X");
pageTracker._trackPageview();
</script>

 


Control data collection settings

Back to Top

By default Google Analytics will track a set of data elements, such as Flash versions and web browser information. You have the option to turn this detection off by using the functions listed below, but remember - once you do so, you will lose the data permanently.

Control the amount of information you collect by using the functions listed below.

Function Old Style New Style
Collect browser attributes.
Sets the browser tracking module. Controls collection of browser attributes. You can also learn how to disable client tracking for certain pages.
_ufsc _setClientInfo(enable)

Detect Flash version.
Sets the Flash player version detection flag.

_uflash _setDetectFlash(enable)

Detect the page title.
Sets the title detection flag. Learn how to configure GA to ignore titles.

_utitle _setDetectTitle(enable)

Together, the control data collection settings functions should look like the code below.

Old Style New Style
...

<script type="text/javascript">
_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();
</script>
...

<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-12345-1");
pageTracker._setClientInfo(false);
pageTracker._setAllowHash(false);
pageTracker._setDetectFlash(false);
pageTracker._setDetectTitle(false);
pageTracker._trackPageview();
</script>

 


Tracking Subdomains and Third-Party Sites

Back to Top

Track a visitor across a site or multiple sites using links or forms with the functions listed below.

Function Old Style New Style
Share cookies with another domain via HREF or GET.
Passes the cookies from visited site to another via query string parameters.
__utmLinker(this.href); _link(targetUrl)

Share cookies with another domain via POST.
Passes the cookies from visited site to another by adding 0query string parameters in a POST form request.

__utmLinkPost(this); _linkByPost(formObject)
Enable linking to another domain.
Sets the linker functionality flag.
_ulink _setAllowLinker(true or false)

Determine the domain for the tracking cookies.
Use when linking across subdomains or to other domain names.

_udn _setDomainName(newDomainName)

The table below shows what the code will look like when you track multiple sites using a link versus a form.

Tracking method Old Style New Style
Using a link ...

<script type="text/javascript">
_uacct = "UA-12345-1";
_udn = "none";
_ulink = 1;
urchinTracker();
</script>
...
<a href="http://newsite.com/test.html"
onclick="__utmLinker('http://newsite.com/test.html'); return false;">click me</a>
...

<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-12345-1");
pageTracker._setDomainName("none");
pageTracker._setAllowLinker(true);
pageTracker._trackPageview();
</script>
...
<a href="http://newsite.com/test.html"
onclick="pageTracker._link('http://newsite.com/test.html'); return
false
;">click me</a>
Using a form ...

<script type="text/javascript">
_uacct = "UA-12345-1";
_udn = "none";
_ulink = 1;
urchinTracker();
</script>
...
<form name="f" method="post" onsubmit="__utmLinkPost(this)">
...
</form>
...

<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-12345-1");
pageTracker._setDomainName("none");
pageTracker._setAllowLinker(true);
pageTracker._trackPageview();
</script>
...
<form name="f" method="post" onsubmit="pageTracker._linkByPost(this)">
...
</form>

 


E-Commerce Transaction Tracking

Back to Top

Track sales revenue and measure success with the funtions below.

Function Old Style New Style
Add a new item to an e-commerce transaction.


The data in each item line follows a pipe (|) separated list.
UTM:I|order-id|product name|category|total|quantity _addItem(orderId, sku, productName, variation, unitPrice, quantity)

Add a new e-commerce transaction to track.
The data in each transaction line follows a pipe (|) separated list.

UTM:T|order-id|affiliation|total|tax|shipping|city|state|country _addTrans(orderId, store, total, tax, shippingFee, billingCity, billingState, billingCountry)

Send transaction and item data to Google.
Use after all the items are added to the transaction.

__utmSetTrans(); _trackTrans()

Use the code below on your receipt page to track your e-commerce transactions.

Old Style New Style
...

<script type="text/javascript">
_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();
</script>
...

<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-12345-1");
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();
</script>

 


Campaign Tracking

Back to Top

Configure your Google Analytics to track your campaigns using the functions shown below.

Function Old Style New Style
Set campaign tracking flag.
Activates campaign tracking module.
_uctm _setCampaignTrack(enable)

Set campaign content key.
Parses campaign content from URL. Use this information to create A/B tests with our free Website Optimizer tool.

_ucct _setCampContentKey(newCampContentKey)
Set the campaign ID key.
Use to parse campaign ID from URL.
_ucid _setCampIdKey(newCampIdKey)
Set the campaign medium key.
Use to parse campaign medium from URL.
_ucmd _setCampMediumKey(newCampMedKey)
Set the campaign name key.
Use to parse campaign name from URL.
_uccn _setCampNameKey(newCampNameKey)
Set the campaign no-override key.
Use to parse campaign no-override value from URL.
_ucno _setCampNOKey(newCampNOKey)  
Set the campaign source key.
Use to parse campaign source from URL.
_ucsr _setCampSourceKey(newCampSrcKey)
Set the campaign term/keyword key.
Use to parse campaign term or keyword from URL. Should be used to track paid search advertising.
_uctr _setCampTermKey(newCampTermKey)
Set allow anchor usage flag for campaigns.
When enabled, uses # instead of the default ? to separate the request stem from the query string.
_uanchor _setAllowAnchor(enable)

 

Try tracking campaigns with custom field names. Pass these names to the campaign functions on your landing pages so that Google Analytics can recognize your campaign information from your manually tagged URLs.

Old Style New Style

...

<script type="text/javascript">
_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();
</script>

...

<script type="text/javascript">
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._setCampIdKey("ga_id"); // id number
pageTracker._setCampNOKey("ga_nooverride"); // don't override
pageTracker._trackPageview();
</script>

 


Customizations: Sources

Back to Top

Set keyword or referrer ignore preferences, or add new organic search engines to track.

Function Old Style New Style
Add new ignored organic keyword.
List of keywords to ignore. Learn how you can treat certain referral sources as direct traffic.
_uOno[] _addIgnoredOrganic(newIgnoredOrganicKeyword)

Add new ignored referrer.
List of referral domains to ignore.

_uRno[] _addIgnoredRef(newIgnoredReferrer)

Add new organic search engine to track.
List of organic search engines to match automatically in referrals.

_uOsr[]
_uOkw[]
_addOrganic(searchEngine, queryVariable)

 

Look at the code below to see how you can configure Google Analytics to set ignore preferences and add organic search engines.

In the example below, note that "41" is within the brackets for the "add organic" functions. This number may change as Google Analytics is constantly adding more properties to its list of referred search engines. To see an up-to-date list, please visit http://www.google-analytics.com/urchin.js and change the number in the brackets within your urchin.js code accordingly.

Old Style New Style

...

<script type="text/javascript">
_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();
</script>

...

<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-12345-1");
pageTracker._addIgnoredOrganic("ignore");
pageTracker._addIgnoredRef("urchin.com");
pageTracker._addOrganic("new_search_engine","q")
;
pageTracker._trackPageview();

</script>

 


Customizations: Cookies

Back to Top

Reset and control your cookies so they serve your unique business needs.

Function Old Style New Style
Set flag to allow domain hash.
Create a unique domain hash (or numerical representation) of the domain name, which is then put into cookies. Look at controlling data collection settings to see how this is supposed to be implemented into the code.
_uhash _setAllowHash(true or false)
Set new default cookie path.
Controls the path in the request used to determine what cookies are (re)set and delivers to the requesting site. By default, Google Analytics sets the cookie path to /. If you would like to change this, send your preferred cookie path to the _setCookiePath function.

_utcp

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

_setCookiePath(newCookiePath)

....
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-12345-1");
pageTracker._setCookiePath("/path/of/cookie/");
pageTracker._trackPageview();
</script>

Set new default cookie expiration time.
Set the campaign tracking cookie timeout in seconds. Google Analytics credits the most recent campaign if conversion happens within six months, but you customize the timeout by sending the desired number of seconds to the _setCookieTimeout function.

_ucto

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

_setCookieTimeout(newDefaultTimeout)

...
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-12345-1");
pageTracker._setCookieTimeout("31536000");
pageTracker._trackPageview();
</script>

Set the new session timeout in seconds.
The standard session timeout is 30 minutes, but you can customize it to what works for your business. Please use this function carefully since this very important setting is used to compute visits.

_utimeout

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

_setSessionTimeout(newTimeout)

...
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-12345-1");
pageTracker._setSessionTimeout("3600");
pageTracker._trackPageview();
</script>

Set user defined value.
Set the visitor segmentation cookie to help classify the types of visitors coming to your site. For example: new customer vs. prospect, or cat owner vs. dog owner.

__utmSetVar(newVal);

...
<script type="text/javascript">
_uacct = "UA-12345-1";
urchinTracker();
__utmSetVar("test_value");
</script>

_setVar(newVal)

...
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-12345-1");
pageTracker._trackPageview();
pageTracker._setVar("test_value");
</script>

Get the session ID.
Return the session ID from the __utma cookie.

__utmVisitorCode(); _visitCode()

 



Customizations: Using a Local Server

Back to Top

Function Old Style New Style
Set the new local GIF path.
Set the file system path to the __utm.gif file.
_ugifpath _setLocalGifPath(newLocalGifPath)
Set operation in both local and remote server mode.
Sends data to both Google Analytics backend server and local server. Local server path is determined by _setLocalGifPath() value. Please note that you can track Google Analytis and Urchin software by calling this function in the tracking code.

_userv = 2;

...
<script type="text/javascript">
_uacct = "UA-12345-1";
_userv = 2;
urchinTracker();
</script>


_setLocalRemoteServerMode()

...
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-12345-1");
pageTracker._setLocalRemoteServerMode();
pageTracker._trackPageview();
</script>

Set operation in local server mode.
Sends data to a local server. Path to local server is determined by _setLocalGifPath() value.

_userv = 0;

_setLocalServerMode()

Set operation in remote server mode.
Sends data to Google Analytics backend server.

_userv = 1;

_setRemoteServerMode()

 

The information you were looking for?

Learn from other users

Find answers, ask questions, and share your expertise with others in the Analytics help forum.