Creating Cohorts Using Custom Properties in Application Insights

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();

Save the changes and run the application. Now, when you navigate to the application URL with the userType query parameter, the CustomerType property will be added to the telemetry. Send the following HTTP requests to the application to create telemetry items with the CustomerType property set to different values.

http://<URL of the service>/?userType=Gold
http://<URL of the service>/?userType=Silver

Creating Cohorts

You will now define a cohort of operations that have been performed by users with the selected CustomerType property. To create a cohort, open the Application Insights resource in the Azure portal and navigate to the "Cohorts" section.

Creating Cohorts

Next, define the name of the cohort, then click on the "Edit" button to define the cohort's parameters. Click on the "Add Parameter" button to add a new parameter to the cohort as shown below.

Add Parameter

In the "Add Parameter" dialog, set the details of the parameter as shown below.

New parameter

The query text is as follows.

requests
    | distinct tostring(customDimensions.CustomerType)

Click on the "Run Query" button to view the distinct values that the CustomerType property in the telemetry has. Next, click on the "Save" button to save the cohort and click on the "Done Editing" button to close the cohort parameter editor.

Next, launch the result editor by clicking on the "Edit" button and define the query to filter the "Operation Id" based on the CustomerType property as shown below.

Customer Type property

The query text is as follows.

requests
| where tostring(customDimensions.CustomerType) in ({CustomerType})

The query filters the operations based on the selected values of the CustomerType property shown in the cohort parameter dropdown. Click on the "Done Editing" button to save the cohort and close the result editor.

Set the "CustomerType" parameter to "Gold" to limit the results.

Finally, click on the "Save" button to save the cohort. You have now created a cohort that filters operations based on the CustomerType property in the telemetry.

You can now use the cohort to filter the telemetry in the Application Insights resource. For example, to view the sessions of users that belong to the "Gold" cohort vs. others, click on the "Sessions" tab and set the x-axis to the cohort you created as shown below.

Sessions tab

Conclusion

In this article, you learned to add custom properties to your application's telemetry and then use these properties to create cohorts in Application Insights. You can use cohorts to filter telemetry based on custom properties and create custom reports to analyze the data. This can help you to understand the behavior of specific groups of users and make informed decisions about your application.