📜 ⬆️ ⬇️

Google Analytics to collect javascript errors

image
Not so long ago on Habré there was an article about a heaped up startup focused on gathering JavaScript errors. Not always you need so many opportunities, but it turned out that many people simply do not know about the old bearded way with Google Analytics. I will try to tell about him briefly.

The first thing you need to do is install Google Analytics (hereinafter simply GA) on the site, everything is standard and you don’t need to change anything:
<script> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-XXXXXX']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })(); </script> 

Further, it is advisable even before uploading any scripts, that is, in the head, to put the following above all:
 <script> var _gaq = window._gaq || []; window.onerror = function(msg, url, line) { var preventErrorAlert = true; _gaq.push(['_trackEvent', 'JS Error', msg, navigator.userAgent + ' -> ' + url + " : " + line, 0, true]); return preventErrorAlert; }; </script> 


preventErrorAlert - returning true, we prevent annoying windows with errors in old versions of IE and Opera.
msg - error message.
url - the script file in which the error occurred.
line - on which line the error occurred.
navigator.userAgent - simple browser data to make it clear where to dig.

The last two parameters (reminded korzhik ):
0 is a counter, i.e. you can just transfer a line number here and calculate which average error occurs on which average, something useful can be done :)
true - do not include in the bounce rate. That is, the event will not be counted as a custom action. Important for those who are seriously engaged in analytics.
')
Almost all modern browsers support onerror:

Thus, you can safely collect critical errors. But errors are different, if you use jQuery, I recommend adding the following code after it is initialized:
 jQuery.error = function (message) { _gaq.push(['_trackEvent', 'jQuery Error', message, navigator.userAgent, 0, true]); } 


How to make a handler of your own exceptions, I think it is already clear. And a couple of screenshots, where the whole thing then find and how it will look.

Go to GA in our project, select "Standard reports":
image

In the panel on the left we find “Content” → “Events” → “Overview”:
image

We see the schedule of events and the types of events themselves at the bottom right:
image

Choose the event “JS Error” we need:
image

Go to the "Action event":
image

We select the error we are interested in and observe debugging information. Going deeper meaning does not have:
image

Knowing how GA events work, where they are and how they look, you can easily customize the kind of error reporting that best suits your project.

Source: https://habr.com/ru/post/149926/


All Articles