Clean Up 404s with Google Analytics

I’ve been on a 404 bender of late.

It all began with a server migration and improving the way 404s were rendered. While poking around the Web, I happened across a promising Google Analytics tip on tracking these evasive creatures. So I captured the code, pasted it in, and a day later looked at the results.

Nada. I then looked at the code more closely and realized it was the old format, which one usually puts in the footer. No problem. But a day later still nothing.

Now it was a challenge and I was determined. I found one blog post using newer code format, but one part of the JavaScript looked off. Given that I’m no JS ninja, I tried it. It turned out to be a step in the right direction, but sure enough — that one bit of JS was indeed off. All of the results were the same. “404.html?p=/page_not_found_uri/”.

More scrambling…. and somewhere along the way, buried in a forum as I recall, I found some code that looked right. Jackpot. Now when I check our Google Analytics Top Content and enter /404.html in the page filter at the bottom I get a lovely tidy list of 404s, including their source.

To save others who might be on the same quest, here’s the code to use in your 404 html pages. Just replace “UA-xxxxxxx-x” with your account number. Then get out your virtual feather duster and start cleaning out those cobwebs.

<script type="text/javascript">
google.load("jquery", "1.4.2");
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxxx-x']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);