Events
Apr 8, 3 PM - May 28, 7 AM
Sharpen your AI skills and enter the sweepstakes to win a free Certification exam
Register now!This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
In addition to the core SDK, there are also plugins available for specific frameworks, such as the React plugin, the React Native plugin, and the Angular plugin.
These plugins provide extra functionality and integration with the specific framework.
The Angular plugin for the Application Insights JavaScript SDK enables:
To add a plug-in, follow the steps in this section.
npm install @microsoft/applicationinsights-angularplugin-js
Note
On March 31, 2025, support for instrumentation key ingestion will end. Instrumentation key ingestion will continue to work, but we'll no longer provide updates or support for the feature. Transition to connection strings to take advantage of new capabilities.
Set up an instance of Application Insights in the entry component in your app:
Important
When using the ErrorService, there is an implicit dependency on the @microsoft/applicationinsights-analytics-js
extension. you MUST include either the '@microsoft/applicationinsights-web'
or include the @microsoft/applicationinsights-analytics-js
extension. Otherwise, unhandled exceptions caught by the error service will not be sent.
import { Component } from '@angular/core';
import { ApplicationInsights } from '@microsoft/applicationinsights-web';
import { AngularPlugin } from '@microsoft/applicationinsights-angularplugin-js';
// *** Add the Click Analytics plug-in. ***
// import { ClickAnalyticsPlugin } from '@microsoft/applicationinsights-clickanalytics-js';
import { Router } from '@angular/router';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
constructor(
private router: Router
){
var angularPlugin = new AngularPlugin();
// *** Add the Click Analytics plug-in. ***
/* var clickPluginInstance = new ClickAnalyticsPlugin();
var clickPluginConfig = {
autoCapture: true
}; */
const appInsights = new ApplicationInsights({
config: {
connectionString: 'YOUR_CONNECTION_STRING_GOES_HERE',
// *** If you're adding the Click Analytics plug-in, delete the next line. ***
extensions: [angularPlugin],
// *** Add the Click Analytics plug-in. ***
// extensions: [angularPlugin, clickPluginInstance],
extensionConfig: {
[angularPlugin.identifier]: { router: this.router }
// *** Add the Click Analytics plug-in. ***
// [clickPluginInstance.identifier]: clickPluginConfig
}
}
});
appInsights.loadAppInsights();
}
}
If you want to add the Click Analytics plug-in:
Uncomment the lines for Click Analytics.
Do one of the following, depending on which plug-in you're adding:
extensions: [reactPlugin],
.extensions: [RNPlugin]
.extensions: [angularPlugin],
.See Use the Click Analytics plug-in to continue with the setup process.
This section covers configuration settings for the framework extensions for Application Insights JavaScript SDK.
Name | Type | Required? | Default | Description |
---|---|---|---|---|
router | object | Optional | null | Angular router for enabling Application Insights PageView tracking. |
The following code example shows how to enable tracking of router history.
import { Component } from '@angular/core';
import { ApplicationInsights } from '@microsoft/applicationinsights-web';
import { AngularPlugin } from '@microsoft/applicationinsights-angularplugin-js';
import { Router } from '@angular/router';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
constructor(
private router: Router
){
var angularPlugin = new AngularPlugin();
const appInsights = new ApplicationInsights({ config: {
connectionString: 'YOUR_CONNECTION_STRING',
extensions: [angularPlugin],
extensionConfig: {
[angularPlugin.identifier]: { router: this.router }
}
} });
appInsights.loadAppInsights();
}
}
To track uncaught exceptions, set up ApplicationinsightsAngularpluginErrorService in app.module.ts
:
Important
When using the ErrorService, there is an implicit dependency on the @microsoft/applicationinsights-analytics-js
extension. you MUST include either the '@microsoft/applicationinsights-web'
or include the @microsoft/applicationinsights-analytics-js
extension. Otherwise, unhandled exceptions caught by the error service will not be sent.
import { ApplicationinsightsAngularpluginErrorService } from '@microsoft/applicationinsights-angularplugin-js';
@NgModule({
...
providers: [
{
provide: ErrorHandler,
useClass: ApplicationinsightsAngularpluginErrorService
}
]
...
})
export class AppModule { }
Chain more custom exception handlers when you want to want the application to gracefully handle what would previously have been an unhandled exception, but you still want to report this exception as an application failure.
To chain more custom exception handlers:
Create custom exception handlers that implement IErrorService.
import { IErrorService } from '@microsoft/applicationinsights-angularplugin-js';
export class CustomErrorHandler implements IErrorService {
handleError(error: any) {
...
}
}
Pass errorServices array through extensionConfig.
extensionConfig: {
[angularPlugin.identifier]: {
router: this.router,
errorServices: [new CustomErrorHandler()]
}
}
The device information, which includes Browser, OS, version, and language, is already being collected by the Application Insights web package.
N/A
Check out the Application Insights Angular demo.
This section provides answers to common questions.
The browser passes the User Agent string in the HTTP header of the request. The Application Insights ingestion service uses UA Parser to generate the fields you see in the data tables and experiences. As a result, Application Insights users are unable to change these fields.
Occasionally, this data might be missing or inaccurate if the user or enterprise disables sending User Agent in browser settings. The UA Parser regexes might not include all device information. Or Application Insights might not have adopted the latest updates.
Events
Apr 8, 3 PM - May 28, 7 AM
Sharpen your AI skills and enter the sweepstakes to win a free Certification exam
Register now!