Access 404 Error Metrics Using Google Tag Manager

/

404-Error-Blog

As analysts and marketers, we always want to track positive performance metrics and conversions in Google Analytics. However, tracking errors is also important to monitor the health of your site and keep track of signals indicating a negative user experience.

Accessing this data gives us a better idea of what’s causing users to get lost and wander into the dark, unattached voids of your domain. Knowing where these problem spots are makes it easier to fix internal links or set redirects.

I’ll show you different ways to view where people are hitting these error pages and where they are coming from, either through your existing setup or by using Google Tag Manager to fire events or virtual pageviews.

404 – User Not Impressed

As a web user, there’s a good chance that you’ve been acquainted with the ominous “404 Page Not Found” error. They come in many flavors, sometimes with illustrations, sometimes with a site search to help find what you were looking for and sometimes it is just a white page with unfeeling black, bold letters.

Some causes of 404 errors come from

  • A manually mis-typed URL path
  • Third-party sites linking to nonexistent or removed pages
  • Old links from social media platforms (scroll down your Facebook page to 2006, some links you shared might not be valid anymore!)
  • Errors with internal links

No matter how cute or whimsical the page is, a ‘page not found’ error disrupts the user experience. These 404 pages are not actual pages on a site – they are a result of a status code response on the server side and can be thought of as an alert rather than a fixed page. So how can you know if visitors to your site are experiencing these errors?

Easy, Existing Options

In Google Analytics, you may already see the page path that the user attempted to access. If it is an old article that doesn’t exist anymore, the page may show up as something like /2003/04/12/article-title/. Seeing that single pageview in your reports may be your only indication that the page doesn’t exist. Possibly, the page title will give you some indication as well.

If you’re able to determine that a page is an error, either by the page path or title, you could set up 404 errors as goals in Google Analytics. This has been common practice in the past because of the ability to see the funnel a user took before getting to the destination page.

However, goals are best suited for key performance indicators, or KPIs of your site. Also, keep in mind that there is a limit on non-Premium accounts of 20 goals per view.

Another approach to track 404 response pages is to take advantage of Google’s Webmaster Tools. It can show you what is linking to the missing pages and graph the volume of errors over time. Some limitations are that the errors logged are from the Googlebot crawler (not necessarily viewed by users), you can’t see how it affects users’ overall sessions, and you can’t include it in your analytics reporting.

Using Google Tag Manager

If you don’t have this information in Google Analytics or Google Webmaster Tools, we can track 404 errors as events or virtual pageviews with Google Tag Manager. These examples assume basic pageview tracking is set up and the code for the Tag Manager container is on the 404 page template.

Page Title or Page Header

Sometimes, the easiest solution is to look for something consistent on the page that identifies it as an error.

Typically, an error page template will have a page title that does not change. For example, Google’s 404 pages have the title, ‘Error 404 (Not Found)!!1.’

To target the title of this page, we would first create a custom JavaScript macro to represent the page title. The page title can be captured by using “document.title”, which is supported in all major browsers.

PageTitleMacro

Then, our firing rule for the 404 response event tag would simply be {{page title}} equals “Error 404 (Not Found)!!1.”

PageTitleRule

One thing to keep in mind when making this rule is that the field is case-sensitive. Also, if the page starts with something like “Page Not Found” and dynamically adds the path that the user tried to access (“Page Not Found – /notapage”), using “starts with” or a regular expression would be the alternative to “equals.”

This method can be used for an even more complicated scenario where the page title is just the URL or path that the user attempted to access- “/notapage.” This isn’t an ideal situation, but in this instance we could target the header element instead of the page title. For a custom {{header}} macro, you could use jQuery

…or JavaScript.

The firing rule would then be {{header}} equals “Not Found.”

Fire an Event

Now that we’ve identified the 404 Error Page, we can fire an event to let us know some more information about the error. The benefit here is that it gives us a really easy to view metric inside of Google Analytics, as well as the ability to include any information we like.

Below, the label {{referrer}} is a custom macro that can be made easily in Tag Manager by selecting “HTTP Referrer” as the macro type.

404EventTag

ReferrerMacro

Now we know A) that an error occurred by a user and B) how they reached the page. We can fix our site with a redirect and, if possible, remove the offending link to our outdated link.

Adding Code to the Page

One method we use frequently is to send a Google Analytics Event from Google Tag Manager using an “event” that is added to the data layer. When Tag Manager sees this event on the data layer, it triggers a rule, which we attach to the GA Event Tag.

You will likely need cooperation from a developer or the department in charge of maintaining the website since this part involves adding lines of code onto the 404 page template.

The data layer is an object that goes before the container snippet and simply makes variables and events available to Google Analytics and Tag Manager.

Below is the code that we would use for a 404 event:

We can use Data Layer Variable macros to pull the event category, action and label from the data Layer.

DataLayerVariable_EventAction

Next, we would create a firing rule of {{event}} equals ‘404error’.

404dataLayerEvent

We can use this rule to cause a Google Analytics Event tag to fire, much like the one displayed earlier. Now anytime there is a pageview hit on a 404 response page, an event is triggered and sent to Google Analytics.

Using Virtual Pageviews

Another way to track those lost souls wandering your site is to use virtual pageviews with Google Tag Manager. One benefit to tracking hits this way is the ability to look at the data in the content drilldown under the Behavior reports, or to use this virtual pageview in a Goal Funnel like previously mentioned.

To accomplish this, create a pageview tag instead of an event tag. Under “More Settings” set your document path to begin with 404/ so it will be apparent when analyzing the page data later in Google Analytics.

404VirtualPageview

For the firing rule, you can use any of the rules and methods we mentioned earlier either by using the data layer or targeting the page title or header.

Just make sure to re-use the rule as a blocking rule on your default pageview tag so that your data isn’t polluted by duplicate hits.

BlockingRule

Set an alert

Finally, it’s a good practice to set up alerts for errors as well (in the Admin tab under “Custom Alerts”). For the “Alert Conditions”, you will either use the Event Category pictured below, or Page with the condition of “starts with” and value of “404.” This will send you automated emails when pageviews on error pages goes above whatever threshold you specify.

AlertConditinons

This data will not only allow you to keep track of 404 errors, but it will also give you the ability to analyze why they are happening with the referring URLs, and the page path to implement redirects. Redirecting lost users to content will improve the user’s visit and potentially create a converting visitor who may have left at the sight of an error!

Samantha is a Senior Analytics Consultant at LunaMetrics. She has a passion for exploring data and loves the excitement of finding solutions and explanations behind metrics. Her background includes advertising, SEO, and analytics as well as involvement in the arts. When she isn't logged into Google Analytics, she can be found traveling, talking about food or playing games.

  • Stefim

    Great article, but I do have one question though. Would using ‘contains’ instead of ‘equals’ in “{{page title}} equals “Error 404 (Not Found)!!1” also work?

    • Samantha Barnes

      Yes, ‘contains’ would work! You would just have to make sure that no other page title contains the same string. The only situation I can think of would be this one- a blog post about 404s.

      For example, if I tracked a 404 Page by setting the rule to fire on {{page title}} contains ‘404 error’, pageviews on a blog page with the title “Access 404 Error Metrics Using Google Tag Manager” would track as a 404 event.

  • http://www.ganalytics.nl Gerard

    Question: should you create 2 Google tags:

    1. Pageview tag to track 404 pages with the macro pagetitle=page not found;

    2. Event tag to track the 404 pages as an event, with the data layer?

  • Samantha Barnes

    You only need to choose one method of tracking your 404 pages. Between those two, I would say go with the data layer!

  • http://cdz@doginfog.ca Christopher

    @Samantha,

    “Data Layer Variable macros to pull the event category, action and label from the data Layer.”

    Would I have to create a macro for Category,Label,event if I wanted to use those or by doing just the one macro called “event action” allows me to pull the rest?

    In your example you reference the datalayer variable name “action” and in the next screen use “{{event}} equals 404error”

    Wouldn’t the datalayer variable need to be “event” vs “action”? As 404error is listed an event?

    Thanks 🙂 maybe I just need to brush up more on my understanding of datalayer but hope you can share some insight.

  • Samantha Barnes

    Hi Christopher,

    The event category, action and label are all variables that will specify what you see in Google Analytics. You don’t have to specify them in the data layer since you can also use the fields in the event tag.

    The {{event}} macro is used to fire the tag, so we will not actually see “404error” in the GA interface.

    In my example, the first three data layer variables (which correspond to the macros) are for labelling purposes in Google Analytics while the last is for the firing rule.

    dataLayer = [{
    ‘category’: ‘404 Response’,
    ‘action’: url,
    ‘label’: document.referrer,
    ‘event’: ‘404error’
    }]

    Hope this helps!

  • http://www.facebook.com/joomlashine Tam Ngo Le

    Hello Samantha, Thank you so much for an amazing article. But I’m working with a new version of GTM so some steps I do not understand 🙁 Could you please update your article with the new version? I will appreciate it much.

  • disqus_OHHGntxVmN

    Hi,

    Can I just check that I understand the process correctly. There are two methods:

    1) Events via GTM

    2) Virtual PageViews via GTM

    Within 1) Events via GTM, there are two possible ways to implement this. The first is in the ‘Page Title or Page Header’ and ‘Fire and Event’ section.

    The second method is in the ‘Adding Code to the page’ section.

    Is my understanding correct in that to implement this method via events you can either implement entirely within GTM (‘Page Title or Page Header’ and ‘Fire and Event’ section) OR via the dataLayer (‘Adding Code to the page’ section).

    Or have I misunderstood and to implement the event method you have to follow the steps in all three sections (‘Page Title or Page Header’, ‘Fire and Event’ and, ‘Adding Code to the page’).

    I’m just a little unclear which are the implementation choices and which are the steps.

    Many thanks

    J

  • http://www.rickrduncan.com Rick R. Duncan

    Very informative article Samantha. I found your article when searching for a method to monitor my 404 errors on a website redesign project using GTM v2. The code you wrote for v1 helped me create a solution using v2. If anyone is interested in seeing how to track 404 errors using GTM v2 it can be found here.

    http://www.rickrduncan.com/analytics/404-error-tracking-gtm-ga

    Thanks,
    Rick

  • anonymoose

    Why not just fire an event on the page load of a custom 404 page? Seems much easier than dealing with the macros.

  • Karuna Sharma

    I have purchased a template website added graphics and content and uploaded it still see the irk as 404 page not found.
    What specific page or pages I need to go through to detect this and figure out to have client review page via a link.
    I have tried as much and I need support, would greatly appreciate as my bread and butter depends on it.

Contact Us.

LunaMetrics

24 S. 18th Street, Suite 100,
Pittsburgh, PA 15203

Follow Us

1.877.220.LUNA

1.412.381.5500

getinfo@lunametrics.com

Questions?
We'll get back to you
in ONE business day.