Google Analytics Ecommerce Tracking
Posted on 07 Aug 2008 at 03:46PM. Filed under Development
So you’ve installed Google Analytics, the superb free web analytics software and it’s telling you all sorts of things about your site, most of which you don’t even understand. You’re learning what a Bounce Rate is and realising how powerful this thing really is.
Well, it gets even better. I’ve recently discovered its’ Ecommerce tracking feature. It allows you to capture Ecommerce related statistics alongside your normal web related statistics. Once you have it correctly setup for your site you can see how much money you’ve taken in sales, how many transactions have taken place, how many products have been purchased, and your Ecommerce conversion rate. Very nice.
Make it so
In order to activate Ecommerce tracking, you need to firstly turn on the feature within Google Analytics, and secondly add some extra JavaScript code to your receipt or thanks page.
To turn on the feature, go to the Profile Settings for your site, click Edit (top right) and under E-Commerce Website, select “Yes, an E-Commerce Site”.
Here’s the extra code you need to add to the receipt page straight from Google’s help page.
1 <script type="text/javascript"> 2 var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); 3 document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); 4 </script> 5 6 <script type="text/javascript"> 7 var pageTracker = _gat._getTracker("UA-XXXXX-1"); 8 pageTracker._trackPageview(); 9 10 pageTracker._addTrans( 11 "1234", // Order ID 12 "Mountain View", // Affiliation 13 "11.99", // Total 14 "1.29", // Tax 15 "5", // Shipping 16 "San Jose", // City 17 "California", // State 18 "USA" // Country 19 ); 20 21 pageTracker._addItem( 22 "1234", // Order ID 23 "DD44", // SKU 24 "T-Shirt", // Product Name 25 "Green Medium", // Category 26 "11.99", // Price 27 "1" // Quantity 28 ); 29 30 pageTracker._trackTrans(); 31 </script>
If you’re already using Analytics, the first few lines will be familier to you. They’re
just the standard page tracking calls. The bits we’re interested in are the
pageTacker._addTrans(), pageTracker._addItem() and pageTracker._trackTrans() methods.
You’ll need to fill in the relevant details, and more importantly repeat the _addItem
call for each item within the order. The method for doing this will obviously differ depending
on your server side choices, but for Rails I’ve got something similar to this.
1 <% @order.items.each do |item| %> 2 3 pageTracker._addItem( 4 <%= @order.order_id %>, // Order ID 5 <%= @order.sku %>, // SKU 6 <%= item.product.name %>, // Product Name 7 <%= item.product.category %>, // Category 8 <%= item.unit_price %>, // Price 9 <%= item.qauntity %> // Quantity 10 ); 11 12 <% end %>
You should now start to get E-Commerce stats showing up under the Ecommerce section of Google Analytics.