Skip to content

Instantly share code, notes, and snippets.

@vmysla
Last active October 19, 2018 07:26
Show Gist options
  • Save vmysla/5ee9f0e8cecbd12c7234 to your computer and use it in GitHub Desktop.
Save vmysla/5ee9f0e8cecbd12c7234 to your computer and use it in GitHub Desktop.
Monitoring AJAX calls to API with GA reporting for Timing and Errors
// ...
function ajaxRequest(url, done, error){ ... }
//...
realAjaxRequest = ajaxRequest;
ajaxRequest = monitoredAjaxRequest;
function monitoredAjaxRequest(url, done, error){
var startTime = new Date().getTime();
function monitoredDone(response){
setTimeout(function(){ done(response); }, 100);
var endTime = new Date().getTime();
var timeSpent = endTime - startTime;
ga('send', 'timing', 'justanswer.api', 'done', timeSpent, document.location.hostname);
}
function monitoredError(details){
setTimeout(function(){ error(details); }, 100);
var endTime = new Date().getTime();
var timeSpent = endTime - startTime;
var description = 'justanswer.api error:' + details.message;
ga('send', 'timing', 'justanswer.api', 'error', timeSpent, document.location.hostname);
ga('send', 'exception', { 'exDescription': description, 'exFatal': true, 'appName': 'lowlander' });
}
return realAjaxRequest(url, monitoredDone, monitoredError);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment