I have the following code which is supposed to send a notification on ajax error to the user, and as a snap-solution, I decided to use google analytics to log those errors, however it doesn’t work, does anyone have an idea/lead or must I implement a different solution?
showNotification(title, message) is a custom dialog box invoker.
$(document).ajaxError(function(e, xhr, settings) {
$(".modal").modal('hide');
showNotification('Something went wrong...', 'Whoa there! Something happened which we didn\'t predict, we sent ourselves a notification. ');
_gaq.push(['_trackEvent', 'AjaxError', xhr.status, settings.url]);
});
it works in the sense that it shows the notification, but it doesn’t send an event to analytics.
any leads?
The problem is that
xhr.statusis an integer, and typically 0. The second argument totrackEventis supposed to be a string, and apparently if it’s integer 0, Google ignores the call. So usingstatusTextfixes it:Also, I’d recommend putting the GA call before your code since (no offense) Google’s code is less likely to error out.
Great idea, BTW!