← Blog·Analytics Implementation

Analytics Implementation

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.

David PombarSwiss 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:

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 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 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 explains the event and its reason.

A minimal listener that pushes both to the dataLayer:

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:

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 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 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.

David PombarSwiss 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.

Read next

All Blog →

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.