Tracking error pages from WordPress

GA Hacks 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:


Error 404 - Not Found

Modifying it to this (assuming you are using ga.js in your pages):


Error 404 - Not Found

allows you to to view the URLs of missing pages in the Google Analytics Content > Top Content report (see screenshot).

Google Analytics Top Content report

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 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.

Looking for a keynote speaker, or wish to hire Brian…?

If you are an organisation wishing to hire me and my team, please view the Contact page. I am based in Sweden and advise organisations in Europe as well as North America.

You May Also Like…


  1. Suresh

    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.

  2. Brian Clifton

    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.

  3. Jeffrey Staub

    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 + “’ type=’text/javascript’%3E%3C/script%3E”));

    var pageTracker = _gat._getTracker(“UA-xxxxxx-x”);
    pageTracker._trackPageview(“/404/?page-not-found=” + document.location.pathname + + “&page-linked-from=” + document.referrer);

    Great book by the way!

  4. Brian Clifton

    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.

  5. Brian Clifton

    A twist on the same theme… this is an alternative script using variables available in the DOM rather than the GATC:

    Wordpress error code alternative

    [Remember to change the value of 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.

  6. Joost de Valk

    Next to that you’d probably want to improve on that 404 page a lot… Look at mine, for example…


Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share This