Tracking Authenticated User ID in Application Insights

After getting the basic out-of-the-box inplementation of Application Insights (AI) up and running, I decided to customize it to pass the ID of the current user along with the telemetry.  The first couple of articles I discovered recommended writing a custom Telemetry Initializer to populate the Telemetry.Context.AuthenticatedUserId property.  Since my website uses ASP.NET forms authentication, I was able to grab the ID of the current user from the HttpContext.Current.User property.  My initializer ended up looking something like this:

  public class MyTelemetryInitializer : ITelemetryInitializer  
  {  
     public void Initialize(ITelemetry telemetry)  
     {  
        var identity = HttpContext.Current?.User?.Identity;  
        if (identity == null) return;  
        telemetry.Context.User.AuthenticatedUserId = identity.Name;
     }  
  }  

Then I register my initializer using the following line of code in the app_start function of my Global.asax.cs file.

  TelemetryConfiguration.Active.TelemetryInitializers.Add(new MyTelemetryInitializer());

Now the ID of the current user is sent to the AI server with each event that is sent.  To accomplish the same thing on the client side, I called the setAuthenticatedUserContext function passing in the current user ID as follows:

  import { ApplicationInsights } from '@microsoft/applicationinsights-web';

  const currentUserId = userService.getCurrentUser().id;

  const appInsights = new ApplicationInsights({ config: {  
    instrumentationKey: 'YOUR_INSTRUMENTATION_KEY_GOES_HERE'  
    /* ...Other Configuration Options... */  
  }});  

  appInsights.loadAppInsights();
  appInsights.setAuthenticatedUserContext(currentUserId);
  appInsights.trackPageView();

I use the npm package to include AI in my client-side code.  One nonce here is that the ID passed to the setAuthenticatedUserContext must be a string otherwise it will not work

And there you have it... well not exactly.

There is another way!

While snooping around in my ApplicationInsights.config file, I noticed an initilizer called AuthenticatedUserIdTelemetryInitializer which is part of the Microsoft.AI.Web assembly.  So I try to disable my custom initializer to see if the the authenticated user is still set on my server-side AI events.  It was not!  

After hours of Googling the only information I could find on this initializer was this one line description from Microsoft:

- Sets the AuthenticatedUserId property as set by the JavaScript SDK

I was setting the Authenticated User ID on the client-side so I was baffled as to why this initializer was not working.  So I dug in to the source code and discovered that it attempts to read the user ID from a cookie named ai_authUser.  I check the developer console and that cookie did not exist despite setting the user ID in my JavaScript.  So I drill into the source code of the JavaScript SDK.  Low and behold there are two optional parameters on the setAuthenticatedUserContext function, one of which is called storeInCookie.  So I changed my JavaScript code to look like this:

  appInsights.setAuthenticatedUserContext(currentUserId, null, true);

And magically, the user ID of the current users is again sent with my server-side telemetry events.  Just make sure you call clearAuthenticatedUserContext when the user signs out.

Conclusion

So there you have it, two separate ways to send the ID of the current user to Application Insights.

Comments

Popular posts from this blog

Introduction to Reactive Extensions

Writing Your First Rx Statement