Posted on

Simple JavaScript error logging with Analytics

No matter how well you will test your website, some errors will remain. There are just too many OS and browserversion combinations to test them all. Therefore it is important to know which errors your users encounter. This little snippet of code tracks all your JavaScript errors with Google Analytics.

window.onerror = function(error_msg, url, line_number) {
  var error = 'msg: ' + error_msg + '; url: ' + url + '; line:' + line_number;
  _gaq.push(['_trackEvent', 'Error', window.location.pathname, error, 0]);
}

Then, you will see back your errors in Analytics like this.

Analytics error log