Tagged with " Analytics"
Mar 6, 2010 - Analytics    5 Comments

Implementing Google Analytics tracking for 3rd party shop

When you run a business, there are always dozens of things that should be done, but there is never time. We use Plimus here as our payment processor company, so our online shop is hosted on Plimus servers. This proved to be problematic, as we lost all referral information for our orders. This means we had little idea where did our customers come from. And it stayed like this for far too long.

When you are a programmer and customers like your products, it’s easy to fall into trap thinking that improving your business is done 99% by improving the applications. However, that’s not exactly true – even if you have the best software in the world, if it is a niche product, word of mouth isn’t enough to reach the tipping point where ‘everybody knows what you do’. So, it’s crucial to track where do your customers come from, and simply, make that stream as large as you can.

To have as precise analytics as possible, we used asynchronous Google tracking code. So, instead of placing tracking code on the bottom of the page (before </body> tag), it will be placed on the top (right after opening <body> tag). As it is asynchronous code, it doesn’t slow down page loading, but does run earlier than standard tracking code.

Here is the code snippet:

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXX-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);
  })();

</script>

Two bolded lines (_setDomainName and _setAllowLinker) are required for cross-domain tracking, and you should replace UA-XXXXX-X with your Google Analytics domain identifier.

Second thing we need to do is to update links to online shop. In addition to normal href tag of the link, we have to add onlick Javascript event to push necessary cookies across domains:

<a href="https://your.shop/url" onclick="_gaq.push(['_link', 'https://your.shop/url']); return false;">Buy Now</a>

The last thing needed is placing proper tracking script to your shopping cart pages (on domain other than your site). This step is actually probably not needed, as many shops now directly support Google Analytics and you only need to give them your UA-XXXXX-X GA site identifier. I know Plimus, FastSpring, and PayPro support GA natively – and probably many others, too.

In case your shop doesn’t support GA natively, this is not hard. Paste the async tracking code we used above to each page of your shop, right after opening <body> tag. You will probably have a template page in your shop control panel, so you will only need to do it in one place.

Don’t be lazy in a wrong places, as I have been for way too long. You cannot improve what you don’t track – so make sure you know exactly from where your customers come, why do they buy, how many of them downloaded trial, and as much other information you can.