Google Analytics will happily show you a pile of 404 hits, but it answers the wrong question. It tells you which URL failed, not how anyone reached a broken address. That second question is the one that matters, because it decides whether you fix a link, add a redirect, or ignore it.
The answer is the referrer: the page a visitor was on right before the 404. It is almost always an internal link, an external site, or direct traffic, and each points to a different fix. Capture the referrer and every error turns into a clear instruction.
Here is how to surface it in GA4, confirm it in Search Console, and log it automatically in WordPress.
For the full list of what triggers these errors in the first place, see what causes 404 errors in WordPress.
Track 404 Referrers in GA4
GA4 only sees a 404 if your error page loads the analytics tag and sends an event. Fire a dedicated event from your 404 template that carries the broken URL and the referrer.
// Run this on your 404 template, after gtag has loaded
gtag('event', 'page_not_found', {
broken_url: location.pathname + location.search, // the broken URL that was requested
referrer: document.referrer || '(direct/none)' // where the visitor came from
});Before that data can show up in reports, register each parameter as an event-scoped custom dimension. In GA4, open Admin, then Custom definitions, then Custom dimensions, and click Create custom dimensions once for each row below.
| Dimension name | Scope | Event parameter |
|---|---|---|
| Broken URL | Event | broken_url |
| 404 Referrer | Event | referrer |
The Event parameter must match your code exactly, including case. If it is not in the dropdown yet, type it in by hand, since GA4 only lists parameters it has already received. Custom dimensions are not retroactive and can take 24 to 48 hours to appear in reports, but they show instantly in DebugView and Realtime, which is the fastest way to confirm the event is firing.
With the dimensions in place, build a Free Form exploration, set the event name to page_not_found, and add broken_url and referrer as dimensions. You get a table that pairs every broken URL with the page that led to it. It is the same custom parameter pattern from my guide to events in Google Analytics.
Read the Referrer to Find the Cause
Once the referrer is visible, it points to one of those three causes, and each has a clear fix.
| Referrer | What it means | What to do |
|---|---|---|
| A page on your own domain | A broken internal link | Edit the source page and correct the link |
| An external domain | An outdated or wrong inbound link | Add a 301 redirect to the right URL |
(direct) or empty | Typed URL, old bookmark, or stripped referrer | Redirect if it is a former URL, otherwise ignore |
The Document.referrer property returns the URI of the page that linked to this page. (MDN Web Docs)
For an external link worth keeping, a permanent 301 redirect passes its value to the correct page instead of wasting it.
Confirm Crawler Errors in Search Console
Search Console catches the 404s that Google hits, which are the ones that hurt SEO. Open the Pages report, expand the Not found (404) group, and select a URL. The full workflow is in my complete Google Search Console guide.
Run URL Inspection and check the Referring page, which shows where Google found the broken link. If it is your page, fix the link. If it belongs to another site, a redirect keeps the value of that inbound link.
Log 404s Automatically in WordPress
On WordPress you can skip the manual setup. A logging plugin records the requested URL, the referrer, and the user agent for every 404, and lets you add a redirect on the spot.
The Redirection plugin is the common choice. Its log groups repeated misses, so you see which broken URLs get real traffic and which are just bots probing for files that never existed.
FAQs
Common questions about tracing the source of 404 errors.
document.referrer in a custom event so the source shows up next to each broken URL.301 redirect to the correct page.Summary
A 404 names the URL that failed; the referrer names the cause. Capture document.referrer in a GA4 event, confirm crawler errors in Search Console, or let a WordPress plugin log both. Once you see where each visitor came from, every 404 becomes a simple call: fix the link, add a redirect, or leave it alone.

