question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support `setAuthenticatedUserContext`

See original GitHub issue

The ApplicationInsights-JS SDK supports a method called setAuthenticatedUserContext which sends a custom auth token with each event.

The client code is basically just doing some validation then updating the ai.user.authUserId tag.

On the server, it looks like the same thing can be done, but is not documented:

let client = appInsights.defaultClient;
let key = client.context.keys.userAuthUserId;
client.context.tags[key] = <authToken>;

Providing this same method on the server side would be helpful for several reasons:

  • A users client and server activity can be tied together by the custom auth id
  • SDK users don’t have to figure this out themselves
  • SDK consistency

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:4
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
glenjamincommented, Jul 3, 2018

This is what I ended up going with

appInsights.defaultClient.addTelemetryProcessor(attachUserId);
function attachUserId(envelope, context) {
  const res = context['http.ServerResponse'];
  if (res && res.locals && res.locals.user) {
    envelope.tags['ai.user.authUserId'] = res.locals.user.username;
  }
}
2reactions
OsvaldoRosadocommented, Dec 1, 2017

Great catch!

The JS SDK can also provide this userId to the server-side using the “ai_authUser” cookie. It’s my understanding that our .NET SDK will read this cookie and automatically set the authUserId that it received from the JS SDK. It would be nice to implement that here as well.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Tracking your authenticated users with Azure AppInsights
setAuthenticatedUserContext (userId);. This will create a cookie that will track your authenticated user on each event/page view/request. The ...
Read more >
Application Insights API for custom events and metrics
Use the Application Insights core telemetry API to send custom events and metrics and your own versions of standard telemetry. This API is...
Read more >
Application insights setAuthenticatedUserContext with API ...
I have Application Insights configured on both the front and back-end of my website, which operate on different sub-domains (let's say www.
Read more >
Azure Application Insights
... to create an Azure Application Insights service and hook it up with your ... to setAuthenticatedUserContext() passing a user.id and role.
Read more >
Power Apps Portals - Application Insights Authenticated Users
Would be great if anyone could help getting back on track . ... "];f.length;)b("track"+f.pop());if(b("setAuthenticatedUserContext") ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found