Universal Analytics 404 Tracking

Recently at work all the talk has been of Universal Analytics (UA), so I thought it was about time that I take the plunge and upgrade my personal WordPress to Google’s all singing all dancing analytics solution.

One of the nice things about UA is that it is now possible to pass data back to Analytics in a “Data Layer”. I decided a simple 10 minute task that would help with running the blog would be using Universal Analytics 404 tracking. Using UA in this way will fire an event each time a 404 error is experienced by a visitor to the blog.

If you’re looking to do a similar thing with your WordPress blog the first thing you need to do is make sure that your using a Universal Analytics account. Check out the Google Dev guides for more information on upgrading to UA.

How to get Universal Analytics 404 Tracking on your Blog

Next up locate the 404.php template file. The easiest way to do this is to log in to your WordPress dashboard and go: Appearance -> Editor and then select the 404 Template on the right hand side.

Once you’ve selected the correct template pop these lines of code in:

<script>
jQuery( document ).ready( function( $ ) {
ga('send', 'event', '404', 'Visit', '404 Error Page - Visit - Page');
});
</script>

This script now sends to UA the useful data that’ll fire an event, helping you track down pages that users are stumbling onto that don’t exist.

Here’s how it’ll look in Analytics:

Using Universal Analytics to track 404 error events

I’ve added in a secondary dimension so we’re displayed the page so we know exactly where the visitor has experienced the 404 error (jeffmainwood.co.uk/bob and /ask).

If you’d like more information just send me a Tweet.