首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在JavaScript中加载和卸载Azure应用程序洞察网页

如何在JavaScript中加载和卸载Azure应用程序洞察网页
EN

Stack Overflow用户
提问于 2020-11-03 20:47:04
回答 2查看 926关注 0票数 3

我遵循这个指南和这个指南添加Azure到我们的角度应用程序。它很好,但我遇到的问题是我们如何有条件地启动/加载和停止/卸载跟踪洞察力?

基本上,我们的应用程序中有一个切换,允许用户打开数据收集,application应该分析和收集数据。一旦用户关闭这个开关,它就应该停止分析和跟踪。

似乎一旦我们调用了this.appInsights.loadAppInsights(),就没有办法解除锁存/卸载/停止侦听。如果有办法解除锁存/卸载/停止监听,请告诉我。

谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-11-16 00:28:29

您可以参考失能遥测文档。

例如,如果您想卸载javascript中的应用程序洞察力,请使用下面的代码:

代码语言:javascript
运行
复制
telemetry.config.disableAppInsights = true;
票数 1
EN

Stack Overflow用户

发布于 2020-11-13 15:42:04

最后,我用来自AppInsights的这里创建了一个与所有事物相关的公共服务。

代码语言:javascript
运行
复制
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;
  }
}

JavaScript部分对文档没有帮助,因为这些方法/属性并不存在,但是C#文档提供了帮助,而且它似乎与此类似。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64670400

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档