google-analytics

Logging JavaScript errors within Google Analytics

Remarks#

The example above has two tracking events, Event Tracking and Exception Tracking.

Event Tracking will allow you to see JS errors in real-time. Under Real Time -> Events sections.

Unfortunately, your error messages will be limited by 500 Bytes, so you will not be able to understand a problem properly, however you will know that something is going wrong.

Exception Tracking will give you more detailed report, with full error message and browser information.

You can generate Exception Tracking report with Custom Reports.

Following code will submit all JavaScript errors into Google Analytics

<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','_watchdog');

_watchdog('create', 'UA-xxxxxxx-x', 'auto');

window.onerror = function(msg) {
  _watchdog('send', 'exception', { 'exDescription': msg });

  _watchdog('send', 'event', {
    eventCategory: 'javascript',
    eventAction: 'error',
    eventLabel: msg,
    transport: 'beacon'
  });
}
</script>

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow