How to track broken links in your website using Google Analytics

Linking between pages in your website is a great way to make it easier to use, for search engines as well as for human visitors. It’s simple to make a link, especially if your website has a Content Management System, and usually means creating a bit of code that includes the ‘address’ of the linked-to page. The address is the bit at the top of your browser that starts with ‘http://’ then has your domain name, eg ‘cicada-online.com’, and then has a part that is specific to the page eg ‘/blog’.

Now, particularly with sites that have been around for some time, it can be beneficial to change a web page’s address. In recent years, the address has become increasingly important to  search engines, and it’s helpful to them if you use something long and descriptive. For example, I know that with Cicada client First Line IT, a number of the page addresses are not as helpful and descriptive as they could be. The difficulty is that if I’ve linked to them from other pages, then changing their address risks breaking the link.

So how do we get round this?


Google Analytics offers a solution which you can read in full here. Here’s the summary:

  • When a visitor to your site clicks on a ‘broken link’ ie they visit a page that doesn’t exisit, your site will report ‘Page not found’. Often the number ‘404’ appears in the error message.
  • By default this won’t be reported to you so you can’t fix it.
  • But you can change your Google Analytics code to report it for you, as follows:
  • Add the Google Analytics tracking code to a custom 404 error page.
  • Modify the tracking code on the 404 page as follows:

<script type=”text/javascript”>
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”>
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>

This code sends a virtual pageview of “/404.html?page=[pagename.html?queryparameter]&from=[referrer]” to your account, where [pagename.html?queryparameters] is the missing page name and referrer is the page URL from where the user reached the 404 page.

Look for /404.html in your Top Content report.

Leave a Reply

Your email address will not be published.