How do you attached error handler to window.onerror after utag.js has loaded?

andrew_gatlabay
Visitor
andrew_gatlabay

I am looking for the best practice with Tealium. And also to find out if there is there a way to configure utag.js. Configure it in a manner to not take control over window.onerror and allow the client to implement utag.js's error handler? My code currently works as such:

 

// utag.js loads and updates window.onerror.
// my scripts
var oldOnError = window.onerror || function(){};
var myOnError = function ( errorMessage, url, lineNumber ) {
    // handle error for client reporting
}; // update window.onerror window.onerror = function ( ) {     var args = Array.prototype.splice.call( arguments );     // run my error handler     myOnError.apply( window, args );     // run previously set error handler     oldOnError.apply( window, args ); };

 

But, I feel it would be good practice for utag.js to not take over window.onerror based on a configuration. The implementation could look like this:

 

window.utag_cfg_ovrd= { no_onerror: true };
window.onerror = function ( ) {
    var args = Array.prototype.splice.call( arguments );
    // run my error handler
    myOnError.apply( window, args );
    // run utag's onError handler
    utag.onError.apply( window, args );
};

 

Thanks, Drew

2 REPLIES 2

How do you attached error handler to window.onerror after utag.js has loaded?

Tealium Employee

Hi Drew, The utag.js error tracking is added in when you have entered a email address into the "Alert Notify Email" under the Publish Configuration settings. If you wanted to remove the utag error tracking code, then removing this email address would be the way to go. With regards to the the code you have it isn't very dynamic in regards to other scripts appending to the window.onerror. What I would suggest is something more like:

 

var oldOnError = window.onerror || function(){};
var myOnError = function (errorMessage, url, lineNumber, colNumber) {
    // pass through to standard error handling
    oldOnError(errorMessage, url, lineNumber, colNumber);

    // custom error handling here...
};
window.onerror = myOnError;

 

Hope this helps, Adrian

Ask me anything Tealium related or Javascript, or NodeJS.
Please remember to mark solutions as accepted for future searchers.
Highlighted

How do you attached error handler to window.onerror after utag.js has loaded?

andrew_gatlabay
Visitor
andrew_gatlabay
Hi Adrian, The Alert Notify Email is a feature I plan on leaving on. And the suggested approach that you have for appending to the window.onerror is definitely better. Thanks for the quick response. -Drew