A poor mans metrics

Everyone wants metrics these days, but it takes a while to invest in good quality metrics for your software. However, if your running a javascript based single app page, or well mostly any webpage. You could use Google Analytics to get some basic quality metrics.

What can we measure?

So I’ll briefly cover some things that you can use google analytics for.

  • Ajax response times
  • Ajax failed requests
  • Feature usage
  • some aspects of javascript errors

There are more specialized measuring tools, but one thing that is interesting with using Google Analytics for this is that your accutally measuring what people are using. The response times will be the response time a client acctually has. Which I find quite intresting. But were getting ahead of our selves.

I’ll some some code at the end of the post describing how to measure some of these. Others can be deducted from the code.

Ajax response times

jQuery has these interesting functions you can add, where you get to maniupate the jQuery jqXhr request before sending it. Here you can jack in, take a timestamp and save it on the request for when your sending it. Then when the request recieves is done you can take the timestamp and compare it against the current time. I’m sure there are similar functions with other libraries, and if not you could easily wrap all your ajax calls in a method that support taking these snapshots.

So now we have a measurement of how long a request took to complete the request, and this can easily be sent to Google Analytics as a custom event.

Ajax failed request

Similar to the time calculated, we can also handle the case where the server returns certain error codes, and just measure how much of these error codes we get. It’s quite easy then to see, how many timeouts does our clients get? Are we getting internal server errors? Unauthorized, not found, etc.

Feature usage

You could make a simple function that you call with a given key to log a usage of a certain feature. This can allow you to experiment with the user interface, or just measure the popularity of a new feature.

Be warned, just because a certain feature gets low measures dosn’t necesarilly mean you can drop it.

Javascript errors

Now adays you can pick up on window.onerror in most browsers, which does allow you to keep track on how many javascript / image loading faults you have on your page. There are tools that speciallize in this, such as my own www.js-analytics.com. That will allow you better details regarding such errors, but sometimes just getting a number will be enough.

The code

As you can see the code sets up two functions to save a timestamp and report back the calculated execution time of the ajax request in milliseconds. There is also a function for reporting ajax errors and hooks up on window.onerror.

It should be quite easy to attach a function that reports back usage on a function based on a given key. But as the script does not attach any public funtions I didn’t want to decide on a namespace or object to attach the feature tracking too.

Conclusion

So you can capture a bunch of metrics and report to Google Analytics. You can probably collect even more metrics form the site, that I have yet too think of. Don’t hesitate to point out if I missed anything or you come up with some other brilliant metric to capture.

What is interesting is that the metrics gathered are also gathered along with other metrics such as how long the user stayed on the site, what content he or she browsed, if they used adcampagins to reach the site, etc. Allowing you to correlate the data gathered with the behaviour data already gathered by Google Analytics itself.

This post is called “poor mans metrics” as you need not set up any service and just depend on Google Analytics, which in most cases already is available. I wonder how poor it really is. Then again, it will never give you detailed metrics of your servers behaviour.