Skip to content

Commit 17e733c

Browse files
author
Sergey Kanzhelev
committed
fix example of using ASP.NET session
1 parent 45520b1 commit 17e733c

File tree

1 file changed

+88
-39
lines changed

1 file changed

+88
-39
lines changed

articles/application-insights/app-insights-usage-send-user-context.md

Lines changed: 88 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,33 @@ ms.pm_owner: daviste;NumberByColors
1616
ms.author: daviste
1717
---
1818

19-
# Send user context IDs to enable usage experiences in Azure Application Insights
19+
# Send user context IDs to enable usage experiences in Azure Application Insights
2020

2121
## Tracking users
2222

23-
Application Insights enables you to monitor and track your users through a set of product usage tools:
24-
* [Users, Sessions, Events](https://docs.microsoft.com/azure/application-insights/app-insights-usage-segmentation)
25-
* [Funnels](https://docs.microsoft.com/azure/application-insights/usage-funnels)
26-
* [Retention](https://docs.microsoft.com/azure/application-insights/app-insights-usage-retention)
27-
* Cohorts
28-
* [Workbooks](https://docs.microsoft.com/azure/application-insights/app-insights-usage-workbooks)
23+
Application Insights enables you to monitor and track your users through
24+
a set of product usage tools:
25+
26+
- [Users, Sessions, Events](https://docs.microsoft.com/azure/application-insights/app-insights-usage-segmentation)
27+
- [Funnels](https://docs.microsoft.com/azure/application-insights/usage-funnels)
28+
- [Retention](https://docs.microsoft.com/azure/application-insights/app-insights-usage-retention)
29+
Cohorts
30+
- [Workbooks](https://docs.microsoft.com/azure/application-insights/app-insights-usage-workbooks)
31+
32+
In order to track what a user does over time, Application Insights needs
33+
an ID for each user or session. Include the following IDs in every
34+
custom event or page view.
2935

30-
In order to track what a user does over time, Application Insights needs an ID for each user or session. Include the following IDs in every custom event or page view.
3136
- Users, Funnels, Retention, and Cohorts: Include user ID.
3237
- Sessions: Include session ID.
3338

3439
If your app is integrated with the [JavaScript SDK](https://docs.microsoft.com/azure/application-insights/app-insights-javascript#set-up-application-insights-for-your-web-page), user ID is tracked automatically.
3540

3641
## Choosing user IDs
3742

38-
User IDs should persist across user sessions to track how users behave over time. There are various approaches for persisting the ID.
43+
User IDs should persist across user sessions to track how users behave
44+
over time. There are various approaches for persisting the ID.
45+
3946
- A definition of a user that you already have in your service.
4047
- If the service has access to a browser, it can pass the browser a cookie with an ID in it. The ID will persist for as long as the cookie remains in the user's browser.
4148
- If necessary, you can use a new ID each session, but the results about users will be limited. For example, you won't be able to see how a user's behavior changes over time.
@@ -46,50 +53,92 @@ If the ID contains personally identifying information about the user, it is not
4653

4754
## ASP.NET apps: Setting the user context in an ITelemetryInitializer
4855

49-
Create a telemetry initializer, as described in detail [here](https://docs.microsoft.com/azure/application-insights/app-insights-api-filtering-sampling#add-properties-itelemetryinitializer), and set the Context.User.Id and the Context.Session.Id.
56+
Create a telemetry initializer, as described in detail [here](https://docs.microsoft.com/azure/application-insights/app-insights-api-filtering-sampling#add-properties-itelemetryinitializer). Pass the session ID through the request telemetry, and set the Context.User.Id and the Context.Session.Id.
5057

5158
This example sets the user ID to an identifier that expires after the session. If possible, use a user ID that persists across sessions.
5259

53-
```csharp
54-
55-
using System;
56-
using System.Web;
57-
using Microsoft.ApplicationInsights.Channel;
58-
using Microsoft.ApplicationInsights.Extensibility;
60+
### Telemetry initializer
5961

60-
namespace MvcWebRole.Telemetry
62+
```csharp
63+
using System;
64+
using System.Web;
65+
using Microsoft.ApplicationInsights.Channel;
66+
using Microsoft.ApplicationInsights.Extensibility;
67+
68+
namespace MvcWebRole.Telemetry
69+
{
70+
/*
71+
* Custom TelemetryInitializer that sets the user ID.
72+
*
73+
*/
74+
public class MyTelemetryInitializer : ITelemetryInitializer
75+
{
76+
public void Initialize(ITelemetry telemetry)
6177
{
62-
/*
63-
* Custom TelemetryInitializer that sets the user ID.
64-
*
65-
*/
66-
public class MyTelemetryInitializer : ITelemetryInitializer
67-
{
68-
public void Initialize(ITelemetry telemetry)
78+
var ctx = HttpContext.Current;
79+
80+
// If telemetry initializer is called as part of request execution and not from some async thread
81+
if (ctx != null)
6982
{
70-
// For a full experience, track each user across sessions. For an incomplete view of user
71-
// behavior within a session, store user ID on the HttpContext Session.
72-
// Set the user ID if we haven't done so yet.
73-
if (HttpContext.Current.Session["UserId"] == null)
83+
var requestTelemetry = ctx.GetRequestTelemetry();
84+
85+
// Set the user and session ids from requestTelemetry.Context.User.Id, which is populated in Application_PostAcquireRequestState in Global.asax.cs.
86+
if (requestTelemetry != null && !string.IsNullOrEmpty(requestTelemetry.Context.User.Id) &&
87+
(string.IsNullOrEmpty(telemetry.Context.User.Id) || string.IsNullOrEmpty(telemetry.Context.Session.Id)))
7488
{
75-
HttpContext.Current.Session["UserId"] = Guid.NewGuid();
89+
// Set the user id on the Application Insights telemetry item.
90+
telemetry.Context.User.Id = requestTelemetry.Context.User.Id;
91+
92+
// Set the session id on the Application Insights telemetry item.
93+
telemetry.Context.Session.Id = requestTelemetry.Context.User.Id;
7694
}
95+
}
96+
}
97+
}
98+
}
99+
```
77100

78-
// Set the user id on the Application Insights telemetry item.
79-
telemetry.Context.User.Id = (string)HttpContext.Current.Session["UserId"];
101+
### Global.asax.cs
80102

81-
// Set the session id on the Application Insights telemetry item.
82-
telemetry.Context.Session.Id = HttpContext.Current.Session.SessionID;
103+
```csharp
104+
using System.Web;
105+
using System.Web.Http;
106+
using System.Web.Mvc;
107+
using System.Web.Optimization;
108+
using System.Web.Routing;
109+
110+
namespace MvcWebRole.Telemetry
111+
{
112+
public class MvcApplication : HttpApplication
113+
{
114+
protected void Application_Start()
115+
{
116+
AreaRegistration.RegisterAllAreas();
117+
GlobalConfiguration.Configure(WebApiConfig.Register);
118+
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
119+
RouteConfig.RegisterRoutes(RouteTable.Routes);
120+
BundleConfig.RegisterBundles(BundleTable.Bundles);
121+
}
122+
123+
protected void Application_PostAcquireRequestState()
124+
{
125+
var requestTelemetry = Context.GetRequestTelemetry();
126+
127+
if (HttpContext.Current.Session != null && requestTelemetry != null && string.IsNullOrEmpty(requestTelemetry.Context.User.Id))
128+
{
129+
requestTelemetry.Context.User.Id = Session.SessionID;
130+
}
83131
}
84-
}
85132
}
133+
}
86134
```
87135

88136
## Next steps
137+
89138
- To enable usage experiences, start sending [custom events](https://docs.microsoft.com/azure/application-insights/app-insights-api-custom-events-metrics#trackevent) or [page views](https://docs.microsoft.com/azure/application-insights/app-insights-api-custom-events-metrics#page-views).
90139
- If you already send custom events or page views, explore the Usage tools to learn how users use your service.
91-
* [Usage overview](app-insights-usage-overview.md)
92-
* [Users, Sessions, and Events](app-insights-usage-segmentation.md)
93-
* [Funnels](usage-funnels.md)
94-
* [Retention](app-insights-usage-retention.md)
95-
* [Workbooks](app-insights-usage-workbooks.md)
140+
- [Usage overview](app-insights-usage-overview.md)
141+
- [Users, Sessions, and Events](app-insights-usage-segmentation.md)
142+
- [Funnels](usage-funnels.md)
143+
- [Retention](app-insights-usage-retention.md)
144+
- [Workbooks](app-insights-usage-workbooks.md)

0 commit comments

Comments
 (0)