applicationinsights clickanalytics.js npm - add custom property

103 views Asked by At

I am using https://www.npmjs.com/package/@microsoft/applicationinsights-clickanalytics-js to send click event as telemetry data to application insights.

What I need to do is to send for all the events a custom propery - for example a tenant id - that I can use in the provided visualizations/queries to do group bys.

enter image description here

Honestly I do not find a way on how to do this. I found there are possiblities to use a TelemetryProcessor or something like

appInsights.defaultClient.commonProperties = {
   environment: process.env.SOME_ENV_VARIABLE
};

But I find no way on how to achive this with the npm package.

Any hints, idea? Basically I want to add a custom property that is written with every autogenerated or manual event.

1

There are 1 answers

2
Stefan Starke On

For future generations stuck with the same issue ;)

I solved it like this

  const telemetryInitializer = (envelope) => {
                const user = getUserFromSomewhere();
                if (!user) {
                    console.log("User is not set")
                    return false;
                }
                envelope.tags["ai.cloud.roleInstance"] = user?.tenant;
                return true;
            };
            appInsights.addTelemetryInitializer(telemetryInitializer);