Google Tag Manager

⌘K
  1. Home
  2. Google Tag Manager
  3. Google Tag Manager Tracki...
  4. Capture JavaScript Errors with Google Tag Manager and Send to GA4

Capture JavaScript Errors with Google Tag Manager and Send to GA4

Objective

To automatically detect and track JavaScript errors on your website using GTM, and send them to Google Analytics 4 as custom events (e.g. js_error), including useful error details.

Step 1: Enable Built-in Error Variables in GTM

  1. Go to Google Tag Manager
  2. Click Variables in the left menu
  3. Click “Configure” (top right)
  4. Scroll down to Errors, and enable:
  • Error Message
  • Error URL
  • Error Line
  • Error Stack Trace (optional but useful)

Step 2: Create a Trigger for JavaScript Errors

  1. Go to Triggers → Click “New
  2. Name the trigger: JS Error – All Pages
  3. Trigger Configuration → Select Trigger Type: Error
  4. Choose: This trigger fires on All JavaScript Errors
  5. Click Save

Step 3: Create the GA4 Event Tag

  1. Go to Tags → Click New
  2. Name it: GA4 – js_error
  3. Tag Configuration → Select Google Analytics: GA4 Event
  4. Under Configuration Tag, select your existing GA4 config
  5. Event Name: js_error
  6. Add Event Parameters (optional but useful for analysis):
Parameter NameValue
error_message{{Error Message}}
error_url{{Error URL}}
error_line{{Error Line}}

(Optional: Add error_stacktrace with {{Error Stack Trace}})

Step 4: Link the Trigger to the Tag

  1. Scroll down to Triggering
  2. Choose the trigger: JS Error – All Pages
  3. Click Save

Step 5: Preview and Test (Optional)

Testing JavaScript errors is tricky since real errors may not be consistent. You can simulate one manually in the browser console.

Simulate a test error:

  1. Open your website
  2. Open Developer Console in your browser (F12 or right-click → Inspect → Console)
  3. Paste this line and press enter:

setTimeout(function () {

  throw new Error(“Simulated GTM error”);

}, 1000);

  1. In the Tag Assistant Preview, check if:
  • A js_error event appears
  • Your GA4 tag fires with the correct parameters

Step 6: Publish

Once tested and verified:

  1. Click Submit → Publish in GTM
  2. Name the version: GA4 – JavaScript Error Tracking
Publish in Google Tag Manager

How can we help?