Use events to track 404 page not found errors in Google Analytics


All web users encounter it sooner or later, the infamous 404 not found error page. Over time pages are removed from sites; incoming links from external sites break, a concept known as link rot, or may be wrong in the first place.

Error iconerrare humanum est (to err is human) – Augustinus Hipponensis

Errors are pretty much inevitable, what is important is that they be managed well. There are some excellent resources on how to make a perfect 404 page (note: if a page is removed, the error should be 410 gone, not 404 not found, but there are few sites that get this right) and server error pages as well. Google even offers some code, along with styling options, to provide users with alternative page suggestions from a site.

Why track error pages in Google Analytics

But what about getting feedback necessary to solve the underlying problem? Enter Google Analytics to the rescue. By default, Google Analytics will only report that a particular error page was displayed, assuming standard tracking code has been installed in the error page. Yet to fix errors, reports need to include the page a user requested and, if applicable, the referring page containing the broken link. This additional information enables webmasters to deploy a missing page, or to get the broken link on a referring page fixed.

Track errors as pages or events?

Historically page not found errors have been tracked as pages in Google Analytics, as directed by the official Google Analytics help topic on the issue. Yet there are problems with Google’s instructions. Despite the notice This article is for the latest version of the tracking code, the documentation is actually for the deprecated 2nd version of the GA tracking code (sites should use the 3rd major iteration, the so-called asynchronous version). Digital media measurement professionals may also recall that the Web Analytics Association site measurement standards say page view counts should not include error pages.

About events

The Web Analytics Association provides the following definition of an event:

Event
any logged or recorded action that has a specific date and time assigned to it by either the browser or server

Event tracking in Google Analytics was announced together with the 2nd version of Google Analytics tracking code in 2007 and released to all users in June 2009. While working on a recent Google Analytics course update, it became clear that page not found errors are a perfect use case for Google Analytics events. Event tracking allows a site to report errors while maintaining clean page view counts.

Error tracking implementation details

Google’s event tracking allows for three levels of textual information (category, action, label), yet there are four error data elements to track – event type, e.g. Error, error code, e.g. 404, the name of the page which triggered the error and the referring page, if any. In the example that follows, the page triggering the error will be lumped together with the referrer. As an alternative implementation, the label “Error” and the error code (404, 410, etc) could be combined in the first event field, category.

<script type='text/javascript'>

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXX-N']);
  _gaq.push(['_trackPageview']);
  _gaq.push(['_trackEvent', 'Error', '404', 'page: ' + document.location.pathname + document.location.search + ' ref: ' + document.referrer ]);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>

Figure 1. Tracking code for the 404 Not found error page.

In the example above:

  • The property tracking code, in blue, should be that of the website being tracked
  • The standard track page call to Google Analytics has been removed (strike-out text). This will prevent the error pages from being tracked as pages.
  • A line of code, in bold text, has been added to track an event. The event category has been set as “Error”, the action as the error code 404 and label will contain the page triggering the error (document.location.pathname + document.location.search) along with referrer information (document.referrer) if available. The labels in green, “Error”, “page:” and “ref:” can be changed as desired.

Errors in Google Analytics event reportFigure 2. Errors in Google Analytics event report. Detail is available for each error type.

Error tracking using include files

For sites which use a standard include file to automatically insert the Google Analytics tracking code in each page, the error event needs to be conditionally tracked based on the server http response status (e.g. 200, 404, 410 etc). The actual code required will depend on the web platform used. The following is a php example which will track page information as an event if the server status is 404 or 410, otherwise a page view is tracked. It can be easily modified to support additional error codes.

<script type="text/javascript"> 

  var _gaq = _gaq || []; 
  _gaq.push(['_setAccount', 'UA-XXXXX-N']); 
  _gaq.push( 
<?php if (stristr($_SERVER['REDIRECT_STATUS'], "404")) { ?> 
     ['_trackEvent', 'Error', '404', 'page: ' + document.location.pathname + document.location.search + ' ref: ' + document.referrer ] 
<?php } elseif (stristr($_SERVER['REDIRECT_STATUS'], "410")) { ?> 
     ['_trackEvent', 'Error', '410', 'page: ' + document.location.pathname + document.location.search + ' ref: ' + document.referrer ] 
<?php } else { ?> 
     ['_trackPageview'] 
<?php 
} 
?> 
  ); 

  (function() { 
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; 
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; 
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); 
  })(); 

</script>

Figure 3. Conditional php Google Analytics error tracking code

Using a Google Analytics plugin for WordPress? Can’t use conditional tracking code?

There may be cases where a site is unable to remove page tracking from an error page, such as when using a WordPress or Joomla plugin to manage Google Analytics options. The recommendation is to add the appropriate error event tracking to the error page, such as a custom 404 page included with a WordPress theme:

<script type="text/javascript"> 
 _gaq.push(['_trackEvent', 'Error', '404', 'page: ' + document.location.pathname + document.location.search + ' ref: ' + document.referrer ]); 
</script>

Figure 4. Event tracking code for a 404 error page, supplementing standard page tracking code

Once this is done, set up an exclude data filter for a profile to remove the page views tracked for the error page. Now all that’s left to do is check code is working and monitor and fix errors, should there be any :-).

Similar Posts:

Registration is now open for the next SEO Course and Google Analytics Course in Milan. Don’t miss the opportunity!


About Sean Carlos

Sean Carlos is a digital marketing consultant & teacher, assisting companies with their Search (SEO + SEA = SEM), Social Media & Digital Media Analytics strategies. Sean first worked with text indexing in 1990 in a project for the Los Angeles County Museum of Art. Since then he worked for Hewlett-Packard Consulting and later as IT Manager of a real estate website before founding Antezeta in 2006. Sean is an official instructor of the Digital Analytics Association and collaborates with the Bocconi University. He is Chairman of the SMX Search and Social Media Conference, 13 & 14 November in Milan. He is also a co-author of the Treccani encyclopedic dictionary of computer science, ICT & digital media. Born in Providence, RI, USA, Sean received Honors in Physics from Bates College, Maine. He speaks English, Italian and German.

5 Responses to "Use events to track 404 page not found errors in Google Analytics"