gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 01:05:28 26 4
gpt4 key购买 nike

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

为了实现这一目标,我尝试添加一个自定义遥测初始值设定项(我正在关注此 link )。问题是我收到一条错误,指出 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";
});
});
}

我在这里缺少什么?

错误屏幕截图 enter image description here

最佳答案

这有点旧,但是不需要直接访问队列或上下文。您可以直接从 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)。

关于javascript - 无法添加到 Application Insight 中的自定义遥测初始值设定项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49676251/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com