As I wrote in my previous article, 404 error page should be at least informative (visitor to understand it's an error page) and useful (visitor to has many links where to go to). But this is not the final task. To my mind main purpose of 404 error page is to track where does the 404 error come from. If it's a broken link on the same site why we should keep saying "Sorry, this page doesn't exist"? Let's try to find and fix it!
Very good option is to use Google Analytics (for those sites which are registered with GA, of cause). Thanks to Google, they published a solution. But, first of all it contains small errors if is just copy-pasted! These are whitespaces on line 2 in " https://ssl." and line 3 " google-analytics.com/ga.js'. The code I give below is cleaned from that errors ("UA-xxxxx-x" has to be replaced with site's GA code).
<script type="text/javascript">// <![CDATA[
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"));
// ]]></script>
<script type="text/javascript">// <![CDATA[
try{
var pageTracker = _gat._getTracker("UA-xxxxx-x");
pageTracker._trackPageview("/404.html?page=" + document.location.pathname + document.location.search + "&from=" + document.referrer);
} catch(err) {}
// ]]></script>
To see the errors, you would have to filter for"404.html" page in your Top Content report in GA. This is a "virtual" page created by the above piece of code in order to be included in the GA statistics. Exactly that was an issue I didn't like.
I didn't want an error page to be included in the visit statistics with all other "real" web-pages. I decided to replace trackPageview with Event Tracking method.
By doing this I do not mess visits to real page with visit to 404 error page. Still I receive the same information on the referring page. And if it's from my site, I can go to it and open the error. That's all!
Please go to the link www.justphp.net/test-error to see an example of custom 404 error page.
Also you could download
the simple 404 error page (3.2 kB). Do not forget to insert your site's Google Analytics code (ID) for error tracking.

