Tracking Javascript errors with GA

Different ways of tracking javascript errors on the same page.

by kazu69

HTML

<h1>Tracking Javascript errors with GA</h1>
<p><b>Different ways of tracking javascript errors on the same page.</b></p>

<script type="text/javascript">
    /* Traditional snippet of Google Analytics Tracking Code */
    
    var _gaq = _gaq || [];
    _gaq.push(
    ['_setAccount','UA-17169655-2'],
    ['_trackPageview']
    );
    
    (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = 'http://www.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
    })();
</script>

<hr><pre id="error_trycatch"></pre>
<hr><pre id="error_jquery"></pre>
<hr><pre id="error_window"></pre><hr>

<script type="text/javascript">
    try{
        var i = 1/new_variable; // Error 1!
    }
    catch(e){
        _trackJsError(e);
    }
    
    var i = 1/new_variable_2; // Error!
</script>


<p><b>For other suggestions about error tracking, consult:</b></p>
<ul>
    <li>http://jsfiddle.net/phms/D7GPs/</li>
    <li>http://jsfiddle.net/phms/GAyrK/</li>
    <li>http://jsfiddle.net/phms/nWdzR/</li>
    <li>http://jsfiddle.net/phms/tuQdd/</li>
</ul>

JavaScript

function _trackJsError(exception) {
    // http://jsfiddle.net/phms/D7GPs/
    exception = exception || {};
    if (typeof(_gaq) === "object") {
        _gaq.push(['_trackEvent',
             'JS Exception ' + (exception.name || 'Error'),
             (exception.message || exception),
             document.location.href,
             0, true
             ]);
    }

    /* DEBUG */
    document.getElementById("error_trycatch").innerHTML = ('[Try/Catch]<br>JS Exception ' + (exception.name || 'Error') + "<br>Message: " + (exception.message || exception || '(message empty)') + "<br>URL: " + document.location.href);
}


// ******************


jQuery(window).error(function (error) {
    // http://jsfiddle.net/phms/tuQdd/
    if (error && error.originalEvent) {
        error = error.originalEvent;
        var message, url, line;

        // Webkit
        if (error && error.message) {
            message = error.message;
            url = error.filename;
            line = error.lineno;
            // Firefox e IE
        } else {
            message = "(message empty)";
            url = document.location.href;
            line = "?";
        }

        if (typeof(_gaq) === "object") {
            _gaq.push([
                "_trackEvent",
                "JS Exception Error",
                message,
                (url + " (" + line + ")"),
                0, true
                ]);
        }

        /* DEBUG */
        document.getElementById("error_jquery").innerHTML = ("[jQuery]<br>JS Exception Error<br>Message: " + message + "<br>URL: " + url + "<br>Line: " + line);
    }
    
    return true;
});


// ******************


window.onerror = function(message, url, line) {
    http://jsfiddle.net/phms/GAyrK/
    if (typeof(_gaq) === "object") {
        _gaq.push([
            "_trackEvent",
            "JS Exception Error",
            message,
            (url + " (" + line + ")"),
            0, true
            ]);
    }
    
    /* DEBUG */
  ...