Tracking 404 and 500 with Google Analytics

Tagged 404, 500, google, analytics, track  Languages javascript

New Google Analytics

_gaq.push(['_trackEvent', 'HTTP status', '404', '/xxx/what-a-fish']);

Old Google Analytics

Tracking 404 and 500 errors with Google Analytics is documented here, but I tend to forget so I'm putting the information here:

// 404
pageTracker._trackPageview("/404.html?page=" + document.location.pathname + document.location.search + "&from=" + document.referrer);

// 500
pageTracker._trackPageview("/500.html?page=" + document.location.pathname + document.location.search + "&from=" + document.referrer);

Rails

In Rails use the response status code to track any HTTP errors:

<% if response.status != 200 %>
_gaq.push(['_trackEvent', 'HTTP status', '<%= response.status %>', '<%= request.fullpath %>']);
<% end %>