How to track JavaScript errors that occur before Matomo JS tracking code is loaded
Matomo CrashAnalytics by default only tracks JavaScript errors that occur after matomo.js
has been loaded and executed successfully. This means if a JavaScript error occurs before matomo.js
is loaded, it will not track such JavaScript errors. To track errors that occur before matomo.js
is loaded, you can add the following snippet into your code as high as possible in the <head>
section, preferably.
window.onerror = function(msg, url, lineNo, columnNo, error) {
if (!window.Matomo) {
//(message, type, category, stack, location, line, column)
window._paq.push(['CrashAnalytics::trackCrash', msg, 'JSError', '{CATEGORY}', error.stack, url, lineNo, columnNo]);
}
};