在cordova插件中为Application Insights的上下文添加属性



我使用cordova插件与应用程序洞察力命名cordova-plugin-ms-appinsights (https://github.com/MSOpenTech/cordova-plugin-ms-appinsights/blob/master/README.md),我试图将我的属性添加到上下文,通过每个请求应用程序将发送额外的信息,例如我的应用程序的代码名称。

我试了如下:

appInsights.context.application。Codename ="app代号";appInsights。trackEvent("事件");

,这不起作用。我可以在上下文中添加其他信息吗?

有两个问题:

1) Cordova插件似乎使用了旧版本的JS SDK。你可以尝试手动更新后,它拉下旧的(采取最新的https://github.com/Microsoft/ApplicationInsights-JS/blob/master/dist/ai.0.js)

2)向所有遥测项目添加数据的功能尚未发布。它是最近实现的-参见github上的JS SDK提交。你可以稍等一会儿,直到它发布,或者从master获取最新版本,然后自己编译(并从/JavaScript/min/ai.min.js获取结果)

一个hack的替代方案可能是在SDK方法上创建一个包装器,如trackEvent(),它添加了你需要的数据(我很抱歉给你JS SDK代码等效,因为我没有使用cordova插件自己):

// this is you implementation of custom data to be attached
// to all telemetry items.
// It needs to return JSON - as it's expected format for custom properties.
// In this specific case you'll create a custom property 'hey' with value 'yo'
var getMyDataJson = function() {
  return { hey: "yo"};
}    
// this is your wrapper, you'll call it instead of appInsights.trackEvent()
var myTrackEvent = function(data) {
  var toBeAttachedToAllItems = getMyDataJson();
  appInsights.trackEvent(data, toBeAttachedToAllItems);
}
<...>
// somewhere later you track your telemetry
// this will call your getMyDataJson() function which'll attach
// custom data to your event.
myTrackEvent("tracked!")

最新更新