diff --git a/examples/EvaluationDataToApplicationInsights/EvaluationDataToApplicationInsights.csproj b/examples/EvaluationDataToApplicationInsights/EvaluationDataToApplicationInsights.csproj index f4f7bbc6..d3a84c9d 100644 --- a/examples/EvaluationDataToApplicationInsights/EvaluationDataToApplicationInsights.csproj +++ b/examples/EvaluationDataToApplicationInsights/EvaluationDataToApplicationInsights.csproj @@ -12,6 +12,7 @@ + diff --git a/examples/EvaluationDataToApplicationInsights/HttpContextTargetingContextAccessor.cs b/examples/EvaluationDataToApplicationInsights/HttpContextTargetingContextAccessor.cs index 4f79e466..eeaa16b1 100644 --- a/examples/EvaluationDataToApplicationInsights/HttpContextTargetingContextAccessor.cs +++ b/examples/EvaluationDataToApplicationInsights/HttpContextTargetingContextAccessor.cs @@ -29,6 +29,8 @@ public ValueTask GetContextAsync() return new ValueTask((TargetingContext)value); } + // + // Grab username from cookie string username = httpContext.Request.Cookies["username"]; var groups = new List(); diff --git a/examples/EvaluationDataToApplicationInsights/Pages/Checkout.cshtml b/examples/EvaluationDataToApplicationInsights/Pages/Checkout.cshtml index 28c323e2..0d750ee0 100644 --- a/examples/EvaluationDataToApplicationInsights/Pages/Checkout.cshtml +++ b/examples/EvaluationDataToApplicationInsights/Pages/Checkout.cshtml @@ -7,6 +7,14 @@

Click Below To Check Out!

- \ No newline at end of file + diff --git a/examples/EvaluationDataToApplicationInsights/Pages/Shared/_Layout.cshtml b/examples/EvaluationDataToApplicationInsights/Pages/Shared/_Layout.cshtml index 30a07f01..1ad8ffe4 100644 --- a/examples/EvaluationDataToApplicationInsights/Pages/Shared/_Layout.cshtml +++ b/examples/EvaluationDataToApplicationInsights/Pages/Shared/_Layout.cshtml @@ -20,9 +20,6 @@ -
diff --git a/examples/EvaluationDataToApplicationInsights/Program.cs b/examples/EvaluationDataToApplicationInsights/Program.cs index 41b25026..4b57a4d1 100644 --- a/examples/EvaluationDataToApplicationInsights/Program.cs +++ b/examples/EvaluationDataToApplicationInsights/Program.cs @@ -2,10 +2,10 @@ // Licensed under the MIT license. // using Microsoft.FeatureManagement.Telemetry.ApplicationInsights; -using EvaluationDataToApplicationInsights.Telemetry; using Microsoft.FeatureManagement; using EvaluationDataToApplicationInsights; using Microsoft.ApplicationInsights.Extensibility; +using Microsoft.FeatureManagement.Telemetry.ApplicationInsights.AspNetCore; var builder = WebApplication.CreateBuilder(args); @@ -20,8 +20,8 @@ builder.Services.AddApplicationInsightsTelemetry(); // -// App Insights User Tagging -builder.Services.AddSingleton(); +// App Insights TargetingId Tagging +builder.Services.AddSingleton(); // // Enter feature management @@ -55,4 +55,8 @@ app.MapRazorPages(); +// +// Adds Targeting Id to HttpContext +app.UseMiddleware(); + app.Run(); diff --git a/examples/EvaluationDataToApplicationInsights/README.md b/examples/EvaluationDataToApplicationInsights/README.md index aa8ee83c..5c64276d 100644 --- a/examples/EvaluationDataToApplicationInsights/README.md +++ b/examples/EvaluationDataToApplicationInsights/README.md @@ -46,23 +46,10 @@ These cookies are used to correlate telemetry from the browser with telemetry fr *The Javascript SDK is not required, but is useful for collecting browser telemetry and generating these cookies out of the box.* -### Authenticated User ID -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. +### Targeting Id +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. -To include the authenticated user id on metrics emitted from the Javascript SDK, this app adds the following to _Layout.cshtml: -```html -appInsights.setAuthenticatedUserContext(getCookie("username")); -``` - -To include it on metrics emitted from the ASP.NET SDK, this app uses a TelemetryInitializer named `MyTelemetryInitializer`: -```csharp -builder.Services.AddSingleton(); -``` - -The initializer sets the Authenticated User on the context object for all telemetry emitted from the server: -```csharp -telemetry.Context.User.AuthenticatedUserId = username; -``` +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. ## Sample App Usage Sample steps to try out the app: diff --git a/examples/EvaluationDataToApplicationInsights/Telemetry/MyTelemetryInitializer.cs b/examples/EvaluationDataToApplicationInsights/Telemetry/MyTelemetryInitializer.cs deleted file mode 100644 index c6ed711b..00000000 --- a/examples/EvaluationDataToApplicationInsights/Telemetry/MyTelemetryInitializer.cs +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT license. -// -using Microsoft.ApplicationInsights.Channel; -using Microsoft.ApplicationInsights.Extensibility; - -namespace EvaluationDataToApplicationInsights.Telemetry -{ - public class MyTelemetryInitializer : ITelemetryInitializer - { - private readonly IHttpContextAccessor _httpContextAccessor; - - public MyTelemetryInitializer(IHttpContextAccessor httpContextAccessor) - { - _httpContextAccessor = httpContextAccessor ?? throw new ArgumentNullException(nameof(httpContextAccessor)); - } - - public void Initialize(ITelemetry telemetry) - { - HttpContext httpContext = _httpContextAccessor.HttpContext; - - if (httpContext == null) - { - return; - } - - string username = httpContext.Request.Cookies["username"]; - - if (username != null) - { - telemetry.Context.User.AuthenticatedUserId = username; - } - } - } -}