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.status is an integer, and typically 0. The second argument to trackEvent is supposed to be a string, and apparently if it's integer 0, Google ignores the call. So using statusText fixes it:
$(document).ajaxError(function(e, xhr, settings) {
// Generic Google error logging
_gaq.push(['_trackEvent', 'AjaxError', xhr.statusText, settings.url]);
// Application-specific UI code
$(".modal").modal('hide');
showNotification('Something went wrong...', 'Whoa there! Something happened which we didn\'t predict, we sent ourselves a notification. ');
});
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With