JavaScript error tracking in Google Analytics via Google Tag Manager: The unsung hero

javascript error tracking

What? How? Why? Was that your reaction when you read the title of my blog? Well, as a developer, it is important for us that the code we have written, should not break or should not be blocked by other scripts. Users are very creative these days – if at all tester misses some use cases, users will encounter some of those for sure. So, you need to make sure that your website is working well with every release and every publish of GTM.

It gives me immense pleasure to introduce Google Tag Manager’s unsung hero “Javascript error tracking feature”. GTM has this beautiful trigger named JavaScript Error ( also known as gtm.pageError dataLayer event ) enabled when any JS error occurs on the page.

Google Tag Manager

Image: javascript inbuilt variable in GTM

And to complement that trigger, it has built-in variables like

  • Error Message ( also known as gtm.errorLineNumber dataLayer variable : It will give value which represents the error description in string format
  • Error Url ( also known as gtm.errorMessage dataLayer variable ): It will give third party js file URL or any file URL which from which error occurred.
  • Error Line ( also known as gtm.errorUrl dataLayer variable ): This is an integer value that shows which line number has caused an error.

When javascript error occurs on site, GTM pushes a dataLayer which contains above-mentioned information with it and voila, you can now track errors in Analytics. Resolve those errors to improve the website performance.

Configure a tag as per the below screenshot and your error reporting will work like a charm:

Javascript Error Tracking

GIF: js error tag configuration

Precautions while implementing JavaScript Error Tracking:

  • Create a separate view to record these errors. Exclude them from Master view and the reason behind creating a new view for such tracking is that it will affect the actual user data by adding javascript error events in user’s session.
  • You can also restrict the JavaScript Error tracking to fire on specific conditions only

Usage of JavaScript Error Tracking:

  • Set alerts if your error rate reaches to certain level. This will enable you to minimise the risk in case of any important functionality on your website breaks down or gets affected via a third party script.
    Google Analytics Javascript error tracking

    Image: Custom Alert for javascript error in google analytics
  • Know which third party scripts are causing issues. Fix them. Below screenshot shows which files are causing these errors.
    dataLayer variable in GTM

    Image: Custom Alert for javascript error in google analytics

JavaScript Error tracking provides a whole new avenue in which we can use data in Google Analytics. This data will help you identify glitches on the website and you will get a notification over mail or on phone based on the configuration set in custom alerts. The faster you act in such cases, the less damage takes place. Most importantly, improve your website performance by resolving the errors which block important scripts execution.

So, start tracking errors on website and get to know the culprits who are affecting your website performance and most importantly, act faster in disaster cases. Feel free to reach out to us in case of any query or leave a comment in the section below.

JavaScript error tracking in Google Analytics via Google Tag Manager: The unsung hero by
The following two tabs change content below.
Yaman Patel

Yaman Patel

Yaman Patel is a Technical Analyst along with being an enthusiastic Customer Success Manager at Tatvic. He is one of the proud pioneers of Tatvic's dataLayer automation tool(DLAT). He is keen on learning business intelligence technologies. He loves to travel and explore new places.

Leave a Reply

Your email address will not be published. Required fields are marked *