Apart from general JavaScript knowledge, this snippets requires someone who knows how to properly use Google Analytics API.
To log errors on my site, I use this code:
try {
window.addEventListener("error", function(e) {
var file = e.filename.substr(e.filename.lastIndexOf("/")+1);
// for some reason, filename sometimes contained path, sometimes just name
if(file.length==0)
file = e.filename;
var msg = file+":"+e.lineno+":"+e.colno+" \""+e.message+"\" Browser: "+navigator.userAgent;
if(typeof ga=="function") {
ga('send', 'event', 'jserror', 'click', msg, {'transport': 'beacon'});
}
else {
// This is executed when google analytic is not loaded
// which happens in debug mode
console.error(msg);
}
});
}
catch(e) {
console.warn("Failed to add error callback.");
}
I'm especially interested to know whether I'm using the google analytics API correctly. Maybe I could log the data about error in multiple columns?
I'm new to google analytics so feedback is very valuable for me.