Chris Peters's Blog

Error page tracking in Google Analytics

September 21, 2015

Are you aware of how many 404 “not found” errors your website is really serving? Are you aware of which URLs are causing this error? Or perhaps it’s a pain to comb through your log analysis software to find the answer?

If your website serves up custom 404 and 500 error pages, you can add a small tracking snippet to these pages to track the following in Google Analytics:

  • When these errors occur
  • What URLs are causing the errors
  • Where the user was referred from to trip the error

Here’s the snippet to add to your 404 error page:

<script>
var referrer = document.referrer;
if (referrer === '') {
referrer = 'No Referrer';
}
ga('send', 'event', '404 Error', document.location.href, referrer, 0, { nonInteraction: true });
</script>
view raw 404-errors.html hosted with ❤ by GitHub

And the one for 500 pages is similar, with a minor difference:

<script>
var referrer = document.referrer;
if (referrer === '') {
referrer = 'No Referrer';
}
ga('send', 'event', '500 Error', document.location.href, referrer, 0, { nonInteraction: true });
</script>
view raw 500-errors.html hosted with ❤ by GitHub

These errors will then be trackable via Events in Google Analytics, which can be found under the Behavior section.

You can now find this info in my Google Analytics Setup Guide in the Playbook. Check it out to learn about the process that I use to setup Google Analytics tracking on websites.

Hat tip to Dan Wilkerson’s Essential Guide to Error Tracking Using Google Analytics for this information.


Written by Chris Peters, your friendly neighborhood digital marketing professional with over 20 years of experience of web design, programming, SEO, and marketing.