简体   繁体   English

如何在 JavaScript 中为网页加载和卸载 Azure App Insights

[英]How to load and unload Azure App Insights for web pages in JavaScript

I have followed this guide and this guide to add Azure App Insights to our Angular application.我已按照本指南和本指南将 Azure App Insights 添加到我们的 Angular 应用程序。 It works great but the problem I am having is how can we start/load and stop/unload tracking of the insights conditionally?它工作得很好,但我遇到的问题是我们如何有条件地启动/加载和停止/卸载洞察力跟踪?

Basically, we have a toggle in our application that allows the user to turn on collection of data and Application Insights should be analyzing and collecting the data.基本上,我们的应用程序中有一个开关,允许用户打开数据收集,Application Insights 应该分析和收集数据。 Once the user turns this toggle off, it should stop analyzing and tracking.一旦用户关闭此开关,它应该停止分析和跟踪。

It seems once we call this.appInsights.loadAppInsights() , there is no way to unlatch/unload/stop listening.似乎一旦我们调用this.appInsights.loadAppInsights() ,就无法解锁/卸载/停止监听。 If there is a way to unlatch/unload/stop listening, please let me know.如果有办法解锁/卸载/停止收听,请告诉我。

Thanks.谢谢。

You can refer to the doc Disabling telemetry .您可以参考文档禁用遥测

For example, if you want to unload app insights in javascript, use the code below:例如,如果您想在 javascript 中卸载应用洞察,请使用以下代码:

telemetry.config.disableAppInsights = true;

I ended up creating a common service related to everything with AppInsights inspired from here .我最终创建了一个与 AppInsights 相关的通用服务,灵感来自这里

import { AngularPlugin, AngularPluginService } from '@microsoft/applicationinsights-angularplugin-js';
import {
  ApplicationInsights,
  IEventTelemetry,
  IExceptionTelemetry
 } from '@microsoft/applicationinsights-web';
....

export class ApplicationInsightService {
  private appInsights: ApplicationInsights;

  constructor(private router: Router, private angularPluginService: AngularPluginService) {
    const angularPlugin = new AngularPlugin();
    this.angularPluginService.init(angularPlugin, this.router);
    this.appInsights = new ApplicationInsights({ config: {
      instrumentationKey: 'Your key here',
      extensions: [angularPlugin],
      extensionConfig: {
        [angularPlugin.identifier]: { router: this.router },
      }
    }});
    this.appInsights.loadAppInsights();
    this.appInsights.trackPageView();
  }

  setUserId(userId: string) {
    this.appInsights.setAuthenticatedUserContext(userId);
  }

  clearUserId() {
    this.appInsights.clearAuthenticatedUserContext();
  }

  logPageView(name?: string, uri?: string) {
    this.appInsights.trackPageView({ name, uri });
  }

  logEvent(event: IEventTelemetry, customProperties: { [key: string]: any }) {
    this.appInsights.trackEvent(event, customProperties);
  }

  trackError(exception: Error) {
    let telemetry = {
      exception,
    } as IExceptionTelemetry;
    this.appInsights.trackException(telemetry);
  }

  stopTelemetry() { // !! this is how you stop tracking
    this.appInsights.config.disableTelemetry = true;
  }

  startTelemetry() { // !! this is how you start/re-start it
    this.appInsights.config.disableTelemetry = false;
  }
}

The JavaScript part of the documentation was not helpful because those methods/properties didn't exist but the C# documentation helped and it seems to be similar to that. 文档的 JavaScript 部分没有帮助,因为这些方法/属性不存在,但 C# 文档有帮助,而且似乎与此类似。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM