Tracking 404 Error in Google tag Manager (GTM)

google tag manager to track 404 error

A 404 error, often referred to as “Page Not Found,” is an HTTP response code indicating that the server could not locate the requested webpage. When a user attempts to access a URL that is either misspelled, no longer exists, or has been moved without proper redirection, the server responds with a 404 error.

Ensuring a seamless user experience on your website involves not only monitoring successful interactions but also identifying and addressing issues, such as 404 errors. Google Tag Manager (GTM) offers a convenient way to track and gain insights into these errors. Here’s a step-by-step guide on setting up 404 error tracking using GTM:

To set up this tracking, firstly you need to set a few things-

  • Setting up GA4on your website
  • The base GA4 tag needs to be installed on your website
  • Setup and install Google Tag Manager need to be installed on your website
  • Install all the Important plugins on your website for analytics

Creating Custom Variable

To get the title from any website you need to create a custom variable that will pull the title from the webpage

  • For that in Google Tag Manager
  • Go to Variable Section
  • Go to the bottom
  • Create a new User-defined variable
  • Select the Variable configuration
  • Then click on JavaScript Variable
  • In the Global variable name write document.title
  • Give the name
  • Save

Creating Tag

Firstly you need to create a tag that will send the data to Google Analytics 4

  • Go to Tag
  • Create a new Tag
  • Select Tag Configuration
  • Select Google Analytics GA4: Event
  • Put the GA4 Measurement ID
  • Choose the recommended event name such as 404 Error
  • Go to User Property
  • Write the property name as page URL and Value as {{Page URL}}
  • Add another row
  • Write property name as Referral and value as {{Referral}}
  • Save

Creating Trigger

Now we need to create the trigger that fire only on the 404 header

  • Go to the trigger section
  • Create a new trigger
  • Now select the Page View trigger
  • And click on some page view
  • Select the trigger configuration as `Document Title Contains Page not Found`
  • Save
creating trigger for 404

another option that you can try is to use the custom event trigger

  • go to the Trigger section
  • create a new trigger
  • now select the Custom Event Trigger
  • put the Event name as page not found
  • and select this trigger fire on All custom event
  • Save

Now you can test your tag by going to the Preview mode or going to Debug mode on your GA4

Do not forget to Publish your workspace and save all your tags and trigger