Google Analytics Ecommerce Tracking

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<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-XXXXX-1");
pageTracker._trackPageview();

pageTracker._addTrans(
  "1234",                                     // Order ID
  "Mountain View",                            // Affiliation
  "11.99",                                    // Total
  "1.29",                                     // Tax
  "5",                                        // Shipping
  "San Jose",                                 // City
  "California",                               // State
  "USA"                                       // Country
);

pageTracker._addItem(
  "1234",                                     // Order ID
  "DD44",                                     // SKU
  "T-Shirt",                                  // Product Name
  "Green Medium",                             // Category
  "11.99",                                    // Price
  "1"                                         // Quantity
);

pageTracker._trackTrans();
</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
2
3
4
5
6
7
8
9
10
11
12
<% @order.items.each do |item| %>

  pageTracker._addItem(
    <%= @order.order_id %>,                     // Order ID
    <%= @order.sku %>,                          // SKU
    <%= item.product.name %>,                   // Product Name
    <%= item.product.category %>,               // Category
    <%= item.unit_price %>,                     // Price
    <%= item.quantity %>                        // Quantity
  );

<% end %>

You should now start to get E-Commerce stats showing up under the Ecommerce section of Google Analytics.

References