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.
errare 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.
The Web Analytics Association provides the following definition of an 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.
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.
Figure 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.
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:
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 :-).
- Comparison of Google Analytics / Urchin Tracking Scripts
- Improve search engine and keyword reporting in Google Analytics, a SEO strategy
- Google Analytics Event Tracking – New Option to Avoid Impacting Bounce Rate