The case for server side analytics

Published: 19 Apr 2014

![] (/img/googles-new-universal-analytics.jpg “Google Analytics”) There are few google services as widely used as Analytics, and in spite of its quirks, its great value for Money, better than a free lunch.

Almost, that is. While its so very easy to use GA scripts on the client side, we know

  • you lose control over what is shared with Google.
  • it slows down the page load.
  • if you have too much data, you outgrow the free plan, and the next tier blows a $150K hole in your pocket, once a year.

But with universal analytics, it becomes more attractive than ever, esp. for non-conventional use cases. By non-conventional, I mean NY Times used twitter as their database. sort of thing.

Server Side Analytics has none of those drawbacks.

  • No slowing of page load times
  • Complete control over what you share with google.
  • Can be used with non js clients & devices.
  • Sample and share events that are important, and stay within the limits of free plan.
  • Real time insights - can be a zero cost replacement to services like new-relic
  • Track CPU spikes, server exceptions, and what not

Before the Measurement Protocol, server side GA was possible, but very hard, and prone to failures. Now, its a breeze.

Let me explain how we use Server side analytics in some interesting ways.

We need a way to track which links in our mailers are being clicked. Now we could use a link tracker, but there are features we needed which most link trackers won’t offer, and surely not for free.

For example, if redirects to, we would want to redirect to However, we will want link stats to be grouped on (e.g. ignore the query).

While its very easy to write a redirect route that does this, it starts becoming complicated once you want to track

  • total clicks to via this link
  • unique clicks
  • unique clicks by day
  • unique clicks by geo region

Our solution? Write the redirector ourselves, and use GA to track. Server side tracking allows us to send what we want to send to google (i.e. strip query string). And all sorts of slicing and dicing options that GA has are now available.

Advantages - we track what we want, and we share with Google only what we want to share. We can track things like CPU spikes, database timings, exceptions, and just about anything and everything we want to track, in near real time.

On to specifics. One could use the universal-analytics npm module, and here’s some sample code.

First, inside the app.js

var ua = require('universal-analytics');

Then add a ga.js module

var ua = require('universal-analytics');

var ga = {
  pageview: function(title) {
    return function(req,res,next) {
      var udata = {
        dp: req.path, 
        dt: title, 
        dh: '',
        uip: req.ip,
        ua: req.headers['user-agent']
      if (req.visitor) { 
module.exports = ga;

And finally, in the routes you want to track

app.get('/mytracker',dosomething, donextthing, ga.pageview('Track Page'),render,handleError);

Even if you are not using node.js, here is a cool way to do this with nginx. Yes, I am talking of post_action.

post_action is the last phase in nginx processing of a request. Its often used for things like Download Tracking, and here we use it for visit tracking.

Here’s a sample UA call.

Parameters like dp (path), dh, and uip are available fron nginx, so you could include a post_action like this.

location / { 
  proxy_pass backend;
  post_action @log;
location @log {


blog comments powered by Disqus