Monday 26 October 2009

Google analytics: easier transaction tracking

In my previous posts I introduced the idea of externalised javascript libraries to abstract over Google Analytics page tracking.

This idea also transfers nicely into Google Analytics transaction tracking where, it seems, most people seem to come unstuck.

We'll try and get you stuck back together!

These articles assume you are at least familiar with the concept of javascript, Google Analytics tracking and the Live HTTP Headers plugin in FireFox.

As with the last posts - use these scripts at your own risk. Don't link to the script libraries directly, copy and tune to your own needs!

Simple transaction tracking made simpler

Take a peek at the transaction tracking example

Thanks for buying stuff! No, seriously, even though the page looks as unspectacular as the Page Tracking Example you have actually registered a (fake) transaction with Google Analytics for a t-shirt and an indoor frisbee.

Take a look at the page source. We're using ms_ga.js again. The method we are interested here is ms_ga_trans.

Notice the source does not track a page view! That'll be taken care of for us by the method.

ms_ga_trans takes three parameters: an array of UA accounts, a URL and a data structure representing the transaction.

The array of UA accounts enables us to track the transaction in any number of Google Analytics accounts. The URL is nullable so we can track the transaction page as a specific URI or just use the default. The transaction data structure mirrors the structure set out in the Google Analytics Transaction tracking documentation.

A transaction requires:

an order id
an affiliation
a shipping value
a tax value
a total value
a country, city and state

Clearly an order also requires one or more order items. Each item consists of:

the order id
a sku code
a name
optionally, a category (very useful)
a unit price (how much for one?!)
a quantity

Now, I'll stress this point about numerical values here. Do not include currency symbols. It is really important that when specifying a price or tax value or shipping value that you specify the value as a number to two decimal places. That is all. Not:






or similar.

Just go for the simplest option which is (for example),


and stick it in quotes - everything is a string. Simple. No fuss. No complexity. If something is free, list it as 0. Don't miss out fields. You'll miss the wrong field and the script will fall over. With abstracted/externalised scripts and method calls you can build in more error handling and checking - handy!

So, inside the guts os ms_ga_trans we see a similar format to ms_ga_pagetrack where we iterate through the array of UA account values to track against:

{"order_id": "666666", "shipping": "5.95", "web": "", "tax": "1.79", "total": "21.74", "country": "UK", "items": [
{"price": "6.00", "category": null, "name": "t-shirt", "sku": "t-1", "quantity": 1},
{"price": "8.00", "category": "home", "name": "indoor frisbee", "sku": "f-1", "quantity": 1}

We see that the page view is tracked here to - hence, we do not track the page separately. It is important to track the page view before recording the transaction.

The transaction is then added using the ga.js addTrans method. Each item in the transaction is then added and finally the transaction is tracked.

So, there isn't that much that is simplified here other than the order in which the steps happen and the opportunity to build in more error checking and handling. If you've got these basics right - well done. You want to know the transaction has registered though, right? Real time? Right, thought so.

Reload the page with our new best friend Live HTTP Headers looking over our shoulder so we can review what happened under the hood.

First of all, no surprises, ga.js loaded.

Then we track the pageview with utm.gif

Take note of the third section in the HTTP Headers output - there is a new utm value called utmtid and it has our transaction id, '666666'. Cool - transaction being tracked with expected values! Okay, so we can see the total in utmtto, the tax in utmtx, shipping in utmtsp etc.

Each follwing section details each order item. utmut=item, utmipn for the item name, utmiqt for the quantity. Real self explanatroy stuff when you know where to look and what to look for.

Never have a transaction fail to track ever again!

Thats should do for this article - next time we Optimiser our website with Google Website Optimiser - but simpler. ;-)

No comments: