(Ab)using Google Analytics user timings to track SQL queries

A client has been suffering unstable system load. One big root cause is that lot of overly-complex queries fire off for each page. We're improving matters, but we wanted to grab some metrics to prioritize our efforts.

We tried custom variables, but found it frustrating that they didn't let us link to the right page, and dealt with our values as strings rather than numbers.

So we're using the new user timings so we can sort pages by their number of queries and get a ranked list of which pages are causing the most load. We just pretend each query is a millisecond when we call Google Analytics:

_gaq.push(['_trackTiming','Other Metrics','Database Queries',<?= // number of queries here ?>,window.location.pathname])

Then we get a nice list of URIs sortable by the average number of queries that render each page. The number tracks over time, so you can drill down to a particular page to see if the number is changing over time.

We plan to add New Relic monitoring into the mix sometime soon, but it was fun to try it this way.

I was really surprised at how poorly the various metrics integrate in Google Analytics. I really expected to have a way to toss a value on a pageview and be able to treat it like just another data point, but it really doesn't work that way at all. It didn't look like event values were going to solve it either, though maybe those would work just like this wound up working—I thought of the window.location.pathname bit last (I'd been hoping to key off the underlying view file instead, but couldn't make that work quickly).