How to provide custom names for page view events in Azure App Insights?

白昼怎懂夜的黑 提交于 2020-01-21 19:13:09

问题


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" })

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.


回答1:


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.

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(...);



回答2:


With help of Dmitry Matveev I've came with the following final code:

var appInsights = window.appInsights;

if (appInsights && appInsights.queue) {
    function adjustPageName(item) {
        var name = item.name.replace("AppName", "");

        if (name.indexOf("Order") !== -1)
            return "Order";

        if (name.indexOf("Product") !== -1)
            return "Shop";

        // And so on...

        return name;
    }

    // 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 || envelope.name === Microsoft.ApplicationInsights.Telemetry.PageViewPerformance.envelopeType) {

                // Do not track admin pages
                if (telemetryItem.name.indexOf("Admin") !== -1)
                    return false;

                telemetryItem.name = adjustPageName(telemetryItem);
            }

        });
    });
}

Why this code is important? Because App Insights use page titles by default as Name for PageView, so you would have hundreds and thousands of different events, like "Order 123132" which would make further analysis (funnel, flows, events) meaningless.

Key highlights:

  • var name = item.name.replace("AppName", ""); If you put your App/Product name in title, you probably want to remove it from you event name, because it would just repeat itself everywhere.
  • appInsights && appInsights.queue you should check for appInsights.queue because for some reason it can be not defined and it would cause an error.
  • if (telemetryItem.name.indexOf("Admin") !== -1) return false; returning false will cause event to be not recorded at all. There certain events/pages you most likely do not want to track, like admin part of website.
  • There are two types of events which use page title as event name: PageView and PageViewPerformance. It makes sense to modify both of them.


来源:https://stackoverflow.com/questions/53778932/how-to-provide-custom-names-for-page-view-events-in-azure-app-insights

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!