Javascript 无法添加到Application Insight中的自定义遥测初始值设定项

Javascript 无法添加到Application Insight中的自定义遥测初始值设定项,javascript,azure,react-native,azure-application-insights,Javascript,Azure,React Native,Azure Application Insights,我正在开发React本机应用程序,希望使用应用程序洞察来记录数据。为此,我静态地包含了AI JS SDK脚本。日志工作正常,我能够记录跟踪和异常。但是,我有一个要求,即对于每个日志(跟踪、异常或事件),需要添加一些特定的自定义属性 为了实现这一点,我正在尝试添加一个自定义遥测初始值设定项(我将遵循这一点)。问题是,我收到一个错误,指出AppInsights.queue.push未定义。这是我的全部代码: import Microsoft from "./ai.0"; type Props = {

我正在开发React本机应用程序,希望使用应用程序洞察来记录数据。为此,我静态地包含了AI JS SDK脚本。日志工作正常,我能够记录跟踪和异常。但是,我有一个要求,即对于每个日志(跟踪、异常或事件),需要添加一些特定的自定义属性

为了实现这一点,我正在尝试添加一个自定义遥测初始值设定项(我将遵循这一点)。问题是,我收到一个错误,指出
AppInsights.queue.push
未定义。这是我的全部代码:

import Microsoft from "./ai.0";
type Props = {};
export default class App extends Component<Props> {
  AppInsights = null;
  constructor(props) {
    super(props);
    var snippet = {
      config: {
        instrumentationKey: "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
      }
    };
    var init = new Microsoft.ApplicationInsights.Initialization(snippet);
    this.AppInsights = init.loadAppInsights();
    this.AppInsights.trackPageView("First Page", null, {Comp: "App"},{hit:"1"}, 100); //This step is working

    this.AppInsights.queue.push(function() { //This is where it breaks;
      this.AppInsights.context.addTelemetryInitializer(function(envelope) {
        var telemetryItem = envelope.data.baseData;

        telemetryItem.Properties = telemetryItem.Properties || {};
        telemetryItem.Properties["MyCustomProperty_1"] = "This is a custom property";
        telemetryItem.Properties["MyCustomProperty_2"] = "This is another custom property";
      });
    });
  }
从“/ai.0”导入Microsoft;
类型Props={};
导出默认类应用程序扩展组件{
AppInsights=null;
建造师(道具){
超级(道具);
变量代码段={
配置:{
仪表键:“XXXXXXXX-XXXX-XXXX-XXXX-XXXX-XXXXXXXXXX”
}
};
var init=新的Microsoft.ApplicationInsights.Initialization(代码段);
this.AppInsights=init.loadAppInsights();
this.AppInsights.trackPageView(“第一页”,null,{Comp:“App”},{hit:“1”},100);//此步骤正在工作
this.AppInsights.queue.push(函数(){//这是它的断点;
this.AppInsights.context.addTelemetryInitializer(函数(信封){
var telemetryItem=信封.data.baseData;
telemetryItem.Properties=telemetryItem.Properties | |{};
telemetryItem.Properties[“MyCustomProperty_1”]=“这是一个自定义属性”;
telemetryItem.Properties[“MyCustomProperty_2”]=“这是另一个自定义属性”;
});
});
}
我错过了什么

错误屏幕截图

我也遇到了同样的问题,当代码段被调用两次,并且在第二次调用时,就不再有“队列”了。解决方法是用一个复选框来包装代码段

if (this.AppInsights.queue) {
        this.AppInsights.queue.push(function() { //This is where it breaks;
        this.AppInsights.context.addTelemetryInitializer(function(envelope) {
        var telemetryItem = envelope.data.baseData;

        telemetryItem.Properties = telemetryItem.Properties || {};
        telemetryItem.Properties["MyCustomProperty_1"] = "This is a custom property";
        telemetryItem.Properties["MyCustomProperty_2"] = "This is another custom property";
      });
    });
}

这有点旧,但不需要直接访问队列或上下文。您只需直接从
AppInsightsInitializer
实例调用
addTelemetryInitializer

init.addTelemetryInitializer((envelope) => {
        var telemetryItem = envelope.data.baseData;

        telemetryItem.Properties = telemetryItem.Properties || {};
        telemetryItem.Properties["MyCustomProperty_1"] = "This is a custom property";
        telemetryItem.Properties["MyCustomProperty_2"] = "This is another custom property";
      });

另外,请确保在初始化AppInsights后添加过滤器(通过调用
loadAppInsights
)。

让我试试,但我可以通过直接添加遥测初始值设定项(即,没有将其推到队列中)找到解决方法,只编写了这个.AppInsights.context.add(…)并删除此.AppInsights.queue.push方法。Nope不起作用。错误已消失,但未插入自定义属性。在上面的示例中,请尝试初始化并查看此。AppInsights.queue=[];是否尝试过?它消除了错误,但数据未通过。回答正确!