0

I want the window.onerror handler to be called when an error occures inside the "ga(function(tracker)" function. Apparently the ga function has it's own error handler which prevents the window.onerror handler to be invoked.

(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

window.onerror = function() {console.log('Hello world') };
    
ga('create', '********', '********');
ga(function(tracker) {
console.log('Start');
/* 
Variable y is undefined 
window.onerror should be invoked
*/
var x = y;
console.log('End');
});
ga('send', 'pageview');

Just to be clear. My question is NOT how to track errors with google analytics but how to catch errors inside google analytics scripting.

5
  • you can use google tag manager onerror or check this answer Commented Sep 8, 2020 at 10:07
  • 1
    Does this answer your question? Report for exceptions from Google Analytics analytics.js exception tracking Commented Sep 8, 2020 at 10:28
  • @MichelePisani Thanks for replying. No my question is not how to track javascript errors with google analytics but how to track errors inside the javascript of google analytics. Commented Sep 8, 2020 at 10:51
  • What kind of error would you like to track within the analytics code? Commented Sep 8, 2020 at 20:25
  • @MichelePisani All errors. If an error occurs inside this part of the script it is possible a pageview/event/etc is not send to google analytics. We track all javascript errors on our website via the window.onerror event. However for some reason the window.onerror is not invoked for errors inside the ga scripts. This means it can take a while before we notice a problem. Commented Sep 9, 2020 at 10:45

1 Answer 1

1

using custom try..catch and dispatchEvent would help:

    
ga('create', '********', '********');

// declaring readyCallback function
var initFunction = function(tracker) {
console.log('Start');
/* 
Variable y is undefined 
window.onerror should be invoked
*/
var x = y;
console.log('End');
}

// Wrapping your readyCallback with custom try...catch:
var initFunctionWrapped = function (tracker) {
  try {
    initFunction(tracker)
  } catch (e) {
    var event = new Event('error', e);
    window.dispatchEvent(event)
}

ga(initFunctionWrapped);
ga('send', 'pageview');
Sign up to request clarification or add additional context in comments.

2 Comments

If the initFunction returns an error why is it caught using the initFunctionWrapped function but not caught if called directly by the window.onerror handler?
If you don't use dispatchEvent any error in readyCallback will be caught by ga inner try...catch and won't propagate to window

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.