Everyone would like to know if their users are seeing error pages due to broken links (404 not found) or server side errors (500 server error). Here’s how you can use Google Analytics and the relatively new Google Analytics Event Tracking API to track these errors.
There’s some brief advice on using this technique with WordPress, Rails or Apache at the bottom of this post.
Prerequisite – ga.js Script
Prerequisite, I’m assuming you’ve got Google Analytics set up for your site. If you did this some time ago, you might still be using the old urchin tracking script. If you are you’ll need to update to the newer ga.js script. If you know you are using ga.js, then feel free to move along to the next section.
If you are not sure, take a look at the source of one of your pages. If it looks something like the snippet below, saying urchinTracker() then you are using the old urchin tracking script and will need to upgrade:
Here are Google’s instructions for upgrading from the older urchin to the newer ga.js script.
If you are already using ga.js your pages should contain something similar to this:
Event Tracking Script
The event tracking script itself is very simple, a single line of javascript will log an event with Google Analytics:
The first parameter ’404′ is our new event category, the second parameter is the URL that has not been found, and the third parameter is the URL of the page that referred the visit.
This line should be added to your Google Analytics tracking script on your 404 Not Found error page, for example:
For 500 Server Errors it is very similar to the above but change the ’404′ to ’500′. Your tracking script becomes something like:
Viewing 404 and 500 Events in Google Analytics
Google Analytics Events are listed under Content. To see your new 404 and 500 errors, select Content / Event Tracking / Categories in the Google Analytics sidebar. You’ll see 404 and 500 listed as Categories once there have been 404 or 500 events. You can view details of these events by clicking on a particular category.
Here’s a screenshot of 404 events on one of my sites – I’ve deliberately created some 404s by pointing my browser at random-ish urls on my site’s domain:

Enjoy!
Paul
404 and 500 tracking on Apache
If you are using Apache and haven’t already specified a 404 error page you can do so by adding the following to either your .htaccess file or inside your site’s virtual host element:
Replace /path/to/your/404.html with the path to your 404 error page. The 404 error page is just a plain old html page.
For 500 errors, you’ll need to specify a 500 error page in either your .htaccess file or inside your site’s virtual host element just like we did for 404 errors:
404 and 500 tracking on Ruby on Rails
Ruby on Rails users will find you already have 404.html and 500.html files inside your public directory. You can add the GA tracking script there.
404 and 500 tracking on WordPress
Wordpress users can add the following lines to their theme’s footer.php inside the Google Analytics tracking script:
So for example, your footer.php should look something like: