Tracking 404 error pages with Google Tag Manager (GTM) is an essential step for monitoring user experience and ensuring that missing pages are handled appropriately. By tracking 404 errors, you can get insights into how often users encounter non-existent pages on your site, which can help you fix broken links or improve the user journey.
Here’s how to track 404 error pages with GTM:
Step 1: Create a Trigger for 404 Pages
You can detect 404 error pages based on the URL, the HTTP status code, or page content. Most commonly, 404 pages are identified by their URL path (e.g., /404, /page-not-found) or a specific class or ID in the HTML that indicates a 404 error.
1. Using the URL or Page Path to Detect 404s
One of the simplest ways to track 404 pages is by setting up a trigger based on the URL or path of the page. Many websites have a standard URL structure for 404 pages, such as /404, /not-found, or /page-not-found.
- Go to your Google Tag Manager container.
- In the left-hand menu, select Triggers and click New.
- Name the trigger something like “404 Error Page Trigger.”
- Choose Page View as the trigger type.
- Under Trigger Configuration, select Some Page Views.
- In the conditions, select Page Path (or Page URL) and set it to match the 404 error page path. For example:
- Page Path contains /404(or/page-not-found, or whatever your 404 URL pattern is).
- You can also use Page URL if your URL structure includes the 404 page name or slug.
 
- Page Path contains 
Step 2: Set Up the Tag to Fire on 404 Pages
Once you’ve created the trigger to detect when users land on a 404 error page, you need to attach it to a tag.
- Go to Tags and click New.
- Choose a tag type based on what you want to track. For instance, you could use:
- Google Analytics to send an event when a user hits a 404 page.
- Custom HTML to send a custom event to another analytics tool or log it for debugging.
 
- If using Google Analytics, choose the Google Analytics: Universal Analytics or Google Analytics 4 Event tag type, depending on your setup.
- Configure the tag:
- For Universal Analytics, set it to track an event with parameters like:
- Category: ‘Error’
- Action: ‘404 Page’
- Label: {{Page Path}} (or {{Page URL}}, to capture the URL of the error page)
 
- For GA4, set it up as an event with a similar structure, e.g., event_name = '404_error'and any relevant parameters (e.g.,page_location = {{Page URL}}).
 
- For Universal Analytics, set it to track an event with parameters like:
- Under Triggering, choose the 404 error trigger you created earlier.
Step 3: Preview and Test Your Setup
Before publishing the GTM container, test to make sure everything works correctly:
- Click Preview in GTM to enable the preview mode.
- In the debug mode, open your website in a new browser tab and intentionally go to a non-existent page (e.g., https://yourwebsite.com/nonexistentpage).
- Check the GTM preview panel to see if the trigger for the 404 page fires correctly. If it does, the tag should show up under Tags Fired.
- Check your Google Analytics real-time events to verify that the 404 error event is being sent.
Step 4: Publish Your Container
Once you’ve confirmed the trigger and tag work as expected, it’s time to publish the container:
- Go to GTM and click Submit.
- Provide a version name and description (e.g., “Track 404 Error Pages”).
- Click Publish to make the changes live.
Step 5: Verify in Google Analytics
After publishing the GTM container, you can verify that the 404 error tracking is working in your analytics platform (Google Analytics or GA4).
For Google Analytics Universal:
- Go to Behavior > Events > Top Events.
- Look for the event category (e.g., “Error”) and check if the 404 page events are appearing under the Action (e.g., “404 Page”).
For Google Analytics 4:
- Go to Realtime or Events.
- Check if the 404 event is appearing in the events list.
- You can create a custom report or use Event Exploration to track how often 404 errors are triggered.
Advanced Considerations
1. Track 404 Errors Based on HTTP Status Code
If your website uses JavaScript or a server-side solution to handle 404 errors (such as showing an error message on the page), you may also be able to detect 404 errors based on the HTTP status code. This requires more advanced setup, typically using Custom JavaScript or Custom HTML tags in GTM.
2. Use Content on the 404 Page to Trigger Events
Some websites design their 404 pages with specific content (such as a unique ID or class name), which can be used to trigger an event. For example, you could use a DOM Element trigger to detect the presence of an element on the 404 page. Here’s how to do that:
- In GTM, create a new trigger of type DOM Element.
- Set the trigger condition to look for a specific ID or class on the 404 page (e.g., an element like <div class="error-404">).
- Use this DOM Element trigger with your tag to track 404 errors.
Best Practices for Tracking 404 Error Pages
- Monitor 404 Errors Regularly: Track and monitor 404 errors regularly to identify broken links or missing pages. This helps you fix issues that could negatively affect your SEO or user experience.
- Use Google Search Console: While GTM can track 404 errors, also monitor Google Search Console for 404 errors reported by Googlebot, especially if the error is SEO-related.
- Fix or Redirect 404 Errors: Once you track 404 errors, ensure you fix broken links or set up proper redirects (e.g., 301 redirects) for missing pages to improve the user experience and reduce bounce rates.
- Implement Custom 404 Pages: A well-designed 404 page with useful links and a search bar can help users find the content they’re looking for instead of leaving your site.
By setting up 404 error tracking with GTM, you can improve your website’s functionality, enhance user experience, and monitor important error pages to avoid losing potential visitors.