How can I access appInsights.context from javascript code

658 Views Asked by At

I want to display the UserId on the website, so users can communicate it to us whenever they encounter a problem. However, in code, appInsights.context is undefined. How can I access this?

    window.appInsights = appInsights;
    appInsights.trackPageView();
    console.log(appInsights.context);

is logged as undefined. However, I can access it in the developer console when I run the application. It seems like the context is being removed/not added yet.

1

There are 1 best solutions below

0
On

I believe this is because the appInsights context is not necessarily created right away?

you may need to do this work "asynchronously" by adding events to the ai queue:

appInsights.queue.push(function () {
    console.log(appInsights.context);
});