Skip to content

Azure Application Insights – trackEvent with custom properties in Javascript

We are using the client-side javascript SDK (via the snippet-based integration) for Application Insights to track custom events

appInsights.trackEvent({name:"WinGame"});

How can I add custom properties to this? Something like this?!

appInsights.trackEvent({name:"WinGame", customProperties:{gameName:"Game 1", player:"my player 1"}});

Answer

As seen on the JS Azure appinsights github page:

Custom properties can be included in your telemetry through the properties named argument. This can be done with any of the Track APIs.

appInsights.trackEvent({   
    name: 'some event',  
    properties: { // accepts any type
       prop1: 'string',
       prop2: 123.45,
       prop3: { nested: 'objects are okay too' }   
    } 
});