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.
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.
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.
<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:
- Comparison of Google Analytics / Urchin Tracking Scripts
- Google Analytics Event Tracking – New Option to Avoid Impacting Bounce Rate
- Web Analytics Embedded JavaScript Page Tracking Code: Place at the top or bottom of the page?
- Web Analytics: Embedded JavaScript Page Tracking Code vs. Web Server Log Files
- Web Analytics and the Missing Right Clicks Conundrum
Registration is now open for the next SEO Course and Google Analytics Course in Milan. Don’t miss the opportunity!
errare humanum est (to err is human) – Augustinus Hipponensis
By Arnaud Lachaise 2011-05-20 - 12:34:44
An example of the best 404 pages of the web: http://www.404notfound.fr
By Sean Carlos 2011-05-22 - 8:45:02
Nice examaple, merci Arnaud.
By Matthew Hunt @ Small Business Online Coach 2012-11-20 - 23:40:54
Sean, wicked post! I am sending this to my team now. thanks!
By Alvaro 2013-01-04 - 18:59:48
Hi Adrian
In my case, document.location.pathname shows the URL of the 404 ErrorDocument itself, and not the URL requested by the user. (The referrer is shown correctly). This makes it impossible to find out which URLs were actually requested.
I have not found the bug yet. Hard to google this also, as just this solution shows up.
At first I thought it has to do with mod_rewrite “pretty” URLs (my ErrorDocument is error/404 instead of error_404.php or something similar. Then I rewrite this to error_404.php. However, also without rewriting, document.location.pathname seems to show the ErrorDocument URL instead of the actually requested (wrong) URL.
Do you have any ideas or hints?
By Angie Schottmuller (@aschottmuller) 2013-02-25 - 18:35:20
Hi Sean! Handy post! I’m curious as to why you bundled both URL and referrer into one event parameter. Is there a particular benefit to not splitting them like the example below?
_gaq.push(['_trackEvent', 'Error 404', 'page: ' + document.location.pathname + document.location.search, ' ref: ' + document.referrer ]);