如何在 Azure App Insights 中为页面视图事件提供自定义名称?

Igo*_*voy 5 azure azure-application-insights

默认情况下,App Insights 使用页面标题作为事件名称。拥有动态页面名称,例如“Order 32424”,会产生大量的事件类型。

关于此事的文档说使用 trackEvent 方法,但没有示例。

appInsights.trackEvent("Edit button clicked", { "Source URL": "http://www.contoso.com/index" })
Run Code Online (Sandbox Code Playgroud)

最好的方法是什么?拥有某种地图/过滤器将是完美的,它允许将某些页面的事件名称修改为共享名称,例如“订单 23424”=>“订单”,同时让大多数页面保持原样。

Dmi*_*eev 2

您应该能够利用遥测初始化程序方法将事件名称中的某些模式替换为该名称的更“常见”版本。

\n\n

以下是来自 Application Insights JS SDK GitHub 的示例,介绍如何在发送 pageView 数据之前对其进行修改。通过稍加修改,您可以使用它根据事件的外观更改事件名称:

\n\n
window.appInsights = appInsights;\n...\n// Add telemetry initializer\nappInsights.queue.push(function () {\n    appInsights.context.addTelemetryInitializer(function (envelope) {\n        var telemetryItem = envelope.data.baseData;\n\n        // To check the telemetry item\xe2\x80\x99s type:\n        if (envelope.name === Microsoft.ApplicationInsights.Telemetry.PageView.envelopeType) {\n            // this statement removes url from all page view documents\n            telemetryItem.url = "URL CENSORED";\n        }\n\n        // To set custom properties:\n        telemetryItem.properties = telemetryItem.properties || {};\n        telemetryItem.properties["globalProperty"] = "boo";\n\n        // To set custom metrics:\n        telemetryItem.measurements = telemetryItem.measurements || {};\n        telemetryItem.measurements["globalMetric"] = 100;\n    });\n});\n// end\n\n...\nappInsights.trackPageView();\nappInsights.trackEvent(...);\n
Run Code Online (Sandbox Code Playgroud)\n