Google Analytics Third-Party Shopping Cart (Cross-Domain) Tracking Using Traditional and Asynchronous Snippet

Cross-domain tracking is used for several situations the most obvious for third-party shopping carts. Proper installation of the Google Analytics (GA) tracking code is essential for accurate tracking of visitors and sales.

GA offers two tracking code snippets for you to choose from: traditional (synchronous) and asynchronous.

First, a short review of the basic installation for both the traditional and the asynchronous:

Basic Tracking Code Installation

Traditional (Synchronous) Tracking Code

<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">
try{
var pageTracker = _gat._getTracker("UA-xxxxxx-x");
pageTracker._trackPageview();
} catch(err) {}
</script>

Traditional (synchronous) basic installation is done by placing the above GA tracking code, after replacing “UA-xxxxxx-x” with your account number, before the closing body (</body>) tag on all pages of your site. This placement is supposed to decrease the likelihood that it will interfere with other javascript on your page.

The drawbacks with this installation include increased page load time and the possibility it won’t run at all if the user clicks away from the page before it fully loads.

More on the traditional tracking code installation from Google.

Asynchronous Tracking Code

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

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
  })();

Asynchronous basic installation is done by placing the above GA tracking code, after replacing “UA-xxxxxx-x” with your account number, before the closing head (</head>) tag on all pages of your site. Like the traditional code, this placement is meant to decrease the likelihood that it will interfere with other javascript on your page.

According to Google, the asynchronous code has the following advantages:

  • Faster tracking code load times for your web pages due to improved browser execution
  • Enhanced data collection and accuracy
  • Elimination of tracking errors from dependencies when the JavaScript hasn’t fully loaded

More on the asynchronous tracking code installation from Google.

Third-Party Shopping Cart (Cross-Domain) Tracking

It is essential that your GA tracking code is installed correctly for this to work properly.

GA tracks with first-party cookies. If you do not install this correctly, the cookies will be set incorrectly, have the wrong data in them, or both. More on this later…

Traditional (Synchronous) Tracking Code

Put the following code, replacing “UA-xxxxxx-x” with your account number, on ALL pages of your site and the shopping cart pages anywhere between the body tags but ABOVE any calls to pageTracker:

<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._setDomainName("none");
pageTracker._setAllowLinker(true);
pageTracker._trackPageview();
</script>

Links to the shopping cart when adding items to the cart, for example, must include onclick="pageTracker._link(this.href); return false;"

Forms that submit to the shopping cart must include onsubmit="pageTracker._linkByPost(this)"

Do not forget to put that on ALL forms if checkout is a multi-form (multi-step) process. Your data will be incorrect if you do not do this.

If your third-party cart already includes built-in GA code, follow their instructions exactly.

More on the traditional code installation of third-party shopping cart tracking from Google.

Asynchronous Tracking Code

Put the following code, replacing “UA-xxxxxx-x” with your account number, on ALL pages of your site and the shopping cart pages just above the closing head (</head>) tag:

 var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-xxxxxx-x']);
  _gaq.push(['_setDomainName', 'none']);
  _gaq.push(['_setAllowLinker', true]);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
  })();

Links to the shopping cart when adding items to the cart, for example, must include onclick="_gaq.push(['_link', 'https://your-cart.com/cart.html']); return false;"

Replace “https://your-cart.com/cart.html” with the actual link to your cart.

Forms that submit to the shopping cart must include onsubmit='_gaq.push(['_linkByPost', this]);'

Do not forget to put that on ALL forms if checkout is a multi-form (multi-step) process. You will data will be incorrect if you do this incorrectly.

Do not add the asynchronous tracking to third-party cart that have their own GA solution built-in unless specifically instructed to do so.

More on the asynchronous code installation of third-party shopping cart tracking from Google.

Why It’s Important to Do This Correctly

The cookies set by GA when the setDomainName parameter is set to “none” may in some cases have different HOST setting. That is, it may have a www.mydomain.com when the original code without the setDomainName may set HOST to just .mydomain.com. Or, worse still, you could lose all the visitor data and have the sale appear to have been referred from your site and nothing else.

This results in bad data coming into GA particularly in the referrer area. It probably disassociates your keywords to your sales as well.

Recommended Reading

Posted in Google Analytics. Tags: . Permalink. Both comments and trackbacks are closed.

5 Comments

  1. Brian
    July 9, 2010 at 1:38 pm | Permalink

    Thanks for the post. Between your post and the Google help pages, I was able to set up everything with our third party shopping cart and it worked great.

  2. June 24, 2010 at 7:20 am | Permalink

    @Shopping Cart
    The code provided is from Google. There is a link under each code example for an explanation from them.

  3. June 24, 2010 at 3:16 am | Permalink

    the codes you provided also needs some description

  4. March 4, 2010 at 2:51 pm | Permalink

    Sorry I forgot to click notify when new comments are added.

  5. March 4, 2010 at 2:46 pm | Permalink

    Anyone have any input on Netsuite integration?

One Trackback

  1. [...] Original post: Google Analytics Third-Party Shopping Cart (Cross-Domain) Tracking … [...]