How can I send custom complex non-string properties to Telemetry to Azure Portal with App Insights TrackEvent in basic Javascript (not NodeJS)?
I initialized the Application Insights JavaScript SDK through the following setup snippet:
<script type='text/javascript'>
var appInsights=window.appInsights||function(config)
{
function r(config){ t[config] = function(){ var i = arguments; t.queue.push(function(){ t[config].apply(t, i)})} }
var t = { config:config},u=document,e=window,o='script',s=u.createElement(o),i,f;for(s.src=config.url||'//az416426.vo.msecnd.net/scripts/a/ai.0.js',u.getElementsByTagName(o)[0].parentNode.appendChild(s),t.cookie=u.cookie,t.queue=[],i=['Event','Exception','Metric','PageView','Trace','Ajax'];i.length;)r('track'+i.pop());return r('setAuthenticatedUserContext'),r('clearAuthenticatedUserContext'),config.disableExceptionTracking||(i='onerror',r('_'+i),f=e[i],e[i]=function(config, r, u, e, o) { var s = f && f(config, r, u, e, o); return s !== !0 && t['_' + i](config, r, u, e, o),s}),t
}({
instrumentationKey: "@Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.Active.InstrumentationKey"
});
window.appInsights=appInsights;
appInsights.trackPageView();
</script>
I tried the example from here https://github.com/microsoft/ApplicationInsights-JS and here https://learn.microsoft.com/en-us/azure/azure-monitor/app/api-custom-events-metrics#properties but without success:
appInsights.trackEvent({
name: 'EventName',
properties: { // accepts any type
prop1: 'string',
prop2: 123.45,
prop3: {
nested: 'objects are okay too'
}
}
});
In the Ajax track request sent to Azure the sent payload has this form:
ver: 2
name:
name: 'EventName'
properties:
prop1: 'string'
prop2: 123.45
prop3:
nested: 'objects are okay too'
And in the Azure Portal App Insights, I get this:
CUSTOM EVENT
Event name [object Object]
I also get an Javascript Warning in the Console:
Logging.ts:206 AI: CannotSerializeObjectNonSerializable
message:"Attempting to serialize an object which does not implement ISerializable"
props:"{name:name}"
I was able to send only by specifying name
property separately and only string typed properties and only one-level nested properties.
Successful tests:
appInsights.trackEvent("EventName1", { properties: 'something' });
appInsights.trackEvent("EventName2", { prop1: 'something', prop2: 'prop2' });
Unsuccessful tests:
appInsights.trackEvent("EventName3", { prop1: 'prop1', nestedProp2: {prop2: 'prop2'} });
appInsights.trackEvent('EventName4', { properties: { dataToSend: 'something' }, measurements: {prop1: 'prop1', prop2: 'prop2'}});
The problem is with the SDK version.
In Azure portal and in
tags
property from browser AJAX calls it shows that i'm using the 1.0.21 SDK version, and those examples are for 2.5.3 SDK version, which are not backward compatible.The solution was to upgrade to the latest SDK version by replacing the snippet setup (the main difference would be in changing url
az416426.vo.msecnd.net/scripts/a/ai.0.js
with this oneaz416426.vo.msecnd.net/scripts/b/ai.2.min.js
).The new Snippet Setup:
Now, the following Cutom Event is sent and received in Azure portal App Insights as expected: