Martin Staufcik
Martin Staufcik

Reputation: 9490

How can I use AppInsights with Angular?

I am trying to use AppInsights with Angular:

import { AppInsights } from 'applicationinsights-js';

....

if (!AppInsights.config) {
    var config: Microsoft.ApplicationInsights.IConfig = {
        instrumentationKey: environment.appInsightsInstrumentationKey
    };

    AppInsights.downloadAndSetup(config);
}

// code for logging exception:
AppInsights.trackException(errorMessage, "GlobalErrorHandler", {
    UserName: userName,
    ViewName: url
}, { }, AI.SeverityLevel.Error);

// code for logging page views
AppInsights.trackPageView(name, url, {
    UserName: userName,
    ViewName: url
}, { });

I ran into two issues with this code:

  1. an exception is not tracked at all in app insights using this code, and
  2. a page view is tracked, but it does not contain the custom properties.

I tried to look into the source code of the module for AppInsights (applicationinsights-js), but could not find a solution.

Thank you for any advice.

Upvotes: 1

Views: 871

Answers (1)

Ivan Glasenberg
Ivan Glasenberg

Reputation: 29985

an exception is not tracked at all in app insights using this code

Please check if disableExceptionTracking is set to true or false.

a page view is tracked, but it does not contain the custom properties

you should try to add properties like below:

AppInsights.trackPageView(name, url, properties:{
    UserName: userName,
    ViewName: url
}, { });

And also please try to use @microsoft/applicationinsights-web packages.

Upvotes: 2

Related Questions