When websites are regularly updated, it’s common for outdated or incorrect links to remain in circulation either from external sources or within your own site. Matomo can track which URLs result in 404 errors and identify the referrer pages that lead visitors to those broken links.

You can capture 404 pages using either the Matomo JavaScript tracking code or Matomo Tag Manager. Both methods record a page view or event when a 404 page is displayed, allowing you to identify the missing URL, the referring page, and how often visitors encounter these errors.

This guide explains how to track 404 pages with Matomo Tag Manager and capture both the original (broken) URL and the page or website the visitor came from.

Before you start

  • Your site must handle 404 pages correctly before setting up tracking.
  • The example in this guide uses a web server to route all missing URLs to a custom 404 page while keeping the original broken URL in the browser’s address bar. This ensures that Matomo receives the correct path, such as /missing-page.html, rather than the internal file used to render the error page.
  • The custom 404 page content that is served has a defined page title 404 Error, which provides a simple and reliable way to trigger 404-specific tracking. When the page title contains 404, Tag Manager can detect the error state and fire the correct tags, even though the browser still shows the original broken URL.
  • Make sure the MTM container snippet is added before the closing </head> tag on all tracked pages including the custom 404 page.
    workflow visual showing 404 tracking in mtm

Create a Custom JavaScript variable

  1. In Tag Manager, go to Variables and click Create New Variable.
  2. Choose the Custom JavaScript type.
  3. Add the following code in the JavaScript Function field and save the new variable.
function() {   return '404/URL = ' + encodeURIComponent(document.location.pathname + document.location.search) +          ' /From = ' + encodeURIComponent(document.referrer); }

Configure Pageview triggers

You will likely have an existing Pageview trigger in Tag Manager that fires your tracking tags on every page. This trigger will need to be updated to prevent sending normal pageviews for the 404 page.

  1. Go to Triggers and edit the existing Pageview trigger.
  2. Add a condition to exclude 404 pages, for example, use: Page Title not contains 404.
  3. Then create a separate Pageview trigger with the condition: Page Title contains 404. In the section below, you will link this trigger to a new tracking tag that collects 404-specific data. This ensures that standard tracking runs on normal pages, while the dedicated 404 tag captures the URL and referrer information only when a missing page is viewed.
    add condition to pageview trigger

Configure a Matomo tracking tag

  1. In Tag Manager, go to Tags and click Create New Tag.
  2. Choose the Matomo Analytics tag and provide a custom name, e.g Track 404 pages.
  3. The Matomo Configuration Variable is automatically assigned to the tag.
  4. Select Pageview as the Tracking Type.
  5. In the Custom Title field, select the Custom JavaScript variable created earlier. This will override the default page title and append both the missing URL and the referrer when displayed in Matomo reports.
    matomo tracking tag settings
  6. Select the new Pageview trigger with the condition Page Title contains 404.
  7. Save the tag.

Test the tracking setup

  1. Open a known broken link to test the Matomo tracking setup.
  2. After clicking the link, open Matomo and go to the Visitors > Real-time and Visit Log reports to check your test pageview correctly recorded the redirect to the 404 page.
    view visits log
  3. Once the reports are processed, go to Behaviour > Pages Titles to view the 404 pages, as well as all referrers leading to this page.
  4. With 404 tracking in place, create a Segment based on the 404 page to see which channels send the most broken traffic, and whether 404 visitors complete goals or drop off immediately.
  5. Build Custom Reports to combine dimensions to understand the impact of 404 errors. You can identify which user groups encounter missing content, spot entry paths or channels lead to broken pages, and identify the links and pages to update.

Analysing 404 data at this level helps prioritise fixes and improve the overall user experience by reducing friction and preventing users from reaching dead ends.

Previous FAQ: How do I set the User ID using Matomo Tag Manager?