Capturing 404 Errors with Google Tag Manager and Google Analytics

When migrating from our old blogging system to WordPress, our URL structures changed, occasionally resulting in 404 errors. Such errors are typically mitigated with redirect rules. Google Webmaster Tools will notify you of errors, but with days or weeks of lag time. I wanted immediate notification.

So, we used Google Analytics to trap edge cases or special URLs that result in 404 errors.  This way, I can generate 404 error reports quickly, monitor them in realtime during a major migration, capture the referring URL, and make fixes.

We are also using Google Tag Manager to deploy all of our third-party Javascript (it’s awesome). Here’s how to get the two products to intermingle and get the job done:

1. In our 404 error template, we use the Tag Manager dataLayer variable:

2. Create a new macro in Google Tag Manager called Error.

Set the macro to look use a Data Layer Variable named Error (case-sensitive).

3. Create a new rule in Google Tag Manager called “404”.

Set the rule’s conditions to {{Error}} equals 404.

4. Configure your Google Analytics account in Tag Manager.

Name it “Google Analytics” and enter your web property ID.
Assign it the blocking rule of “404 Error”. (important!)

5. Create another Google Analytics account in Tag Manager named “Google Analytics – 404 Errors”.

Under More Settings → Basic Configuration, give it a virtual page path of /404error/?url={{url}}&ref={{referrer}}.

You’re done. Generate some fake 404 errors on your website by entering some bogus URLs.

Monitor the errors in Google Analytics’ realtime view.

For reports, go to Content → Site Content → All Pages. Then filter URLs with our prefix of /404/.

You can now generate helpful reports containing 404 errors. Hopefully you won’t have many, but your users will always surprise you!

Comments are closed.