List 404 errors + referral in Google Analytics with the Google Tag Manager

To track every 404 error on your website in Google Analytics you could use GTM in combination with jQuery. When a 404 error occurs we could register an event in GA.

We have to track occurring errors on every page. Therefore, we could create a new Custom HTML tag.

Track Http errors

And the code:

Line 1:   Load jQuery from a Google CDN
Line 6:   Set url to check. {{Page URL}} is a pre-defined variable in GTM.
Line 8:   If there is an error, a function that sets the data layer is initiated.
Line 10: The data layer stores httperror as event, and returns the status code.

To make sure we could use the Page URL built-in variable we have to check it. At a later moment we also need the built-in variable Referrer, so check them all.

enable built in variables google tag manager

To register our 404 error, we have to use a trigger to catch this event.

Create a new trigger based on a Custom Event and the event name httperror:

trigger event httperror

We would like to save the status code (error code 404) and Page url where the error occurs. To get the status code we have to retrieve it from the data layer.

Create a new Data Layer variable and call it Statuscode. The value of this variable gets retrieved from the data layer variable statuscode. Set the Default Value to 200 if no value is set (Standard Status Code).

Statuscode datalayer variable

To track this error as an event in Google Analytics, we have to create a new Google Analytics tag.

I have created a Constant variable with my Google Analytics Trackingcode in it. You will see you are going to use this variable more than once;)

UA-trackingcode standard

As Track type we chose Event. Now some additional fields show up. Give up here the pre-defined variables and the {{Statuscode}} variable. This info will show up in Google Analytics under Behaviour -> Events. Set Non-interaction Hit to True. This will prevent Google Analytics registering a new page view. This is important for measuring your bounce rate on a correct way.

Google Analytics http error tracking

As trigger we use Event – Http error.

Google Analytics http error tracking2

Regarding the Google Analytics data delay (about 4 hours) the data will show up soon. If not, there’s something wrong;)

Was it interesting? Share it, so someone else can read this too!Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

6 thoughts on “List 404 errors + referral in Google Analytics with the Google Tag Manager”

  1. Hi there,

    Nice post :)

    Does this method rely on having GTM ingtegrated on the 404 pages? I m wondering If we could use the same method to track 500 errors, on my website I have GTM on 404s but not on 500s.


  2. Thanks for another informative site. The place else could I get
    that kind of information written in such an ideal manner?
    I have a venture that I am just now operating on, and I’ve been at the glance out for such

  3. Very helpful. Unfortunately it shots down the Magento ajax search functionality. Therefore i had to deactivate it. Any ideas?

Leave a Reply

Your email address will not be published. Required fields are marked *