Angular
The following docs are applicable to Angular 2+ only. Angular 1 (also known as AngularJS) has reached its EOL in January 2022 which, therefore, we no longer support.
Step 1: Install the Raygun4JS package
NPM
npm install raygun4js
Yarn
yarn add raygun4js
Bun
bun add raygun4js
Step 2: Enable Crash Reporting
Create a new script named app.raygun.setup.ts. This script will store our configuration, which enables Crash Reporting.
Create a custom error handler to send errors to Raygun instead of logging them to the console. If you want to maintain console logging, you can add a console.error
line to your handler.
import rg4js from 'raygun4js';
import { ErrorHandler } from '@angular/core';
rg4js('apiKey', 'paste_your_api_key_here');
rg4js('enableCrashReporting', true);
// Create a new ErrorHandler and report an issue straight to Raygun
export class RaygunErrorHandler implements ErrorHandler {
handleError(e: any) {
rg4js('send', {
error: e,
});
}
}
Import the setup script
Import the RaygunErrorHandler
from app.raygun.setup into app.module.ts.
import { ErrorHandler } from '@angular/core';
import { RaygunErrorHandler } from './app.raygun.setup';
// Add the custom error handler to the providers array
@NgModule({
imports: [...],
declarations: [...],
providers: [{...}, {
provide: ErrorHandler,
useClass: RaygunErrorHandler
}],
bootstrap: [...]
})
Tracking navigation errors
In your app.module.ts file, extend the AppModule
component to subscribe to Router events. Using Angular's NavigationError event we can track any navigation errors a user has encountered.
import rg4js from 'raygun4js';
import { Component, OnInit } from '@angular/core';
import { Router, NavigationError, NavigationEnd } from '@angular/router';
export class AppModule implements OnInit {
constructor(private router: Router) {}
ngOnInit() {
this.router.events.subscribe(event => {
// Track navigation errors when the NavigationError event occurs
if (event instanceof NavigationError) {
// Track navigation error
rg4js('send', {
error: event.error
});
}
});
}
}
Using Raygun with Server Side Rendering
Because server side rendering does not have access to the client-side window
object, the setup needs to be a little different.
Step 1 - Set up the Raygun4JS provider
Ensure you have added the Raygun4JS
CDN or NPM package and have enabled Crash Reporting using the instructions above before continuing.
Step 2 - Update app.raygun.setup.ts
Replace the contents of app.raygun.setup.ts with:
import rg4js from 'raygun4js';
import { ErrorHandler } from '@angular/core';
import { Injectable } from '@angular/core';
// To be injected at Client Side Render time
@Injectable()
export class RaygunErrorHandler implements ErrorHandler {
handleError(e: any) {
// @ts-ignore
window.rg4js('send', {
error: e,
});
}
}
// To be injected at Server Side Render time
@Injectable()
export class FakeRaygunErrorHandler implements ErrorHandler {
handleError(e: any) {
console.error('send', {
error: e,
});
}
}
Step 3 - Inject a fake ErrorHandler For SSR
Update your app.server.module.ts to look something like this:
import { NgModule } from '@angular/core';
import { ServerModule } from '@angular/platform-server';
import { AppModule } from './app.module';
import { AppComponent } from './app.component';
import { ErrorHandler } from '@angular/core'; // <- Add this
import { FakeRaygunErrorHandler } from './app.raygun.setup'; // <- Add this
@NgModule({
imports: [
AppModule,
ServerModule,
],
providers: [
{
provide: ErrorHandler, // <- Add this
useClass: FakeRaygunErrorHandler // <- Add this
}
],
bootstrap: [AppComponent],
})
export class AppServerModule {}
This will inject an ErrorHandler
which reports errors to the console and does not need access to the window
object.
Step 4 - Inject the real ErrorHandler to be rendered on the client side
Modify the app.module.ts file to look like this:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { HomeComponent } from './modules/general/home/home.component';
import { NotFoundComponent } from './modules/general/not-found/not-found.component';
import { AppRoutingModule } from './app-routing.module';
import { ErrorHandler } from '@angular/core'; // <- Add this
import { RaygunErrorHandler } from './app.raygun.setup'; // <- Add this
@NgModule({
declarations: [
AppComponent,
HomeComponent,
NotFoundComponent,
],
imports: [
BrowserModule.withServerTransition({ appId: 'serverApp' }),
AppRoutingModule,
],
providers: [
{
provide: ErrorHandler, // <- Add this
useClass: RaygunErrorHandler // <- Add this
}
],
bootstrap: [AppComponent]
})
export class AppModule { }
This will inject an ErrorHandler
when the page is rendered on the client side, which has access to the window
object.
So it is able to use the rg4js
to send monitoring events.
Optional - Enable Real User Monitoring
Enable Real User Monitoring to also track your website's performance.
<script type="text/javascript">
rg4js('enableRealUserMonitoring', true); // Enables Real User Monitoring
</script>
The provider is open source and available at the Raygun4js repository.