如何在Azure App Insights中为页面视图事件提供自定义名称? [英] How to provide custom names for page view events in Azure App Insights?
问题描述
默认情况下,App Insights使用页面标题作为事件名称.具有动态页面名称(例如"Order 32424")会创建大量的事件类型.
By default App Insights use page title as event name. Having dynamic page names, like "Order 32424", creates insane amount of event types.
Documentation on the matter says to use trackEvent method, but there are no examples.
appInsights.trackEvent("Edit button clicked", { "Source URL": "http://www.contoso.com/index" })
什么是最好的方法?拥有某种地图/过滤器将允许将某些页面的事件名称修改为共享名称,例如"Order 23424" =>"Order",同时保留大多数页面,这将是完美的.
What is the best approach? It would be perfect to have some sort of map/filter which would allow to modify event name for some pages to the shared name, like "Order 23424" => "Order", at the same time to leave most pages as they are.
推荐答案
You should be able to leverage telemetry initializer approach to replace certain pattern in the event name with the more "common" version of that name.
这是Application Insights JS SDK GitHub上的示例,说明如何在发送pageView数据之前对其进行修改.稍作修改,您就可以根据事件的外观来更改它的名称:
Here is the example from Application Insights JS SDK GitHub on how to modify pageView's data before it's sent out. With the slight modification you may use it to change event names based on their appearance:
window.appInsights = appInsights;
...
// Add telemetry initializer
appInsights.queue.push(function () {
appInsights.context.addTelemetryInitializer(function (envelope) {
var telemetryItem = envelope.data.baseData;
// To check the telemetry item’s type:
if (envelope.name === Microsoft.ApplicationInsights.Telemetry.PageView.envelopeType) {
// this statement removes url from all page view documents
telemetryItem.url = "URL CENSORED";
}
// To set custom properties:
telemetryItem.properties = telemetryItem.properties || {};
telemetryItem.properties["globalProperty"] = "boo";
// To set custom metrics:
telemetryItem.measurements = telemetryItem.measurements || {};
telemetryItem.measurements["globalMetric"] = 100;
});
});
// end
...
appInsights.trackPageView();
appInsights.trackEvent(...);
这篇关于如何在Azure App Insights中为页面视图事件提供自定义名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!