The Best Way to Track "Not Found" 404 Errors in a WordPress Site

apache404

So, you want to know about every 404 on your site.

There are plugins that do this, but they all suffer from one fatal problem. Plugins such as these all fail:

The Problem with These Plugins

They fail because tracking pageviews is not easy. Web pages are not only accessed by human users. They are also accessed by junk machines, such as spam bots.

But you only want to know about 404 errors from visitors that matter — namely: humans and reputable search engines. You don’t care when a spam bot tries and fails to access http://your-site.com/non-existent-link-that-spam-bot-stupidly-constructed/. The plugins above will report 100s of these junk records, which will clutter your 404 log — making it harder for you to notice real 404s.

The 404 Tracking Method That Works

On the other hand, Google Analytics knows who’s junk and who’s real. You can track 404s in Analytics and it’ll give you a report like this:

404_errors

This report tells you the exact landing 404 page and who the referrer was — so it’s pretty useful.

How to Get Google Analytics to Track Your 404 Errors

The easiest way I know for getting Analytics to track your 404s is to use the Google Analytics for WordPress plugin. It has this feature built in. Simply install and activate and that’s it. 404 errors will start showing in your Analytics reports like the screenshot above.

Separate 404 Errors for Better Noticeability

As you can see in the screenshot above, 404 errors will be reported as pageviews in Analytics. You’ll want to separate 404 errors from the rest of your content for better noticeability. Here’s how:

  1. Go to Analytics Top Content page.
  2. Click Advanced Filter
  3. Select Page and Containing, from the drop down lists if they’re not selected by default 
  4. Enter /404\.html in the text box
  5. Click Apply Filter

In front of you then will be a list of all 404 errors. Now click on Add to Dashboard at the top so that you can reach this report with a single click from Analytics dashboard.

Note: if you don’t want to use Google Analytics for WordPress plugin for whatever reason, there are other ways to track 404 errors with Analytics. Ask in the comments and I’ll cover some of those other ways.

“That’s for humans, how about search engines?”

The method above tracks 404s that human visitors encounter — not search engines. The best way to track search engine 404 errors is to use Google Webmasters Central. Go to the Crawl errors page and there you’ll see a list of all 404 errors that the Google bot encountered.

web-crawl-404-errors-click-source-highlighted

WordPress Can Mess with Your 404 Errors — Tell It to Stop!

This bit isn’t essential to tracking 404 errors, but it’s very important — especially if you use WordPress as a CMS with plugins that customize URLs, such as Custom Permalinks.

If your WordPress site has a link like http://your-blog.com/your-category/your-post/, and a user mistakenly tries to access http://your-blog.com/typo/your-post/ before serving a 404 page, WordPress will try to guess what the user was looking for and redirect the user. This can lead to problems.

For example, when writing a post, you might make a typo in a hyperlink. Now it leads your visitors to a 404 page. But instead of logging a 404 error that you can notice and fix, WordPress will try to guess a valid destination for the URL and redirect visitors to it. The destination won’t necessarily be the destination you intended — ouch!

To tell WordPress to not do that, paste the following code in your theme’s functions.php file:

function stop_404_guessing($url) { if (is_404()) return false; return $url; }
add_filter('redirect_canonical', 'stop_404_guessing');

12 comments

  1. I am confused, where the code is placed? after open the functions.php?

  2. Jose

    I follow this instructions and now I get the message
    Parse error: syntax error, unexpected ‘{‘ in /home/content/82/7996882/html/wp-content/themes/dejavu/functions.php on line 21
    And I don’t have any idea how to fix the problem, Please I need some help

    • The code in the post works perfectly. If you’ve copied it correctly, then it is not what’s causing your problem. Your problem maybe related to another piece of code in your functions.php file.

  3. Great info. All blog owners should be doing this. Well at least the ones who care about offering a great user experience.

  4. Yoast is a real WordPress rockstar, his plugins don’t just work they kick ass and take names.

    Thanks for that 404 code too, I already implemented it for my blog.

  5. If have 404 errors, you are very likely losing Google juice (in addition to providing a bad experience for the user). Very important to track and correct these errors!

  6. Good tips. I still like using GA to find 404′s. Use the Content >> Content By Title report and filtering for your 404 page name. Example: “page not found”. IF you’re in a situation where there are tons of attempted visits to dead URL’s you immediately have a priority list. Great for link clean-up from referral sites or even monitoring your marketing campaign channels; /email/, etc.

  7. Les

    Great post – BTW. And I agree 100% about Yoast’s plugin!

    I would also add that it is good practice to clean up 404 Errors that one finds in Google Webmaster tools as Google will view these negatively in a site’s rankings.

    GA = Human 404 errors
    Google WMT = 404 Crawl errors

    Excellent!

  8. I have been using the Google Analytics for WordPress plugin for many months now, and it seems that I learn something new about it everyday. That is one powerful plugin for helping track traffic on your blog. It just extends the capability of Google’s Analytics.

  9. Currently use this method, like your extension of adding it to the dashboard I never thought of that.

    I like to have add source when looking the urls in your list as it really shows where the error comes from, internal external, referes or google. Useful to work out what to spend time fixing and where to apply that time.

    Thanks

  10. Just wanted to pop in and say thanks for the post. It’s a good one and surprisingly provides a ton of value. With so much content farming going on, I’d have to say that this is a good post. Thanks, and yes, I’ll be using this method to track 404′s to my WP blog.

Leave a comment

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

*

Read this to make your comment cool.