Error Tracking for Angular Apps: Zone.js Errors, RxJS Failures, and Route Drop-Off
Angular error tracking with Zone.js, RxJS, and route analytics in one 5KB script.
Picture an error that sits in production for weeks before anyone notices. TypeError: Cannot read properties of undefined (reading 'subscribe') — happening hundreds of times a day, exclusively on the checkout confirmation page, only for users who clicked "Apply Coupon" before their cart finished loading.
Nobody notices because the app doesn't crash. Angular's Zone.js catches the error, logs it to the console, and keeps running. Users see a spinner that never stops. They refresh. Try again. Eventually give up and go somewhere else.
The only visible symptom is in the analytics: checkout drop-off climbs. The funnel shows users reaching the confirmation step and then... nothing. No completion. No error page. Just gone.
The cause can be an error swallowed by an RxJS catchError that returns an empty observable. Angular keeps running. Users keep leaving. And an error tracker that only watches the global handler won't see errors from RxJS streams that never throw to it.
That's the problem with Angular error tracking in 2026: Zone.js and RxJS play by different rules than vanilla JavaScript, and most error tracking tools expect window.onerror to catch everything. It doesn't. Teams often don't notice until a silent failure has cost them a week.
What You'll Have by the End
A complete Angular error tracking setup that captures:
- Zone.js errors via a custom
ErrorHandler - RxJS stream failures that don't bubble to the global scope
- Route-level pageviews tied to the same session context
- All three in one under-5KB script, with unified session IDs for correlation
The output: when someone drops off your checkout flow, you can filter to "sessions with errors" and see exactly which exception caused it — in 30 seconds instead of three weeks.
Prerequisites
- Angular 15+ (tested through Angular 18)
- Node.js 18+
- A JustAnalytics account (free tier covers 100K events/month)
- Basic familiarity with Angular's dependency injection
Step 1: Install the SDK and Add the Script
There is no npm package for the browser. Add the tracking script to the <head> of your index.html:
<script
defer
src="https://justanalytics.app/tracker.js"
data-site-id="YOUR_SITE_ID">
</script>
<script
defer
src="https://justanalytics.app/monitor.js"
data-site-id="YOUR_SITE_ID">
</script>
The second script, monitor.js, provides error tracking and exposes window.JA. The tracker detects single-page route changes on its own (it watches history.pushState, history.replaceState and popstate), so Angular's router navigations are picked up without extra configuration.
You'll also want the TypeScript declarations. Add this to a src/types/global.d.ts file:
declare global {
interface Window {
JustAnalytics?: {
track: (eventName: string, properties?: Record<string, unknown>) => void;
trackPageview: () => void;
};
JA?: {
captureException: (error: unknown, options?: Record<string, unknown>) => void;
};
}
}
export {};
Now your IDE won't yell at you every time you call window.JustAnalytics or window.JA. Small win, but it matters when you're trying to ship something by Friday.
Step 2: Create a Custom ErrorHandler
Angular's default ErrorHandler logs to console and moves on. We need one that sends errors to JustAnalytics while still letting Angular continue running.
Create src/app/core/error-handler.service.ts:
import { ErrorHandler, Injectable, Injector } from "@angular/core";
@Injectable()
export class GlobalErrorHandler implements ErrorHandler {
// Use Injector to avoid circular dependency with Router
constructor(private injector: Injector) {}
handleError(error: unknown): void {
// Unwrap Zone.js wrapped errors
const unwrapped = this.unwrapError(error);
// Send to JustAnalytics
if (typeof window !== "undefined" && window.JA) {
window.JA.captureException(unwrapped);
}
// Still log to console for development
console.error("Angular Error:", unwrapped);
}
private unwrapError(error: unknown): Error {
// Zone.js wraps errors in a rejection object sometimes
if (error && typeof error === "object") {
if ("rejection" in error && error.rejection instanceof Error) {
return error.rejection;
}
if ("error" in error && (error as { error: unknown }).error instanceof Error) {
return (error as { error: Error }).error;
}
}
if (error instanceof Error) {
return error;
}
return new Error(String(error));
}
}
The unwrapError function handles a Zone.js quirk: rejected promises get wrapped in an object with a rejection property rather than thrown directly. If you don't unwrap them, your error messages look like [object Object] instead of actual stack traces. It's easy to lose days debugging an [object Object].
Register it in your app module (or app.config.ts for standalone apps):
// app.module.ts
import { NgModule, ErrorHandler } from "@angular/core";
import { GlobalErrorHandler } from "./core/error-handler.service";
@NgModule({
providers: [
{ provide: ErrorHandler, useClass: GlobalErrorHandler },
],
// ...
})
export class AppModule {}
Or for standalone:
// app.config.ts
import { ApplicationConfig, ErrorHandler } from "@angular/core";
import { GlobalErrorHandler } from "./core/error-handler.service";
export const appConfig: ApplicationConfig = {
providers: [
{ provide: ErrorHandler, useClass: GlobalErrorHandler },
],
};
At this point, any unhandled error in your Angular app — component lifecycle errors, template binding failures, service exceptions — flows through JustAnalytics with route context attached.
Step 3: Capture RxJS Stream Errors
Here's the part most tutorials skip — and what makes unified observability so valuable. It saves a lot of time otherwise spent chasing ghosts.
RxJS streams that use catchError don't throw to the global error handler. They're "handled" — even if your handler just returns EMPTY and moves on.
The coupon bug from the opening could live in an effect() that looked like this:
// This error never reaches the ErrorHandler
loadCoupons = effect(() => {
this.couponService.validate(this.couponCode()).pipe(
catchError((err) => {
console.log("Coupon validation failed:", err);
return EMPTY; // swallowed silently
}),
).subscribe();
});
The catchError handled it. Angular never saw it. JustAnalytics never saw it. Users just saw a spinner.
The fix is to explicitly track errors in your catchError blocks:
import { catchError, EMPTY } from "rxjs";
loadCoupons = effect(() => {
this.couponService.validate(this.couponCode()).pipe(
catchError((err) => {
// Track before swallowing
if (window.JA) {
window.JA.captureException(err);
}
this.errorMessage.set("Coupon validation failed");
return EMPTY;
}),
).subscribe();
});
For teams with a lot of streams, create a utility operator:
// src/app/core/operators/track-error.ts
import { Observable, throwError } from "rxjs";
import { catchError } from "rxjs/operators";
export function trackError<T>(context: string) {
return (source: Observable<T>): Observable<T> =>
source.pipe(
catchError((err) => {
if (typeof window !== "undefined" && window.JA) {
window.JA.captureException(err);
}
return throwError(() => err); // rethrow so downstream can still handle
}),
);
}
Usage:
this.userService.getProfile().pipe(
trackError("user_profile_load"),
catchError(() => of(DEFAULT_PROFILE)), // your actual recovery
).subscribe();
The error gets tracked, then rethrown, then caught by your application-level handler. You get visibility without changing your error recovery logic.
Step 4: Track Route Changes as Pageviews
The tracker already detects Angular route changes (see Step 1), so you normally don't need this step. Manual trackPageview() is only for navigations the tracker can't see. If you have those, subscribe to router events.
Create src/app/core/analytics.service.ts:
import { Injectable } from "@angular/core";
import { Router, NavigationEnd } from "@angular/router";
import { filter } from "rxjs/operators";
@Injectable({ providedIn: "root" })
export class AnalyticsService {
constructor(private router: Router) {}
init(): void {
this.router.events
.pipe(filter((event): event is NavigationEnd => event instanceof NavigationEnd))
.subscribe((event) => {
if (typeof window !== "undefined" && window.JustAnalytics) {
window.JustAnalytics.trackPageview();
}
});
}
}
Initialize it in your app component:
// app.component.ts
import { Component, inject, OnInit } from "@angular/core";
import { AnalyticsService } from "./core/analytics.service";
@Component({
selector: "app-root",
template: `<router-outlet></router-outlet>`,
})
export class AppComponent implements OnInit {
private analytics = inject(AnalyticsService);
ngOnInit(): void {
this.analytics.init();
}
}
Now the navigations the tracker can't see also fire a pageview. Soft navigations, lazy-loaded modules, guards that redirect — all tracked. It's simpler than Angular routing usually makes things.
Step 5: Tie It Together — Session Context
Here's where JustAnalytics differs from running Sentry plus a separate analytics tool. Every error and pageview shares the same session ID. When someone drops off your checkout funnel, you filter by "sessions with errors" and immediately see which exceptions correlate with abandonment.
The correlation happens automatically — you don't need to pass session IDs around. The script generates one on first load and attaches it to every event.
For teams migrating from GA4 to JustAnalytics, this is the biggest difference. GA4 and Sentry have separate session models that don't talk to each other. Correlating them requires BigQuery exports and SQL joins — and if you've ever written those joins at 2am during an incident, you know they're not fun. With unified observability, you just click a filter.
Common Errors and How to Fix Them
ERROR Error: NG0200: Circular dependency
If you inject Router directly in the ErrorHandler constructor, Angular throws a circular dependency error during bootstrap. Use Injector and resolve the router lazily (as shown in the example above). The router isn't available until after the app initializes anyway.
Zone.js has detected that ZoneAwarePromise (window|global).Promise has been overwritten
Some polyfills and third-party scripts replace window.Promise after Zone.js patches it. This warning is mostly harmless for error tracking, but it can cause unhandled rejections to miss the Zone.js wrapper. If you're seeing errors that don't reach your ErrorHandler, check for polyfill load order issues.
RxJS errors showing as [object Object]
You're passing an HttpErrorResponse or custom error object to the tracking call without unwrapping it. Either extract the message: window.JA.captureException(err.message || err) — or make sure your error objects have a sensible toString() method.
Pageviews firing twice on initial load
The tracker already records pageviews and route changes automatically, so a trackPageview() call on every NavigationEnd records the same navigation a second time. Remove the router subscription unless you have navigations the tracker can't see.
Errors not appearing in production but working in dev
Check that your environment.production.ts isn't disabling error tracking. Also verify the script is loading — ad blockers sometimes flag analytics domains. You can proxy the script through your own domain (the JustAnalytics docs have a Nginx config for this) to avoid that. A common trap: it works in Chrome without extensions, then goes quiet in production because some users run an ad blocker such as uBlock Origin.
Next Steps
With this setup, you've got the foundation for real Angular observability. A few things to add next:
Track custom events for conversion funnels. Add window.JustAnalytics.track('checkout_started', { cart_value: 149.99 }) calls at key points in your user flow. Then correlate error rates by funnel step — See the exact query for finding conversion-killing bugs.
Enable session replay for the bugs that need visual context. JustAnalytics includes DOM replay with PII masking enabled by default. It's opt-in per-session and adds about 1KB to the initial script size. It can help debug race conditions that stack traces alone can't explain. Honestly? I think every production app should have replay enabled. The storage cost is trivial compared to the debugging time you save.
Add uptime monitoring for your backend APIs. The same JustAnalytics account includes HTTP checks, SSL monitoring, and alert routing. One script, one dashboard, one bill — instead of stitching together Sentry, GA4, Pingdom, and LogRocket. For teams doing Rails backend integration, the approach is similar.
If you're running multiple Angular apps or need to isolate errors by environment, check out JustBrowser for testing your tracking across isolated browser profiles before deploying. And for teams protecting paid traffic, ClickzProtect catches click fraud while JustAnalytics catches application errors — the two surfaces complement each other when you're debugging why conversions tanked.
The coupon bug from the start is a good example of the cost: weeks of silent drop-offs because an RxJS stream swallowed an error, and a quick fix once you can see it. That's the cost of not tracking Angular errors properly.
Don't pay it.
Frequently Asked Questions
Does JustAnalytics work with Angular's standalone components?
Yes. Standalone components (Angular 14+) work identically to NgModule-based apps. You provide the custom ErrorHandler in your bootstrapApplication() call instead of an NgModule. The tracking code is the same — JustAnalytics doesn't care whether you're using standalone or NgModule architecture.
How do I capture errors from RxJS streams that use catchError?
When you handle errors with catchError, they don't propagate to the global ErrorHandler. You need to explicitly track them: inside catchError, call window.JA.captureException(error) before returning your fallback observable. The same applies to any operator that swallows errors like retry() with no rethrow.
Will this slow down my Angular app?
The JustAnalytics script is under 5KB gzipped. It loads async and doesn't block rendering. We've benchmarked it on Angular apps with 50+ components and lazy-loaded routes — the impact on Time to Interactive is under 10ms. For comparison, Sentry's Angular SDK adds 28KB to your bundle.
How do I correlate errors with specific routes or user actions?
JustAnalytics automatically associates errors with the current route via the session context. When you track route changes with router.events, the error and the pageview share the same session ID. You can filter errors by route in the dashboard without any extra code.
Try JustAnalytics
All-in-one observability in one under-5KB script: cookieless analytics + error tracking + APM + session replay + uptime + structured logs. Replaces GA4 + Sentry + Datadog + Pingdom + LogRocket. Free tier (100K events/mo), Pro $49/month ($39 annual).
Author at JustAnalytics.