Send Javascript client errors to Google Analytics

Not sure how well this will catch errors before onready but as a quick fix seems to work okay.

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-19303815-1']);
  _gaq.push(['_setDomainName', '.etherpad.org']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

  /* Google Analytics error catching */
  window.onerror = function(message, file, lineNumber) {
    _gaq.push([
     '_trackEvent',
     'error',
     file + ':' + lineNumber,
     message + ''
    ]);
  };

Don’t forget to replace the UA-part with your own code…

One thought on “Send Javascript client errors to Google Analytics

Leave a Reply

Your email address will not be published. Required fields are marked *