12.0.0 • Published 2 years ago

ng-appinsights v12.0.0

Weekly downloads
93
License
MIT
Repository
github
Last release
2 years ago

ng-appinsights

A simple wrapper of Application Insights JS Library for Angular applications.

Supported features are:

  • Simple setup (no modules required)
  • Deferred Initialization
  • Built-in Global Error Handler

Installation

Using npm:

npm install --save ng-appinsights

Usage

To initialize Application Insights, add the following to your entry point module (usually app.module.ts):

import { APP_INSIGHTS_CONFIG } from 'ng-appinsights';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule
  ],
  providers: [
    {
      provide: APP_INSIGHTS_CONFIG,
      useValue: {
        instrumentationKey: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx',
        enableAutoRouteTracking: true,
        // Visit https://github.com/microsoft/ApplicationInsights-JS to know all possible configurations.
      }
    }
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

Import the AppInsightsService into your components and use the available tracking methods.

import { Component } from '@angular/core';
import { AppInsightsService } from 'ng-appinsights';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html'
})
export class AppComponent {
  count = 0;

  constructor(private appInsights: AppInsightsService) {
  }

  increment(): void {
    this.count++;
    this.appInsights.trackEvent('increment', { count: this.count });
  }

  decrement(): void {
    this.count--;
    this.appInsights.trackEvent('decrement', { count: this.count });
  }
}

Deferred Initialization

You may also skip the configuration on the Module and do it using the Service at any time.

import { Component } from '@angular/core';
import { AppInsightsService } from 'ng-appinsights';

@Component({
  selector: 'app-root',
  templateUrl: './my.component.html'
})
export class MyComponent {
  constructor(private appInsights: AppInsightsService) {
  }

  init(): void {
    this.appInsights.init({
      instrumentationKey: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx',
    });
  }
}

Global Error Handler

You may also include the built-in Error Handler which will automatically send exception events to AI for every errors that occurs inside your application.

import { AppInsightsErrorHandler } from 'ng-appinsights';

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

2 years ago

11.2.0

2 years ago

11.1.0

3 years ago

11.0.0

4 years ago

10.0.0

4 years ago

9.0.0

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago