Skip to Content

How to Track 404 Events Using Google Analytics and Google Tag Manager

404 Errors are some of the most frustrating issues for both website visitors and owners. Here's how to track those dreaded errors in Google Analytics and what you can do when you find them.

You're probably familiar with this scenario. You're on a quest to find one singular piece of information -- that one nugget of info you know will improve your life somehow. You scroll through the Google results page and find a promising H1 title and an even more enticing meta description.

You quickly click on what you believe will hold the answer you seek. However, heartbreak sets in when you see this instead:

404 Errors as Seen in the Browser

Emptiness. Hopelessness. Not the answer you need. A 404 Error.

These 404 errors frustrate both web managers and web users. However, there are simple ways to track 404 errors using Google Analytics and Google Tag Manager. This tutorial seeks to lay those out step by step and also tell you what to do when you stumble upon these issues.

What's a 404 Error?

Simply put, 404 errors are messages from browsers letting an internet user know that the address they want can't be found. While these won't kill your website entirely, enough 404 errors on critical website pages could greatly hurt conversions. Imagine having optimized a page, spending hours on the copy, and making sure the UX design is near perfect -- only to have no one actually reach the page. And if that happens on one of your website's most important resources, you've just wasted not only a visitor's time but also your own. 

There are many reasons a 404 error may be thrown. Specific pages may have been deleted or the source URL could have a typo in it. In any event, 404 errors indicate to the user that something has gone wrong and be greatly disruptive to the end user experience.

Why Track 404 Errors?

Tracking 404 errors has a number of benefits. Most obviously, it helps you sniff out the causes of these events. Is there a specific URL (or set of URLs) that produce the most errors? Is there a specific referral that's responsible for many of the errors? Tracking this data gives you the capability of not only improving experience but stopping 404 errors before they render in a user’s browser.

What You Need to Start Tracking

The code provided will report 404 events within Google Analytics, so you must have an up-and-running account there to take advantage of this tutorial. You also need access to your site’s 404 template and (this is important) the 404 page must preserve the URL structure of the typed/clicked page. This means that your 404 events can’t just redirect to your 404 page. You must serve the 404 template dynamically with the exact URL that is throwing the error. Most web server services (such as Apache) allow you to do this with a variety of rewrite rules.

Right and Wrong URL Structures for 404 Tracking

Tracking 404 Errors with Google Analytics

With Google Analytics, tracking explicit 404 errors is straightforward and simple. Just ensure that your main Google Analytics tracking script is in place and then add the following code to your 404 Error Page/Template:

You will need to swap out the ID of your specific Google Analytics account. After that, the script works by sending an “event” to Google Analytics. The category is “404 Response," the action uses JavaScript to pass the URL that throws the error, and the label uses JavaScript to pass along the previous URL the user was on. Through all of this data, you can then see what URLs cause 404 events and where people are accessing those URLs.

Tracking 404 Errors with Google Tag Manager

More and more web managers have decided to move to Google Tag Manager. This tool gives them the capability of embedding a whole host of scripts through a single container. It's especially useful if you have a number of tracking scripts from several providers.

To begin tracking 404s through Tag Manager, first begin by creating a “Variable” called “Page Title Variable”. This variable type is a “JavaScript” variable and the Variable Name is “document.title”:

Setting Up Variables in Google Tag Manager

Essentially, we’re creating a variable that checks for a page’s given title. This is how we will check if we are on a 404 page.

Then create a “Trigger” called “404 Page Title Trigger”. The type is “Page View” and the trigger fires when the “Page Title Variable” contains “404 – Page Not Found” or whatever it is your 404 page title displays as within the browser.

Setting up Triggers in Google Tag Manager

Lastly, you will need to create a “Tag” called “404 Event Tag”. The tag type is “Universal Analytics” and contains the following components:

Setting up Events in Google Tag Manager

The Variable, Trigger, and Tag all work to pass along the relevant data directly to Google Analytics.

404 Event Reporting

No matter your tracking method (be it through Tag Manager or direct event beacons), your reporting should be the same within Google Analytics. Under “Behavior”, you will see an item called “Events”. Here you will see all reported 404 events. The “Event Action” and “Event Label” dimensions will give you the pertinent data of what URLs are throwing 404 errors and their referring source.

Reporting 404 Events in Google Analytics

With all of this place, you can now regularly monitor your 404 errors and take the necessary steps to minimize their occurrence. In doing so, you optimize your referral sources and provide the best user experience, keeping conversions and engagement on the right path.

Don't worry about disappointing your users with 404 Errors. Give them the content they crave by taking care of those pesky issues.

Scott Madara
Contributions Editor here at Solodev. Want to be featured on the Solodev Blog? Get in touch.
Follow me on Twitter

Sign up to explore

Create an account in seconds and start building today.

Sign up for Solodev