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

Lesson 9 of 11

Promise Rejection 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

GTM's JavaScript Error trigger only hears window.onerror, which fires for synchronous throws. A rejected promise that nobody catches fires a different event, unhandledrejection, and slips straight past the built-in trigger. On modern, async-heavy SPAs that's a real blind spot: a failed fetch or a broken await never shows up.

The fix is a tiny Custom HTML listener that subscribes to unhandledrejection and pushes a dataLayer event you can trigger on. Then it's the familiar loop: a Custom Event trigger matches the event, a tag fires.

The button below rejects a promise with no .catch(). Build the listener and tag it.

Goal

Catch an unhandled promise rejection with a listener and fire a tag on it.

Build it in GTM

  1. Listener: Custom HTML - Rejection Listener

    1. Go to Tags → New → Custom HTML.
    2. Paste a listener that pushes an event on rejection:
      <script>window.addEventListener('unhandledrejection',function(e){window.dataLayer.push({event:'unhandled_rejection',reason:String(e.reason)});});</script>
    3. Trigger it on Initialization, All Pages so it's listening early, name it Custom HTML - Rejection Listener and Save.
  2. Trigger: Custom Event - Unhandled Rejection

    1. Go to Triggers → New → Custom Event.
    2. Set the event name to unhandled_rejection, the name your listener pushes.
    3. Name it Custom Event - Unhandled Rejection and Save.
  3. Tag: a GA4 Event tag (or Custom HTML - Test)

    Add a tag (GA4 Event exception, or the Custom HTML - Test stand-in) firing on Custom Event - Unhandled Rejection, 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

Rejecting the promise pushes unhandled_rejection to the dataLayer and your tag fires, the async error the built-in trigger missed.

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