How to Track 404 Errors with Google Tag Manager

If you have a website containing 100s of pages, chances are you will have pages which have been deleted or not existing. Somehow these pages are still appearing and people visit these pages from referral sites.

This results in a decrease in your organic rankings and hence provides a way to your competitors to rank. In this article, I will teach you how to track 404 errors with Google Tag Manager and view the reports in Google Analytics.

 

Step1: Check the error message

The very first step you will have to do is to check what message displays when a nonexisting page is loaded. On some websites, the title displays as ‘404 not found’  and some websites display simply the website name and message in the body.

You can test this by simply typing in the URL, www.yourwebsite.com/test. This will display not found page on your website.

404 not found in the title is easy to track so I will explain the second method. There is no 404 not found message displayed in the body nor the title. What will you do?


In this case, you will have to right click and open page source. Press ctrl+f and type not found, this will display the id or CSS selector.

 404 not found message

Copy this ID and go to your GTM account.


Step2: Create a User-Defined Variable

Open your Google Tag Manager account and navigate to variables section.

Create a new user-defined variable.

Select the variable type as DOM element

Select selection method as CSS Selector

Enter element selector as body

Enter id in the attribute name field

Enter a name of the variable and hit save

 not found variable

Step3: Create a new Trigger

Navigate to triggers section and create a new trigger

Select the trigger type as DOM ready

Select the trigger to fire on some DOM ready events

Select the not found variable equals not-found
Enter the trigger name as not found

 not found trigger

Hit save


Step4: Create a New Tag

Now, we have to send this data to Google Analytics. To do this, we will create a new universal analytics tag.

Select the tag type as Event

Enter the tag name as GA Event PageNotFound
Category: Page Not Found

Action: {{Page Path}}

Label: {{Referrer}}
Non-Interaction Hit: True

 not found tag

Add the trigger we created in step 3.

Preview and publish the once you make sure it’s firing correctly.

 

Conclusion

The biggest benefit of tracking 404 pages is you can resolve the issues whenever it arises. This tracks even faster than search console and lets you find the issues instantly. If you have anything to ask related to this topic, please let us know directly on Twitter.