404 snippets

Tracking 404 and 500 with Google Analytics

Tagged track, 500, 404, google, analytics  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 %>

Custom 404 Rails error pages

Tagged custom, error, 404, rails  Languages ruby

All credit for this snippet is due to Henrik Nyh.

Put this in application_controller.rb:

alias_method :rescue_action_locally, :rescue_action_in_public if RAILS_ENV == 'development'
  
  def render_optional_error_file(status_code)
    if status_code == :not_found
      activate_authlogic
      render_404
    else
      super
    end
  end

  def render_404
    respond_to do |type| 
      type.html { render :template => "errors/error_404", :layout => 'application', :status => 404 } 
      type.all  { render :nothing => true, :status => 404 } 
    end

    true  # so we can do "render_404 and return"
  end