Angular


Error Handler

Angular catches errors thrown in components, services and event handlers with its ErrorHandler, which logs them to the console. Replace it with one that also reports them. Extending the default handler keeps the console output:

// src/app/understand-error-handler.ts
import { ErrorHandler, Injectable } from '@angular/core';
import Understand from '@understand/understand-js';

@Injectable()
export class UnderstandErrorHandler extends ErrorHandler {
  override handleError(error: unknown) {
    Understand.logError(error);
    super.handleError(error);
  }
}

Initialise the SDK in src/main.ts, before the app is bootstrapped:

// src/main.ts
import { bootstrapApplication } from '@angular/platform-browser';
import Understand from '@understand/understand-js';
import { appConfig } from './app/app.config';
import { AppComponent } from './app/app.component';

Understand.init({ env: 'production', token: 'your-input-key' }).catchErrors();

bootstrapApplication(AppComponent, appConfig);

Standalone apps (Angular 15+, the default since Angular 17): register the handler in app.config.ts:

// src/app/app.config.ts
import { ApplicationConfig, ErrorHandler } from '@angular/core';
import { UnderstandErrorHandler } from './understand-error-handler';

export const appConfig: ApplicationConfig = {
  providers: [
    { provide: ErrorHandler, useClass: UnderstandErrorHandler }
  ]
};

NgModule apps: register it in the root module:

// src/app/app.module.ts
import { ErrorHandler, NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { UnderstandErrorHandler } from './understand-error-handler';

@NgModule({
  declarations: [AppComponent],
  imports: [BrowserModule],
  bootstrap: [AppComponent],
  providers: [{ provide: ErrorHandler, useClass: UnderstandErrorHandler }]
})
export class AppModule {}

HTTP errors from HttpClient reach the ErrorHandler only when nothing handles them. To report failed API calls you handle yourself, call Understand.logError(error) in your catchError operator or HTTP interceptor.

Analog

Using Analog (server-side rendering, API routes)? See the Analog guide.