Tracking error pages from Wordpress
Google Analytics Hacks, Google Analytics specific June 2nd, 2008
My standard word of caution for all “GA Hacks” posts – This is a tech tip and requires you to have a knowledge of html and javascript to implement and use it…
Tracking error pages is something that page tag solutions cannot track out of the box – including Google Analytics. In Chapter 9 of the book I describe how to track all your error pages using Google Analytics. Essentially, you modify the server error template to include the GATC, then use an advanced filter to rewrite the URL string.
However, if you are a Wordpress user, there is a simpler method than tinkering with your web server…
Wordpress includes a template file called "404 template" (404.php). This is the file used to display an error message if a page is not found. By default it does not contain very much:
<?php get_header(); ?>
<div id="content">
<h2>Error 404 - Not Found</h2>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Modifying it to this (assuming you are using ga.js in your pages):
<?php get_header(); ?>
<div id="content">
<h2>Error 404 - Not Found</h2>
<script type="text/javascript">
pageTracker._trackPageview("/missing pages" + _udl.pathname + _udl.search)
document.write("<p>The page: " +_udl.pathname+_udl.search+ " is not found</p>")
</script>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
allows you to to view the URLs of missing pages in the Google Analytics Content > Top Content report (see screenshot).

As a tip, use the inline filter as shown to bubble up these pages – otherwise they could be buried deep in your reports. Once you have identified the missing URLs, click through on the pages and then select "Navigation Summary". This tells you which links within your website point to these missing pages – that is, have broken links.
The code _udl.pathname and _udl.search are Google Analytics variables captured by the GATC, so it is important that the GATC is loaded first.
Tip: if you are still using the legacy urchin.js page tag (why?), subsitute the string urchinTracker for pageTracker._trackPageview in the above functions.
Obviously there is more than one way to skin a cat. Please let me know if you have used a different hack to acheive this by leaving a comment.
---Related posts:



(7 votes, average: 4.71 out of 5)


June 2nd, 2008 at 10:07 am
Next to that you’d probably want to improve on that 404 page a lot… Look at mine, for example…
June 2nd, 2008 at 10:14 am
A twist on the same theme… this is an alternative script using variables available in the DOM rather than the GATC:
[Remember to change the value of domain.com in the script to your real domain name].
I prefer this method as it not effected by any changes that may take place for GATC.
June 2nd, 2008 at 7:50 pm
Joost: That’s a very good point. Having a clear messeage as to what the error is, plus presenting options to the visitor so they have an alternative is a great way to recover the user experience session.
June 18th, 2008 at 2:25 pm
Good Stuff. I have also found that it is great to know the referring page that led to the 404 so that you can trouble shoot the issue. The use of the document.referrer variable works. I use the following code which could be modified:
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”));
var pageTracker = _gat._getTracker(”UA-xxxxxx-x”);
pageTracker._initData();
pageTracker._trackPageview(”/404/?page-not-found=” + document.location.pathname + document.location.search + “&page-linked-from=” + document.referrer);
Great book by the way!
June 19th, 2008 at 1:30 pm
Jeffery: That’s a good point, though you can also see this by clicking through on the page link in question in your reports and viewing the Navigation Summary report.
Thaks for the feedback.
August 6th, 2008 at 5:23 am
I was missing that tracking error page on my site, but after reading this useful post and implementing to track error page in wordpress script, now I am able to track easily. Thanks for sharing this awesome post.