Customizing AppInsights end-user location recording

If you are using AppInsights JavaScript SDK, you probably noticed that all telemetry has location associated with it.

1

 

It is generally very handy when investigating region-specific issues or analyzing end-user client performance, however sometimes compliance or other reasons prevent you from recording end-user location or you would like to override it based on your custom logic.

Location is determined by our data collection endpoint from the IP address when SDK is issuing dc.services.visuastudio.com/track request. However it only uses the IP of the request, when IP in the telemetry item context is not specified. So when you specify IP address as part of telemetry item, it uses this IP to determine location, and, when this IP is specified as 0, it doesn't resolve the end-user location. To specify custom IP for all telemetry with JS SDK, add this telemetry initializer immediately after the snippet and before trackPageViewCall().

 window.appInsights = appInsights;
        // Add telemetry initializer
        appInsights.queue.push(function () {
        appInsights.context.addTelemetryInitializer(function (envelope) {
          envelope.tags[ "ai.location.ip" ] =  "0" ; //0 to hide or an IP address to resolve to location
        });
        });
        // end of insertion
        appInsights.trackPageView();

For more information on telemetry initializers for JavaScript SDK, please see this blog post.

Feedback, comments? Let me know what you think.