Server-side tracking with Google Analytics

Server-side tracking with Google Analytics

Server-side tracking with Google AnalyticsCopyright © Matt Clarke

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.

Google Analytics does not yet provide a server-side API for sending data to the GA servers. The current solution uses JavaScript and a GIF to send data from the client side and can’t be easily or reliably triggered from the server-side.

Using the client-side method, the data is sent in the clear to Google so inquisitive developers can look at the JavaScript code or the GIF query string to see what data you’re sending.

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.

However, I don’t really want customers or competitors finding profit or margin data in the JavaScript code or GIF data.

I’ve therefore been looking for an effective server-side solution for sending event tracking data to the Google Analytics server without the need to use either JavaScript or the GIF – and now I’ve found one.

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.

Like the current JavaScript and mobile server-side code, SSGA works by constructing a query string containing the various name=value pairs, but rather than loading a GIF it uses the Zend HTTP client to make the request from the server side.

This means that data can be sent to GA without the need to load up either the JavaScript tag, GIF or cookies keeping the data away from prying eyes. It also means you can use the class in situations where there’s no physical page view, such as via a cron job or other server-side process.

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.

3. You can send data when cookies and JavaScript are disabled, making it useful for mobile sites and for those affected by forthcoming EU cookie legislation.

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

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s