Debuging JS with Google Analytics by Eduardo C.


/ Published in: JavaScript
Save to your folder(s)

1. add the function track_error_event() to your tracking snippet
2. call it when a Javascript Exception raises.


Copy this code and paste it in your HTML
  1. <!-- GA Code Start -->
  2. <script type="text/javascript">
  3. var _gaq = _gaq || [];
  4. _gaq.push(
  5. ['_setAccount','UA-XXXXX-X'],
  6. ['_trackPageview']
  7. );
  8. function track_error_event (exception) {
  9. _gaq.push(['_trackEvent',
  10. 'Exception ' + (exception.name || 'Error'), //event category
  11. exception.message || exception, //event action
  12. document.location.href //event label
  13. ]);
  14. }
  15. (function() {
  16. var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  17. ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  18. var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  19. })();
  20. </script>
  21. <!-- GA Code End -->
  22.  
  23. <script type="text/javascript">
  24. try{
  25. //Javascript Code goes here
  26. //Raises Division by zero exception
  27. var i = 1/0;
  28. }
  29. catch(e){
  30. track_error_event(e);
  31. }
  32. </script>

URL: http://www.directperformance.com.br/en/javascript-debug-simples-com-google-analytics

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.