Lesson 8 of 11
JavaScript Error Tracking
① Connect your GTM container
Paste your container ID to load it into this page. It only ever runs here.
Advanced: use a specific environment
A broken script fails silently: the visitor sees a dead button, you see nothing. GTM ships a JavaScript Error trigger that catches uncaught window.onerror errors and exposes them as a gtm.pageError event, so you can send them to GA4 and watch for spikes.
Paired with it are four built-in variables, Error Message, Error URL, Error Line and Debug Mode, that tell you what broke and where. Turn them on and pass them as event parameters so the report is actionable, not just a count.
The button below throws a real uncaught error (harmless here). Build the trigger and a tag, then watch gtm.pageError land in Tag Assistant.
Goal
Catch an uncaught JavaScript error and fire a tag on it, carrying the Error variables.
Build it in GTM
Turn on the Error built-in variables
Go to Variables → Configure and enableError Message,Error URLandError Lineso your tag can report what broke.Create the JavaScript Error trigger
- Go to Triggers → New and choose JavaScript Error.
- Leave it on All JavaScript Errors.
- Name it
Error - JavaScriptand Save.
Tag: a GA4 Event tag (or Custom HTML - Test)
- Go to Tags → New. Use a GA4 Event tag named
GA4 - JS Error(event nameexception), mapping{{Error Message}}and{{Error URL}}as parameters. Or use Custom HTML with<script></script>as the stand-in. - Under Triggering, add
Error - JavaScript, then Save.
- Go to Tags → New. Use a GA4 Event tag named
Debug in Tag Assistant
Copy this lesson's live URL and paste it into GTM Preview, that is the page Tag Assistant connects to. It has the clickable elements, so this page stays clean for reading.
- In your GTM, click Preview.
- Paste the live URL above and click Connect.
- Interact with the live page and watch your tag fire in Tag Assistant.
What you should expect to see
gtm.pageError event to the timeline and your tag fires under Tags Fired, with the Error variables populated.Verify your container
Built it? Export your container, Admin → Export Container, choose your workspace, then drop the JSON here to check it against this lesson.
Drop your container .json here
or browse · checked in your browser, nothing is uploaded