3

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?

4
  • Do you know if Analytics is invoked? You could check it if you use fiddler Commented Mar 15, 2012 at 10:02
  • invoked in the sense that it sends a request? than no, it doesn't send a request. in the sense that there's _gaq initialized? than yes. Commented Mar 15, 2012 at 10:04
  • 1
    The push function is inside a try statement, maybe you could attatch a debugger and see whats happining inside. Commented Mar 15, 2012 at 10:12
  • 1
    Try switching the order of showNotification and _gaq.push. If that doesn't work, you could incorporate the _gaq.push into the showNotification function based on title. Commented Mar 15, 2012 at 12:27

2 Answers 2

2

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!

Sign up to request clarification or add additional context in comments.

Comments

0

You may have another error happening somewhere else in your code. I have mocked up a sample of this functionality here, and it works:

http://jsbin.com/emocah/edit#preview

Did you just start tracking this? Sometimes new events take a few days to propagate in GA. Also, are there any other JS errors that occur on your page?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.