[Analytics Implementation](/categories/analytics-implementation) November 25, 2026

# JavaScript error tracking for analytics and marketing teams

How JavaScript errors break tags, the dataLayer and conversions, how to capture them with the error and unhandledrejection events, and what to measure.

![](/images/65d75b0ae8d69ef95785a5d6_david-pombar.webp)

**[David Pombar](/author/david-pombar)** Swiss army knife at Trackingplan

5 min read · 1176 words

JavaScript errors rarely break a page in a way anyone sees. They break the code around it. A script throws before it pushes the purchase to the dataLayer, and every tag that waits for that push stays silent. A custom tag fails on one browser, and that browser's conversions disappear from one ad platform. The order still goes through. The report is what goes wrong.

That makes JavaScript error tracking a data question as much as an engineering one. This post is for the analytics and marketing side: how errors break tracking, how to capture them, and what to measure. Dedicated error monitoring tools go further into stack traces and debugging, and developers should keep them. The point here is to know when an error is costing you data.

## How JavaScript errors break tracking

Tracking on a website is a chain of scripts: the site code that publishes values to the dataLayer, the tag manager that reads them, and the tags that send hits to each platform. An uncaught error stops the script it happens in. Whatever came after it in that script does not run.

Four places where that costs data:

-   **Before the dataLayer push.** The checkout code calculates the order, throws on an unexpected value, and never reaches `dataLayer.push({ event: 'purchase', … })`. Every tag that fires on the purchase stays silent.
-   **Inside a custom tag.** A custom HTML tag in the tag manager reads a variable that is undefined on one template and throws. That tag sends nothing on that template; the other tags keep working, so the numbers disagree between platforms.
-   **In a third-party script.** A chat widget, an A/B testing tool or a consent banner update throws early in the page and delays or blocks what loads after it.
-   **On a single-page app route change.** The code that sends the virtual page view runs inside the router. An error there and the page views of a whole section stop, while the app itself keeps working.

None of these shows up as a broken page. They show up as a conversion count that drops, a funnel step that looks abandoned, or two platforms that no longer agree. In our [tracking audit benchmark](/guides/tracking-audit-benchmark) of 10 top-brand websites, missing events and blind spots showed up on 9 of the 10 sites, and an error in the code before the push is one of the ways an event goes missing.

## How to capture JavaScript errors

Browsers already report errors. You only need to listen.

The `error` event fires on `window` when a script throws and nobody catches it. [MDN's reference for the error event](https://developer.mozilla.org/en-US/docs/Web/API/Window/error_event) lists what it carries: the message, the file, the line and the column. The `unhandledrejection` event does the same for promises that fail with no handler, which is how many modern scripts and API calls fail; [MDN's page on it](https://developer.mozilla.org/en-US/docs/Web/API/Window/unhandledrejection_event) explains the event and its `reason`.

A minimal listener that pushes both to the dataLayer:

```js
window.addEventListener('error', function (e) {
  window.dataLayer = window.dataLayer || [];
  window.dataLayer.push({
    event: 'js_error',
    error_message: String(e.message).slice(0, 100),
    error_source: e.filename,
    page_path: location.pathname
  });
});

window.addEventListener('unhandledrejection', function (e) {
  window.dataLayer = window.dataLayer || [];
  window.dataLayer.push({
    event: 'js_error',
    error_message: String(e.reason).slice(0, 100),
    error_source: 'promise',
    page_path: location.pathname
  });
});
```

From there, a tag sends `js_error` to GA4 or wherever you keep analytics. In Google Tag Manager you can skip the code: the **JavaScript Error** trigger type listens for uncaught errors and exposes the message, URL and line as built-in variables.

Three rules keep it useful:

1.  **Cut the message.** Error messages can be long, and some include values from the page. Keep the first characters and never send form values or anything personal.
2.  **Expect volume.** A broken script can throw on every page view of every visitor. Send errors to analytics only if your plan and your reports can take it, or sample them.
3.  **Keep the page.** The message tells you what failed; the page tells you which journey lost data. Without it, an error count is hard to act on.

## What to measure

The total number of errors is the least useful number. It grows with traffic, so a good day looks like a bad one.

Two ratios move only when something breaks:

-   **Errors per session**: how often a visit hits an error at all.
-   **Errors per page load**: how many errors each page view produces, which points at the page or the template that broke.

Read them against time, not in isolation. A site with a steady background of errors from old browsers and extensions is normal; a jump in errors per page load the morning after a release is the signal. Line it up with your release dates and your tag manager publishes, because that is when most tracking breaks ship.

Then connect it to the data that matters. A spike in errors on the checkout template the same day purchases drop in one platform is not a coincidence to note; it is the first place to look. For the dataLayer side of that check, our guide on [dataLayer validation](/guides/datalayer-validation) lists what to verify on every push.

## Where Trackingplan fits

Trackingplan tracks JavaScript errors as part of Vitals, next to Web Vitals and traffic. It reports three numbers for your site: JavaScript errors, errors per session and errors per page load, and it warns you when one of them changes unexpectedly. The [Vitals documentation](/docs/platform-metrics) describes each metric and how the warnings work.

Because the same tool watches the hits your tags send, an error spike and a drop in a conversion show up in the same place, on real traffic, instead of in two dashboards owned by two teams. Developers keep their error monitoring for the stack trace. The analytics team gets the signal that something on the page is costing data, and when it started.

## FAQ

### What is JavaScript error tracking?

It is recording the JavaScript errors that happen in your visitors' browsers: how many, on which pages, and when they change. For an analytics team the point is narrower than for developers: an error in the wrong script stops a dataLayer push, a tag or a conversion, and nothing on the page says so.

### How do I track JavaScript errors in GA4?

Listen for the browser's `error` and `unhandledrejection` events, and send each one as a GA4 event with the message and the page. With Google Tag Manager, the JavaScript Error trigger does the listening for you. Keep the volume in check: a broken script can fire the same error on every page view.

### Which JavaScript error metrics matter?

Errors per session and errors per page load, not just the total. The total grows with traffic; the ratios only move when something broke. Watch them right after each release and each tag manager publish.

### Can JavaScript errors affect conversion tracking?

Yes. If the script that pushes the purchase to the dataLayer throws before the push, no tag sees the purchase. If a tag's own code throws, that tag sends nothing. The order still goes through, so the only sign is a conversion count that drops.

On this page

1.  [*01*How JavaScript errors break tracking](#how-javascript-errors-break-tracking)
2.  [*02*How to capture JavaScript errors](#how-to-capture-javascript-errors)
3.  [*03*What to measure](#what-to-measure)
4.  [*04*Where Trackingplan fits](#where-trackingplan-fits)
5.  [*05*FAQ](#faq)

**Stop babysitting tags**

Trackingplan audits your real user data around the clock and flags issues before they cost you.

[Start free trial →](https://app.trackingplan.com/signup)

![](/images/65d75b0ae8d69ef95785a5d6_david-pombar.webp)

**David Pombar** Swiss army knife at Trackingplan

Read more from David, a Senior Product Strategist with 18+ years in digital product development and an atypical error detection knack.

Share Copy link [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fwww.trackingplan.com%2Fblog%2Fjavascript-error-tracking) [X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fwww.trackingplan.com%2Fblog%2Fjavascript-error-tracking&text=JavaScript%20error%20tracking%20for%20analytics%20and%20marketing%20teams)

Read next

[All Blog →](/blog)

-   [
    
    8 minDatalayerNovember 2, 2026
    
    ### dataLayer validation: what to check before every release
    
    What to check in the dataLayer on every push, and how to catch renamed keys, type changes, and missing fields before they reach GA4, ads, or the warehouse.
    
    by **David Pombar**](/guides/datalayer-validation)
-   [
    
    6 minAnalytics ImplementationDecember 25, 2025
    
    ### Web tags: what they are and how website tagging works
    
    What a web tag is, what one looks like, the request it sends, how it differs from meta and HTML tags, and what breaks when tags go unwatched.
    
    by **David Pombar**](/blog/what-is-web-tagging)
-   [
    
    8 minData IntegrityNovember 24, 2026
    
    ### Tracking audit benchmark: what breaks at 10 top brands
    
    We audited the websites of 10 brands that rank top three in their sector. 271 verified tracking problems, ranked by how many sites failed each check.
    
    by **David Pombar**](/guides/tracking-audit-benchmark)

Trackingplan

## See everything. Miss nothing.

Your implementations audited around the clock with real-time, real-user data. Real-time alerts about errors or changes in your data, campaigns, pixels, privacy and consent. Let AI flag issues before they cost you.

[Start free trial →](https://app.trackingplan.com/signup) [Book a demo](/demo)
