Unleash the Power of Google Analytics (6): 404 Tracking

One of the most useful ways of using Google Analytics to identify potential problems on your website is by implementing 404 tracking. A 404 page indicates that your server could not find the resource requested by your visitor, but only tracking can tell you why.

Analytics deployments often overlook this simple, yet effective method of identifying broken links (even those off site), along with missing pages and lost opportunities. But by adding a very small piece of custom code, you can reveal a wealth of information about the 404 pages that your users are seeing.

1) Add the custom tracking code to the 404 page template

Assuming that the 404 pages on your site are currently missing the Google Analytics Tracking Code (GATC), add the following code to the page – this is the standard ga.js code, with an amended line, indicated in bold.

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-xxxxxxx-x");
pageTracker._trackPageview("/404.html?page=" + document.location.pathname + document.location.search + "&from=" + document.referrer);} catch(err) {}</script>


The code sends a virtual page view of "/404.html?page=[pagename.html?queryparameter]&from=[referrer]" to Analytics, where pagename.html?queryparameters shows the missing page name, and referrer represents the page URL the user came from before they landed on your 404 page.

2) Take a look at the incoming data

Log in to your Google Analytics control panel and look for /404.html in the Top Content report. This will display 404 page statistics, along with the locations of the missing pages and the referring information.

Once the code has been collecting 404 visit data for a while, you should see entries that resemble the following appear in the top content report.

In this example, the /404.html serves as an identifier so that we can see that the pages are 404s (and filter them using the ‘filter page’ feature in Analytics). Next, the page= section shows the address that the 404 appeared at – in this case a gif image. Finally, the from= section shows the source of the traffic – this time it was a referring site called ‘mybrowserbar.com’.

This information is valuable in the following situations.

The majority of 404s (except for those that are the result of mistyped URLs) can prevented by using one of the following methods, depending on the reason for the 404 status.

Top tip: Set up filters to segment 404 tracking into a profile of its own – this way, it is clear to see from the Analytics overview page if there has been a significant, recent increase in 404 errors.