Gary Sieling

Recording on-site behavior in Google Analytics

Google Analytics has a ton of built-in features1 to help you learn how people are using your applications. To take full advantage of this, I found it necessary to follow a guide2.

For my research project (a lecture search engine), I’d like to see a report of queries people run, and how good the results are (# of results, # that can be played inline, # where authors have written books). Like so:

To set this up, you’ll want to first create named custom metrics and dimensions you that want inside Google Analytics. I don’t think this is strictly necessary, but it makes the analytics UI show your names. “Metrics” are numeric measures you want to track (e.g. number of results), and dimensions are attributes of an event or page view (query, time of day, etc).

Each dimension / metric is numbered (dimension1, dimension2, …) so you’ll want to keep careful track of these, and not re-purpose them in your site. Not that these values are scoped to “hit”, “session”, “product”, or “user”, the difference between these should be relatively apparent.

To record these values, you need to add some Javascript to your pages. I recommend surrounding this in a try/catch, so that you don’t throw errors, to respect your users with aggressive ad blockers. I would also blank out the values you don’t use, because they will otherwise be sent on subsequent events and cause confusion.

try {
  ga('set', 'dimension1', selections.query);
  ga('set', 'dimension2', selections.page);
  ga('set', 'dimension3', null);
  ga('set', 'dimension4', null);
  ga('set', 'dimension5', null);
            
  ga('set', 'metric1', (data.response || {}).numFound);
  ga('set', 'metric2', numWithPlayer);
  ga('set', 'metric3', numWithAmazon);

  ga('send', 'pageview');
} catch (e) {
}

To see this working, you can look at the network traffic in your browser. You will see attributes named “cd” and “cm” (custom dimension and custom metric). The original Google Analytics didn’t have real time metrics, and some portions still take a while to show up – this is one of them. While some people report that custom values show up within 1-2 hours, it took about 12 for me. If they don’t show up at all, it’s possible your code is not calling their API in the right order3

cd1:test
cd2:1
cm1:13
cm2:13
cm3:0

You can also record event data, which is pretty slick, e.g.:

When you drill into the secondary dimensions, you get a nice view. The challenge here is planning out what dimensions go in each slot (you don’t want the first index to be sometimes a URL and sometimes a query, if possible, to avoid reports that mix these). I also found that if you have two values, like the name of a checked field and it’s value, that there doesn’t seem to be a way to report on both at once. The solution to this may be to add a third value that is both, so that you get the choice later.

This requires more Javascript, similar to the previous example:

try {
  ga('set', 'dimension1', null);
  ga('set', 'dimension2', null);
  ga('set', 'dimension3', null);
  ga('set', 'dimension3', incomingFacetConfig.field);
  ga('set', 'dimension4', incomingFacetValue);

  ga('set', 'metric1', null);
  ga('set', 'metric2', null);
  ga('set', 'metric3', null);
            
  ga('send', 'event', 'facet');
} catch (e) {
}

Finally, if you just want to track queries, Google Analytics has a nice built-in feature for that, but you have to enable it. The setting is in the administration panels for your site – enable “Site Search Tracking” and set the query parameter, and in a few hours, you’ll start to get data.

  1. https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#content []
  2. https://www.optimizesmart.com/complete-guide-to-dimensions-and-metrics-in-google-analytics/ []
  3. http://stackoverflow.com/questions/23528368/google-analytics-custom-dimension-not-being-set []
Exit mobile version