In Application Insights, a cohort is a group of users, sessions, events, or operations that share common properties. Cohorts are created using an analytics query. If you need to analyze a specific set of users or events repeatedly, cohorts can provide more flexibility to express the exact set of users, sessions, events, or operations, you are interested in. Cohorts are similar to filters, but since they are defined by custom analytics queries, they are more adaptable and complex. Unlike filters, you can save cohorts for later use by other members of your team. The Microsoft documentation provides further information on the differences between cohorts and filters.
In this article, you will learn to add custom properties to your application's telemetry and then use these properties to create cohorts.
Creating Application Insights Telemetry Initializer
To begin with, you need to create a new ASP.NET Core minimal API project and configure Application Insights. If you are unfamiliar with the setup process, you can refer to the "Application Insights for ASP.NET Core applications guide" guide to set up Application Insights in your project.
Next, you will create a telemetry initializer to add custom properties to your application's telemetry before it is sent to Application Insights. To create a telemetry initializer, define a class named CustomTelemetryInitializer that inherits from the TelemetryInitializerBase class in your project. Next, override the Initialize method to add a custom property named CustomerType to the telemetry, as shown below.
using Microsoft.ApplicationInsights.AspNetCore.TelemetryInitializers;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
public class CustomTelemetryInitializer(IHttpContextAccessor httpContextAccessor) : TelemetryInitializerBase(httpContextAccessor)
{
protected override void OnInitializeTelemetry(
HttpContext platformContext,
RequestTelemetry requestTelemetry,
ITelemetry telemetry)
{
if (platformContext.Request.Query.TryGetValue("userType", out var value))
{
telemetry.Context.GlobalProperties.TryAdd("CustomerType", value);
}
}
}
In the above code, the CustomerType property is set to the value of the userType query parameter in the request URL. This property will be added to all telemetry items sent to Application Insights.
To register the CustomTelemetry Initializer class with the Application Insights SDK, you need to add it to the services collection in the Program.cs file as shown below.
using Microsoft.ApplicationInsights.Extensibility;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddApplicationInsightsTelemetry();
// Add the custom telemetry initializer.
builder.Services.AddSingleton<ITelemetryInitializer, CustomTelemetryInitializer>();
// Build the ASP.NET Core web application.
var app = builder.Build();
app.MapGet("/", (HttpRequest request) => $"Customer type: {request.Query["userType"]}");
// Run the application.
app.Run();






Join the conversation! Your thoughts help the community grow.