How to Track 404 Errors in WordPress with GTM and GA4

… Last Updated:

Old Man detecting 404 errors on the beach
Metal detector” by (Imagine) 2.0 is licensed under CC BY 2.0 .

The Hidden SEO and UX Problem: 404 Errors

404 errors are silent conversion killers. Users land on broken pages, get frustrated, and leave. Search engines find broken links and reduce your rankings. Yet most WordPress sites have no systematic way to track and fix these issues.

The business impact of 404 errors:

  • Lost organic traffic from broken internal links
  • Poor user experience leading to higher bounce rates
  • SEO penalties for broken link structures
  • Missed conversions from broken promotional links
  • Wasted marketing spend driving traffic to broken pages

The solution: Automatically track 404 errors in Google Analytics 4 using WordPress’s built-in CSS classes and Google Tag Manager.

Common Misconception: “Doesn’t GA4 Already Track 404s?”

Short answer: No. 

GA4 treats 404 pages as regular page views. While you can see traffic to /non-existent-page in your reports, GA4 has no way to know that page returned a 404 error versus displaying actual content.

What GA4 sees:

  • A page view to /broken-link
  • Normal engagement metrics (time on page, bounce rate, etc.)
  • No indication that the page was actually an error

What GA4 doesn’t see:

  • That the server returned a 404 HTTP status code
  • That the user encountered an error experience
  • That this represents a broken link or missing content

This is why manual setup is essential – without the custom tracking we’re implementing, 404 errors are completely invisible in your analytics, disguised as regular page traffic.

Why WordPress Makes 404 Tracking Easy

WordPress automatically adds CSS classes to the <body> element based on page type. On 404 pages, WordPress adds the error404 class, giving us a reliable way to detect these errors.

WordPress body classes for different page types:

  • error404 – Page not found
  • home – Homepage
  • single – Single post pages
  • page – Static pages
  • archive – Archive pages

We’ll leverage the error404 class to automatically detect and track these problematic pages.

Prerequisites

This tutorial builds on our previous post about error tracking:

  • How to Track JavaScript Errors in GTM and GA4

You’ll need:

  • WordPress website with GTM installed
  • Basic understanding of GTM variables and triggers
  • GA4 property set up and connected to GTM

If you followed the JavaScript error tracking tutorial, you already have the custom dimensions set up in GA4. If not, you’ll need to create them.

Setting Up 404 Error Tracking in GTM

Step 1: Create the WordPress 404 Detection Variable

In Google Tag Manager:

  1. Go to Variables → New
  2. Variable Name: CJS - WordPress - Error404
  3. Variable Type: Custom JavaScript
  4. Custom JavaScript Code:

javascript

function() {
  return document.body.classList.contains('error404');
}
  1. Save

What this does: This JavaScript function checks if the current page’s body element contains the error404 class that WordPress automatically adds to 404 pages.

For non-WordPress sites: You can modify this to check for other indicators:

javascript

// Check page title for "not found"
function() {
  return document.title.toLowerCase().includes('not found');
}

// Check for specific text content
function() {
  return document.body.innerText.includes('Page not found');
}

Step 2: Create the 404 Error Trigger

Create a new trigger:

  1. Trigger Name: Exception - 404
  2. Trigger Type: Page View
  3. This trigger fires on: Some Page Views
  4. Condition: {{CJS - WordPress - Error404}} equals true
  5. Save

What this does: This trigger fires only when someone loads a page where WordPress has added the error404 class.

Step 3: Create the GA4 Event Tag

Create a new tag:

  1. Tag Name: GA4 - Event - Exception (404 Error)
  2. Tag Type: Google Analytics: GA4 Event
  3. Measurement ID: {{GA4 - Measurement ID}} (use your constant variable)

Event Configuration:

  • Event Name: exception (keep lowercase)

Event Parameters:

Parameter 1:

  • Parameter: event_category
  • Value: 404

Parameter 2:

  • Parameter: event_label
  • Value: {{Referrer}} (Built-in variable)

Triggering:

  • Select the Exception - 404 trigger

Save the tag

Why these parameters:

  • event_category = “404” groups all 404 errors together
  • event_label = Referrer shows where users came from before hitting the 404

Setting Up GA4 Custom Dimensions (If Not Done Previously)

If you completed the JavaScript error tracking tutorial, skip this section.

In GA4:

  1. Go to Admin → Data Display → Custom Definitions
  2. Click Create Custom Dimension

Create these custom dimensions:

Event Category Dimension

  • Dimension Name: Event Category
  • Description: event_category
  • Scope: Event
  • Event Parameter: event_category

Event Label Dimension

  • Dimension Name: Event Label
  • Description: event_label
  • Scope: Event
  • Event Parameter: event_label

Save both dimensions

Testing Your 404 Error Tracking

Method 1: GTM Preview Mode

  1. Activate Preview Mode in GTM
  2. Navigate to a non-existent page on your site (like /this-page-does-not-exist)
  3. Verify in Tag Assistant that the Exception - 404 trigger fires
  4. Check that the GA4 Event tag executes correctly

Method 2: Check the Variable Value

In GTM Preview Mode:

  1. Go to a 404 page
  2. Click Variables tab in Tag Assistant
  3. Find CJS - WordPress - Error404 – it should show true
  4. On normal pages, it should show false

Method 3: Verify in GA4 Real-Time

  1. Go to GA4 → Reports → Real-time
  2. Navigate to a 404 page on your site
  3. Look for the event in the real-time report
  4. Event name should be “exception”

Creating 404 Error Reports in GA4

Wait 24-48 hours for sufficient data collection.

Step 1: Create a Custom Exploration

In GA4:

  1. Go to Explore → Blank Exploration

Step 2: Configure Dimensions

Add these dimensions:

  • Page Path + Query String (which URLs are broken)
  • Event Category (to filter for 404s)
  • Event Label (referrer sources)

Add to Rows:

  • Page Path + Query String
  • Event Label

Step 3: Configure Metrics

Add metrics:

  • Event Count

Add to Values:

  • Event Count

Step 4: Apply Filters

Under Filters:

  • Event Category exactly matches 404

Step 5: Analyze Your Data

Your report will show:

  • Which broken URLs people are trying to access
  • Where users came from before hitting 404 pages
  • How frequently specific 404 errors occur

Understanding Your 404 Error Data

Critical 404 Patterns to Address

High-traffic 404 pages:

  • URLs getting significant traffic but returning 404
  • Priority: Create content or redirect these URLs

404s from internal links:

  • Referrer shows your own domain
  • Priority: Fix broken internal links immediately

404s from search engines:

  • Referrer shows Google, Bing, etc.
  • Priority: Set up 301 redirects to relevant content

404s from external links:

  • Referrer shows other websites linking to you
  • Priority: Reach out to fix external links or redirect

Common 404 Error Sources

URL structure changes:

  • Changed permalink structure
  • Moved content without redirects
  • Deleted pages/posts

Typos in links:

  • Internal linking mistakes
  • External sites with incorrect URLs
  • Social media posts with broken links

Old promotional campaigns:

  • Expired landing pages
  • Removed product pages
  • Outdated marketing materials

Advanced 404 Error Tracking

Track 404s by Traffic Source

Add source/medium information:

Create additional parameters in your GTM tag:

Parameter: source_medium
Value: {{Source}} / {{Medium}}

Track 404s by Device Type

Add device information:

Parameter: device_category
Value: {{Device Category}}

Set Up 404 Error Alerts

Create automated alerts:

  1. Go to GA4 Admin → Custom Insights
  2. Set up alerts for 404 error spikes
  3. Get notified when important pages start returning 404s

Fixing 404 Errors: Action Plan

Immediate Actions (Fix Today)

High-traffic 404s:

  1. Create content for the requested URL, or
  2. Set up 301 redirect to most relevant existing page

Internal link 404s:

  1. Find and fix broken internal links
  2. Update navigation menus
  3. Fix broken links in old blog posts

Ongoing Maintenance (Weekly)

Review 404 report weekly:

  1. Identify new broken URLs
  2. Check for patterns indicating systemic issues
  3. Set up redirects for legitimate broken URLs
  4. Update any broken external links you control

Long-term Strategy (Monthly)

Comprehensive link audit:

  1. Use WordPress plugins like Broken Link Checker
  2. Review and update old content with outdated links
  3. Implement proper redirect strategy for moved content
  4. Monitor competitor and industry link changes

Integration with Other Error Tracking

This 404 tracking complements other error monitoring:

  • JavaScript Error Tracking for technical issues
  • Form submission error tracking
  • Image/resource loading error tracking
  • Server error (500) tracking

Combined insights provide:

  • Complete picture of website health issues
  • Prioritized list of problems affecting users
  • Data-driven maintenance and optimization roadmap

Professional Website Health Services

While this tutorial covers 404 error tracking basics, comprehensive website maintenance often requires ongoing expertise:

Advanced 404 management services:

  • Automated redirect strategy implementation
  • Comprehensive broken link auditing and fixing
  • SEO-focused redirect planning and execution
  • Integration with development and content workflows

When to get professional help:

  • Large sites with thousands of pages and complex URL structures
  • E-commerce sites where broken product links cost revenue
  • Sites with frequent content changes requiring redirect management
  • Organizations needing training on ongoing link maintenance

What’s included in professional website health monitoring:

  • Complete error tracking setup (404s, JavaScript errors, form errors)
  • Automated alerting and reporting systems
  • Monthly website health audits and fix recommendations
  • Team training on error identification and resolution

Dealing with ongoing 404 errors and website health issues?  Contact Knihter for professional website maintenance and optimization services. We specialize in comprehensive error tracking and systematic website health improvement.

Related Posts:

  • How to Track JavaScript Errors in GTM and GA4 (coming soon)

Related Services:

  • Complete website error tracking and monitoring
  • WordPress maintenance and optimization
  • SEO-focused redirect strategy and implementation
  • GTM implementation and website health consulting