Simple JavaScript Error Tracking with Google Tag  Manager

Track client-side JavaScript errors as Google Analytics events using Google Tag Manager and avoid error logging software.

There’s been a recent surge in the number of log based software services for front-end developers. Overall this is a good thing, many of these services can greatly reduce the amount of time it takes us to find and fix errors. But sometimes I find myself questions the idea that a service is needed for everything. Whenever I can I like to solve simple problems with code, and if possible in a way that’s budget friendly or even sometimes free.

If you’re looking for a way to track JavaScript errors that your website visitors experience on the client-side then I’d suggest you consider the following free option that makes use of event tracking in Google Analytics. If you’re not using Google Tag Manager, I highly recommend switching to it.

1. Create a JavaScript Error trigger

Under the Triggers section create a custom trigger for handling JavaScript errors and make it fire for all All JavaScript Errors.

Google Tag Manager JavaScript Error Tracking: Step 1

More information on the JavaScript error trigger can be found in Google Tag Manager’s official documentation.

2. Create a Universal Analytics Tag

Under the Tags section create a new copy of your Universal Analytics tag and rename it to Google Analytics Exceptions, or whatever you’d prefer.

Google Tag Manager JavaScript Error Tracking: Step 2

Adjust the following configuration options for the tag:

  • Track TypeEvent
  • CategoryJavaScript Error
  • Action{{Error Message}}
  • LabelLine {{Error Line}} of {{Error URL}} on {{Page URL}}
  • Non-Interaction Hittrue

3. Enable Built-in Variables for Errors

From the Google Analytics Exception tag page click the Variables icon for any field. This looks like a lego piece with a “+” sign in the middle. In the upper right-hand corner click the Built-ins link to open the Built-in Variables dialog. From here enable the following under Errors:

  • Error Message
  • Error URL
  • Error Line

Google Tag Manager JavaScript Error Tracking: Step 3

4. Connect the Trigger to the Tag

From the Google Analytics Exception tag page select JavaScript Errors under the Triggering section to track an event when any JavaScript error occurs.

That’s it!

You’re all set, just publish your updated container version and you’re good to go. JavaScript errors will now appear inside of Google Analytics as custom events. Hopefully this saves you a few bucks and helps you simplify the process of tracking client-side JavaScripts.

Related Articles

Meet the Author

Kevin Leary, WordPress Consultant

I'm a custom WordPress web developer and analytics consultant in Boston, MA with 16 years of experience building websites and applications. View a portfolio of my work or request an estimate for your next project.