Skip to content

Commit a853c10

Browse files
authored
Adjusts example to use middleware and initializer for targeting id (#357)
* Adjusts example to use middleware and initializer for targeting id * Update examples/EvaluationDataToApplicationInsights/README.md
1 parent 0cd3e5a commit a853c10

File tree

7 files changed

+23
-59
lines changed

7 files changed

+23
-59
lines changed

examples/EvaluationDataToApplicationInsights/EvaluationDataToApplicationInsights.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
<ItemGroup>
1414
<ProjectReference Include="..\..\src\Microsoft.FeatureManagement.AspNetCore\Microsoft.FeatureManagement.AspNetCore.csproj" />
15+
<ProjectReference Include="..\..\src\Microsoft.FeatureManagement.Telemetry.ApplicationInsights.AspNetCore\Microsoft.FeatureManagement.Telemetry.ApplicationInsights.AspNetCore.csproj" />
1516
<ProjectReference Include="..\..\src\Microsoft.FeatureManagement.Telemetry.ApplicationInsights\Microsoft.FeatureManagement.Telemetry.ApplicationInsights.csproj" />
1617
</ItemGroup>
1718

examples/EvaluationDataToApplicationInsights/HttpContextTargetingContextAccessor.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public ValueTask<TargetingContext> GetContextAsync()
2929
return new ValueTask<TargetingContext>((TargetingContext)value);
3030
}
3131

32+
//
33+
// Grab username from cookie
3234
string username = httpContext.Request.Cookies["username"];
3335

3436
var groups = new List<string>();

examples/EvaluationDataToApplicationInsights/Pages/Checkout.cshtml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77

88
<p>Click Below To Check Out!</p>
99

10-
<button id="checkout" name="checkout" onclick="appInsights.trackEvent({ name: 'checkout', properties: { 'success' : 'yes' } }); appInsights.trackMetric({ name: 'checkoutAmount', average: Math.floor(Math.random() * 100) }); alert('Checked Out!');">
10+
<script>
11+
function checkOut() {
12+
appInsights.trackEvent({ name: 'checkout', properties: { 'success': 'yes' } });
13+
appInsights.trackMetric({ name: 'checkoutAmount', average: Math.floor(Math.random() * 100) });
14+
alert('Checked Out!');
15+
}
16+
</script>
17+
18+
<button id="checkout" name="checkout" onclick="checkOut()">
1119
Check Out
12-
</button>
20+
</button>

examples/EvaluationDataToApplicationInsights/Pages/Shared/_Layout.cshtml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@
2020
<script src="~/lib/jquery/dist/jquery.min.js"></script>
2121
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
2222
<script src="~/js/site.js" asp-append-version="true"></script>
23-
<script>
24-
appInsights.setAuthenticatedUserContext(@username);
25-
</script>
2623
</head>
2724
<body>
2825
<header>

examples/EvaluationDataToApplicationInsights/Program.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
// Licensed under the MIT license.
33
//
44
using Microsoft.FeatureManagement.Telemetry.ApplicationInsights;
5-
using EvaluationDataToApplicationInsights.Telemetry;
65
using Microsoft.FeatureManagement;
76
using EvaluationDataToApplicationInsights;
87
using Microsoft.ApplicationInsights.Extensibility;
8+
using Microsoft.FeatureManagement.Telemetry.ApplicationInsights.AspNetCore;
99

1010
var builder = WebApplication.CreateBuilder(args);
1111

@@ -20,8 +20,8 @@
2020
builder.Services.AddApplicationInsightsTelemetry();
2121

2222
//
23-
// App Insights User Tagging
24-
builder.Services.AddSingleton<ITelemetryInitializer, MyTelemetryInitializer>();
23+
// App Insights TargetingId Tagging
24+
builder.Services.AddSingleton<ITelemetryInitializer, TargetingTelemetryInitializer>();
2525

2626
//
2727
// Enter feature management
@@ -55,4 +55,8 @@
5555

5656
app.MapRazorPages();
5757

58+
//
59+
// Adds Targeting Id to HttpContext
60+
app.UseMiddleware<TargetingHttpContextMiddleware>();
61+
5862
app.Run();

examples/EvaluationDataToApplicationInsights/README.md

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,10 @@ These cookies are used to correlate telemetry from the browser with telemetry fr
4646

4747
*The Javascript SDK is not required, but is useful for collecting browser telemetry and generating these cookies out of the box.*
4848

49-
### Authenticated User ID
50-
In order to connect metrics for the user between multiple services, a Authenticated User Id needs to be emitted. When the application is loaded, a login is simulated by setting a "username" cookie to a random integer. Additionally, the "ai_user" and "ai_session" cookies are expired, to simulate a new browser.
49+
### Targeting Id
50+
In order to connect evaluation events with other metrics from the user, a targeting id needs to be emitted. This can be done multiple ways, but the recommended way is to define a telemetry initializer. This initializer allows the app to modify all telemetry going to Application Insights before it's sent.
5151

52-
To include the authenticated user id on metrics emitted from the Javascript SDK, this app adds the following to _Layout.cshtml:
53-
```html
54-
appInsights.setAuthenticatedUserContext(getCookie("username"));
55-
```
56-
57-
To include it on metrics emitted from the ASP.NET SDK, this app uses a TelemetryInitializer named `MyTelemetryInitializer`:
58-
```csharp
59-
builder.Services.AddSingleton<ITelemetryInitializer, MyTelemetryInitializer>();
60-
```
61-
62-
The initializer sets the Authenticated User on the context object for all telemetry emitted from the server:
63-
```csharp
64-
telemetry.Context.User.AuthenticatedUserId = username;
65-
```
52+
This example uses the provided `TargetingHttpContextMiddleware` and `TargetingTelemetryInitializer`. The middleware adds `TargetingId` (using the targeting context accessor) to the HTTP Context as a request comes in. The initializer checks for the `TargetingId` on the HTTP Context, and if it exists, adds `TargetingId` to all outgoing Application Insights Telemetry.
6653

6754
## Sample App Usage
6855
Sample steps to try out the app:

examples/EvaluationDataToApplicationInsights/Telemetry/MyTelemetryInitializer.cs

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)