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.
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.
Adjust the following configuration options for the tag:
- Track Type —
Event
- Category —
JavaScript Error
- Action —
{{Error Message}}
- Label —
Line {{Error Line}} of {{Error URL}} on {{Page URL}}
- Non-Interaction Hit —
true
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
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.