Last active
October 19, 2018 07:26
-
-
Save vmysla/5ee9f0e8cecbd12c7234 to your computer and use it in GitHub Desktop.
Monitoring AJAX calls to API with GA reporting for Timing and Errors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ... | |
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