Don't have one? Create one ↗
← All courses

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

  1. Turn on the Error built-in variables

    Go to Variables → Configure and enable Error Message, Error URL and Error Line so your tag can report what broke.
  2. Create the JavaScript Error trigger

    1. Go to Triggers → New and choose JavaScript Error.
    2. Leave it on All JavaScript Errors.
    3. Name it Error - JavaScript and Save.
  3. Tag: a GA4 Event tag (or Custom HTML - Test)

    1. Go to Tags → New. Use a GA4 Event tag named GA4 - JS Error (event name exception), mapping {{Error Message}} and {{Error URL}} as parameters. Or use Custom HTML with <script></script> as the stand-in.
    2. Under Triggering, add Error - JavaScript, then Save.

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.

  1. In your GTM, click Preview.
  2. Paste the live URL above and click Connect.
  3. Interact with the live page and watch your tag fire in Tag Assistant.

What you should expect to see

Clicking the button adds a 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

0 of 11 lessons complete