Server-side tracking with Google Analytics
There have been a few times recently when I’ve needed a server-side method for sending tracking data to Google Analytics. Here’s how it’s done.
I’m a big fan of using event tracking to extend the functionality of GA and find it a really useful tool for recording useful e-commerce metrics such as purchase latency, which GA doesn’t record by default. (Check out my post for Econsultancy on such “passive events”, which explains how that’s done.)
I also wanted to take advantage of passive non-interactive event tracking to record some other business critical metrics, such as item profit, item margin, basket profit and basket margin so they could be integrated into my reporting and analysis directly through the GA interface.
Server-Side Google Analytics
The Server-Side Google Analytics class (SSGA for short) is an open source PHP class available from Google Code which aims to tackle precisely the problem I’ve been facing (and a few others I’ll outline later).
The Server Side Google Analytics (SSGA) class relies on the Zend framework’s HTTP client to send data to the GA servers, which means you’ll either need to install Zend on your server or include a local copy and manually set its include_path. If you don’t mind getting your hands dirty you could replacing this dependency with cURL instead, but Zend HTTP works well enough.
How it works
All you need to do is include the SSGA class, pass your account ID (ie UA-1234567-8) to the setAccountId() function, pass your hostname to setHostName and include a page title and page name for setPageTitle() and setPageView(). These last two can be fictitious if you like, as this then won’t skew your existing data.
For each event you want to record you need to provide four values to the setEvent() function, and then use createEvent() to send the data.
The values passed to setEvent() are the same category, action, label and value used by the client-side _trackEvent function and the data you pass to them needs to be in a similar format.
Unlike the client-side API, SSGA converts spaces in the category, action or label to + so you’ll want to bear this in mind when categorising your event tracking data. Like the client-side API, the value needs to be an integer, not a float, so don’t you’ll need to use round() or ceil() to get rid of the decimal point.
Once the code is executed, you should see your event tracking data appearing in Google Analytics under Content > Event tracking > Categories, usually after an hour or so.
I’ve used this method to record the item profit and item margin of each item purchased, the basket margin and basket profit, and other metrics such as the overall product rating and the number and percentage of negative reviews.
However, there are many ways in which you could use the same or similar techniques. You could use it to enhance mobile tracking (in which the current server-side solution doesn’t provide functionality for event tracking or other sophisticated features), record cron-based scripts, track redirects or use this as a back-up in the event of the EU cookie directive preventing you recording e-commerce tracking data.
Using SSGA also helps overcome the Token Bucket algorithm limitation of event tracking, which prevents more than 10 events being sent for a given token.
3 key reasons why server-side GA is useful
1. You can send more confidential data, such as profit and margin, to the GA servers without it being seen in your source code.
2. You can send additional data, bypassing things such as the 10 events per token limit.
If server-side Google Analytics are your cup of tea, you might also be interested in this more recent guide to using the related PHP-GA PHP class to hack a cookie-less GA implementation.
Copy from http://techpad.co.uk/content.php?sid=186