@sentry/angular package provides error tracking and performance monitoring for Angular applications. It includes Angular-specific integrations like ErrorHandler, TraceService, and component tracking.
Prerequisites
- Node.js 18 or newer
- Angular 14 to 21
- RxJS 6.5.5+ or 7.x
- A Sentry account and project DSN
Angular versions 14-21 are officially supported. For older versions, check the compatibility table.
Installation
1
Install the Package
Install Current Version: 10.42.0
@sentry/angular using your preferred package manager:2
Initialize Sentry
Initialize Sentry before you bootstrap your Angular application in
main.ts:3
Configure Error Handler
Register the Sentry
ErrorHandler provider in your app configuration:For NgModule-based apps, add the provider to your
AppModule instead.4
Verify Installation
Test that Sentry is working:Check your Sentry dashboard to see the error.
NgModule Configuration (Legacy)
For older Angular applications using NgModule:Error Handler Options
Customize the error handler behavior:Performance Monitoring
Basic Configuration
1
Enable Browser Tracing
Add the browser tracing integration:
2
Initialize TraceService
Inject the Angular 19+: Use
TraceService in your app initialization to enable Angular Router tracking:provideAppInitializer:NgModule Configuration
For NgModule-based apps:Component Tracking
Track Angular component performance with three different methods:1. TraceDirective
Track component lifecycle in templates:The directive’s
name attribute is required and will be used as the span name.2. TraceClass Decorator
Track component lifecycle in component classes:3. TraceMethod Decorator
Track specific lifecycle hooks:Custom Spans
Create custom performance spans:Tracking Application Bootstrap
Measure how long your Angular app takes to bootstrap:Usage
Capturing Errors
Setting Context
Adding Breadcrumbs
HttpClient Integration
Automatically capture HTTP errors:- Create spans for HTTP requests
- Capture HTTP errors
- Add breadcrumbs for requests
Session Replay
Capture session replays:Advanced Configuration
TypeScript
The SDK is written in TypeScript and includes full type definitions:Custom Transport
Filtering Events
Troubleshooting
Source Maps
For Angular CLI projects:-
Install the Sentry Webpack plugin:
-
Configure in
angular.json: -
Add custom webpack config with
@angular-builders/custom-webpack
Zone.js Compatibility
Sentry is compatible with Zone.js. Make sure Zone.js is loaded before Sentry:Errors Not Captured
Make sure:Sentry.init()is called before bootstrapping Angular- The
ErrorHandlerprovider is registered - The error is not caught without re-throwing
Performance Not Tracked
Ensure:browserTracingIntegration()is addedTraceServiceis injected viaAPP_INITIALIZERtracesSampleRateis greater than 0
Next Steps
- Configure Angular Router Integration
- Set up HttpClient Monitoring
- Learn about Session Replay
- Explore Performance Monitoring