用Google Analytics跟踪JavaScript Errors (译)

时间:2023-03-08 16:24:25

通过custom events来实施

// Track basic JavaScript errors
window.addEventListener('error', function(e) {
_gaq.push([
'_trackEvent',
'JavaScript Error',
e.message,
e.filename + ': ' + e.lineno,
true
]);
}); // Track AJAX errors (jQuery API)
$(document).ajaxError(function(e, request, settings) {
_gaq.push([
'_trackEvent',
'Ajax error',
settings.url,
e.result,
true
]);
});

Now when you go into Google Analytics, you can view the custom event information along with other site stats.  Of course you'll tell the marketing people those aren't really error, they're features, but that's another story.  Consider using Google Analytics for to track site errors -- you can thank me later.

来自: http://davidwalsh.name/track-errors-google-analytics