ApplicationInsights-JS icon indicating copy to clipboard operation
ApplicationInsights-JS copied to clipboard

[BUG] AI web sdk is using user-agent in request header to determine the OS not reading from context

Open AnuragTiwari1 opened this issue 6 months ago • 3 comments

Description/Screenshot I am using Web insight sdk in my react native application and web application. while in web the user agent is getting passed correctly so we are getting OS in application insights as iOS 18.5 for iOS and Android 15 for android phone. same devices when running the react native app is showing just iOS for iPhone and Other for Android. Steps to Reproduce

  • OS/Browser: React Native application, iOS and android
  • SDK Version [e.g. 22]: 3.3.6
  • How you initialized the SDK:
  const appInsights = new ApplicationInsights({
       config: {
         instrumentationKey: AppConstant.MICROSOFT_APPLICATION_INSIGHT_INSTRUMENTATION_KEY,
         extensions: [RNPlugin],
       }
     });
     appInsights.loadAppInsights();
     appInsights.config.enableAjaxErrorStatusText = true;
     appInsights.config.enableAutoRouteTracking = true;
     appInsights.config.autoTrackPageVisitTime = true;
     appInsights.context.os = {name: 'Android'};

Expected behavior there should be some way to override this. I see in ai context there is OS.name, I tried setting that as well but it's not respecting it

AnuragTiwari1 avatar Jun 17 '25 03:06 AnuragTiwari1

for now i am using below patch and have applied customHeaders for user-agent

diff --git a/node_modules/@microsoft/applicationinsights-web/dist/es5/applicationinsights-web.js b/node_modules/@microsoft/applicationinsights-web/dist/es5/applicationinsights-web.js
index b36b191..201c08a 100644
--- a/node_modules/@microsoft/applicationinsights-web/dist/es5/applicationinsights-web.js
+++ b/node_modules/@microsoft/applicationinsights-web/dist/es5/applicationinsights-web.js
@@ -11004,7 +11004,7 @@
                             _throwInternal(diagLog, 1 , 100 , "Invalid Instrumentation key " + _instrumentationKey);
                         }
                         _customHeaders = senderConfig[_DYN_CUSTOM_HEADERS ];
-                        if (isString(_endpointUrl) && !isInternalApplicationInsightsEndpoint(_endpointUrl) && _customHeaders && _customHeaders[_DYN_LENGTH$1 ] > 0) {
+                        if (isString(_endpointUrl) && _customHeaders && _customHeaders[_DYN_LENGTH$1 ] > 0) {
                             arrForEach(_customHeaders, function (customHeader) {
                                 _this.addHeader(customHeader.header, customHeader.value);
                             });
const appInsights = new ApplicationInsights({
  config: {
    instrumentationKey: AppConstant.MICROSOFT_APPLICATION_INSIGHT_INSTRUMENTATION_KEY,
    extensions: [RNPlugin],
    customHeaders: [
      {
        header: 'User-Agent',
        value: 'Android 15'
      }
    ]
  }
});

AnuragTiwari1 avatar Jun 17 '25 06:06 AnuragTiwari1

Hi @AnuragTiwari1 one way is to use addTelemtryinitializer https://github.com/microsoft/ApplicationInsights-JS#telemetry-initializers this would allow to modify the telemetry Item right before sending

Karlie-777 avatar Jun 23 '25 23:06 Karlie-777

Thanks @Karlie-777, From the API call I had seen OS was not getting passed and after overriding header it was confirmed that it is getting read from there.

Still I will check this and update here as soon as I can.

AnuragTiwari1 avatar Jun 25 '25 07:06 AnuragTiwari1