Track 404 Errors
404 errors are broken links, changed URLs, or mistyped addresses — all of which hurt user experience and SEO. Tracking them in GA4 lets you find the highest-volume 404s and fix them. The key is capturing not just that a 404 happened, but which URL was requested and how the user got there.
The recommended approach: dataLayer push on the 404 template
Section titled “The recommended approach: dataLayer push on the 404 template”The most reliable method is to add a dataLayer push directly to your 404 page template. Every CMS and framework has a 404 template — this is the right place to set up the signal.
Add this to your 404 page template, before the GTM snippet if possible, or in the page body.
<script> window.dataLayer = window.dataLayer || []; window.dataLayer.push({ event: 'page_not_found', page_type: '404', requested_url: window.location.href, referrer: document.referrer || 'direct' });</script>If you have access to server-side data, include it:
<script> window.dataLayer = window.dataLayer || []; window.dataLayer.push({ event: 'page_not_found', page_type: '404', requested_url: window.location.href, referrer: document.referrer || 'direct', // If your server passes these as template variables: error_code: '404', server_path: '{{ request.path }}' // Replace with your template syntax });</script>-
Add the dataLayer push to your 404 template, before the closing
</head>tag. If your framework uses_document.js(Next.js),404.vue, or a CMS error page — that is the right file. -
Create a Custom Event Trigger
- Trigger type: Custom Event
- Event name:
page_not_found
-
Create Data Layer Variables
DLV - requested_url→ Data Layer Variable name:requested_urlDLV - referrer→ Data Layer Variable name:referrer
-
Create a GA4 Event Tag
- Tag type: Google Analytics: GA4 Event
- Event name:
page_not_found - Parameters:
requested_url→{{DLV - requested_url}}referrer→{{DLV - referrer}}page_type→404
- Trigger: the Custom Event trigger
-
Register
requested_urlas a custom dimension in GA4Go to GA4 → Admin → Custom Definitions → Custom Dimensions and register:
- Dimension name: Requested URL
- Scope: Event
- Event parameter:
requested_url
This makes the parameter available in reports.
GA4 - page_not_found
- Type
- Google Analytics: GA4 Event
- Trigger
- Custom Event - page_not_found
- Variables
-
DLV - requested_urlDLV - referrer
GTM fallback: Page Path trigger
Section titled “GTM fallback: Page Path trigger”If you cannot modify the 404 template, use a GTM Page Path trigger. This is less precise but works without code access.
-
Create a GA4 Event Tag
- Tag type: Google Analytics: GA4 Event
- Event name:
page_not_found - Parameters:
requested_url→{{Page URL}}referrer→{{Referrer}}
-
Create a Page View Trigger
- Trigger type: Page View
- Fire on: Some Page Views
- Condition: depends on how your CMS serves 404s:
- If 404 pages have a consistent path:
Page Pathmatches RegEx^/404 - If 404 pages have the original URL with a 404 template:
Page Titlecontains404orPage Not Found
- If 404 pages have a consistent path:
Analysing 404 data in GA4
Section titled “Analysing 404 data in GA4”Once you have data flowing, create an Exploration report:
- Go to Explore → Blank Exploration
- Add segment:
Event name = page_not_found - Dimensions:
Requested URL,Session source / medium,Page referrer - Metrics:
Event count
The Requested URL dimension shows you which broken URLs get the most traffic. The Page referrer shows you which pages on your own site link to broken URLs.
Priority fix order:
- High-traffic 404s with internal referrers → fix the internal link
- High-traffic 404s with external referrers → set up a 301 redirect
- High-traffic 404s with no referrer (direct) → likely a bookmarked old URL → redirect
Test it
Section titled “Test it”- Open GTM Preview
- Navigate directly to a URL on your site that returns a 404 (try
/this-page-does-not-exist) - The
page_not_foundevent should appear in the Summary pane - Verify
requested_urlshows the full URL you requested - In GA4 DebugView, verify the event appears
Common gotchas
Section titled “Common gotchas”Site serves soft 404s. Some CMSs return a 200 status code for missing pages but display a “not found” message. GTM cannot detect HTTP status codes. The dataLayer approach on the 404 template is the only reliable solution.
URL includes sensitive data. Some URLs contain form field data or tokens in query parameters. Strip these before pushing to the dataLayer: window.location.pathname + window.location.search.replace(/token=[^&]+/, 'token=REDACTED').
Referrer is blank for internal links. If the user navigated from an HTTPS page to an HTTPS 404, the referrer should be populated. If it is blank, the user may have typed the URL directly or used a bookmark.