emperorsreeni / angularjs-application-insights-v2

AngularJs library for Microsoft Application Insights JavaScript V2 SDK
MIT License
2 stars 6 forks source link

Angular JS Azure Application Insights implementation for JS SDK V2

Connect your AngularJS client-side to Microsofts Application Insights with this easy-to-use Module.

Application Insights is an extensible Application Performance Management (APM) service for web developers on multiple platforms. Use it to monitor your live web application. It will automatically detect performance anomalies. It includes powerful analytics tools to help you diagnose issues and to understand what users actually do with your app.

npm NPM Downloads MIT License


Installation

NPM Setup

Install & save the library to your package.json:

$ npm install --save angularjs-application-insights-v2

Including javascript file in the script tag

Initialization Setup

applicationInsightsService.trackEvent(event: Microsoft.ApplicationInsights.IEventTelemetry, customProperties:object);
applicationInsightsService.startTrackEvent(name: string);
applicationInsightsService.stopTrackEvent(name: string, properties?: object, measurements?:object);
applicationInsightsService.trackPageView(pageView: Microsoft.ApplicationInsights.IPageViewTelemetry, customProperties?:object);
applicationInsightsService.startTrackPage(name?: string);
applicationInsightsService.stopTrackPage(name?: string, url?: string, customProperties?: object);
applicationInsightsService.trackMetric(metric: Microsoft.ApplicationInsights.IMetricTelemetry, customProperties?:object);
applicationInsightsService.trackException(exception: Microsoft.ApplicationInsights.IExceptionTelemetry, customProperties?:object);
applicationInsightsService.trackTrace(trace: Microsoft.ApplicationInsights.ITraceTelemetry, customProperties?: object);
applicationInsightsService.flush();
applicationInsightsService.setAuthenticatedUserContext(authenticatedUserId: string, accountId?: string, storeInCookie?: boolean);
applicationInsightsService.clearAuthenticatedUserContext();
applicationInsightsService._onerror(exception: Microsoft.ApplicationInsights.IAutoExceptionTelemetry);
applicationInsightsService.trackPageViewPerformance(pageViewPerformance: Microsoft.ApplicationInsights.IPageViewPerformanceTelemetry, customProperties?: object});
applicationInsightsService.addTelemetryInitializer(telemetryInitializer: (item: Microsoft.ApplicationInsights.ITelemetryItem) => boolean | void);

Example:

applicationInsightsService.trackEvent({name: 'some event'});
applicationInsightsService.trackPageView({name: 'some page'});
applicationInsightsService.trackPageViewPerformance({name : 'some page', url: 'some url'});
applicationInsightsService.trackException({exception: new Error('some error')});
applicationInsightsService.trackTrace({message: 'some trace'});
applicationInsightsService.trackMetric({name: 'some metric', average: 42});
applicationInsightsService.trackDependencyData({absoluteUrl: 'some url', responseCode: 200, method: 'GET', id: 'some id'});
applicationInsightsService.startTrackPage("pageName");
applicationInsightsService.stopTrackPage("pageName", {customProp1: "some value"});
applicationInsightsService.startTrackEvent("event");
applicationInsightsService.stopTrackEvent("event", {customProp1: "some value"});
applicationInsightsService.setAuthenticatedUserContext('ddd-ddd-dddd-dddd');
applicationInsightsService.clearAuthenticatedUserContext();
applicationInsightsService.flush();

How to send custom events to Application Insights?

How to send application traces to Application Insights?

You can send the application traces to application insights to add additional infomration to your telemetry data.


### How to add the telemetry initializer to application insights
Use can use the telemetry intializer to customize the global data which sending to applicaiton insights apart from the custom data. 
You can use this to set custom role name, operation name, user id, session id, etc.
Use the following snippets to add your telemery insitializer
```js
 app.run(["$rootScope", "$location", "applicationInsightsService",
        function ($rootScope, $location, applicationInsightsService) {
             var customTelemetryInitializer = function (envelope) {
                envelope.tags["ai.cloud.role"] = "your role name";
                envelope.tags["ai.cloud.roleInstance"] = "your role instance";
                envelope.tags["ai.operation.name"] = "customized operation name"
             }
             applicationInsightsService.addTelemetryInitializer(customTelemetryInitializer);
        }
 ]);

How to build from the Source?

To build from source follow the following instructions,

Credits

This project inspired by the following projects,

Test porject used from this tjoudeh/FoursquareAngularJS repository.

License

MIT License


Follow online:

Twitter: @tamvasan