Gathering Google Analytics Data for Magento 404 Not Founds

Magento by default gives you a configuration variable that allows you to set which CMS page is used for 404 Not Found requests.  This is an awesome way to provide useful information to a visitor who can’t find what they are looking for or even for old/broken URLs.

Recently we were looking through the analytics for a few of our sites and we realized that we don’t even know how often people are hitting 404’s.  Not only how many 404’s, but what URLs were they requesting that caused them to hit a 404?

To solve this, we extended Analytics module in the Magento core to provide a 301 redirect to our actual 404 CMS Page, instead of letting Magento simply load the 404 page with the requested URL as the pageview.

(We actually packaged this up as an extension and are giving it away for free on Magento Connect.  Enjoy!)

What we’re doing is extending the /app/code/core/Mage/GoogleAnalytics/Block/Ga.php and adding in code that looks like this (click for full size):

Note: We do a 301 redirect  as a catch-all, not a best practice.  If you find broken links on your site you should be doing 301’s to appropriate pages that have relevant content compared to the original, now non-existent page.

Analytics Before:

You can see in the below shot that there is no nice way to query for all not-found (404) links that people are hitting your site with.  For the purposes of this test, I used a common “idontexist” prefix on each bad URL I put into the site.

Analytics After:

After we install our extension, this is what our Google Analytics looks like.  We can simply filter all of our page views by 404.html and see a nice list of Pageviews that resulted in 404’s.  We can also see which original page they were actually looking for.

As you can see, we can now measure our 404’s and gather insight into what these lost users are looking for!