Coder Social home page Coder Social logo

goenning / ng-appinsights Goto Github PK

View Code? Open in Web Editor NEW
2.0 3.0 2.0 1.15 MB

Connect your Angular applications to Azure Application Insights to automatically track dependency calls, performance metrics and error logging. Use custom events to track anything you want!

License: MIT License

JavaScript 14.95% TypeScript 80.15% HTML 4.29% CSS 0.62%
application-insights angular azure microsoft-application-insights appinsights

ng-appinsights's Introduction

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 { }

ng-appinsights's People

Contributors

goenning avatar patrickkk avatar wyniecronje avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

ng-appinsights's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.