Creating Funnels Using Custom Events in Application Insights

In Application Insights, a funnel refers to the progression through a series of steps in a web application. These steps represent user interactions or events, and analyzing them as a funnel helps gain insights into user behavior and monitor step-by-step conversion rates. For example, in an e-commerce application, a funnel could represent the steps a user takes to complete a purchase, such as adding items to the cart, entering shipping and billing information, and finally completing the purchase.

Funnels help you understand how users progress through your application, identify bottlenecks, and optimize the user experience. In Application Insights, you can create funnels using custom events to represent the steps in your application. Custom events are user-defined events that you can send to Application Insights to represent specific user interactions or steps in your application. You can then use these events to create funnels and analyze user behavior. To learn more about funnels in Application Insights, you can refer to the Microsoft documentation on Funnels in Application Insights.

In this article, you will learn to add custom events to a simple .NET application's telemetry and then use these events to create funnels that identify users who complete a specific sequence of steps in your application.

Prerequisites

To complete this tutorial, you will need:

  • An Azure subscription. If you don't have an Azure subscription, create a free account before you begin.
  • An Application Insights resource. If you don't have an Application Insights resource, create one by following the instructions in Create an Application Insights resource.
  • Visual Studio Code or any other code editor.

Sending Custom Events to Application Insights

Firstly, you need to create a new ASP.NET Core minimal API project and configure Application Insights. If you are unfamiliar with the setup process, refer to the "Application Insights for ASP.NET Core applications guide" guide to help you set up Application Insights in your project.

After that, you will need to send custom events to Application Insights to represent the steps in your funnel. You can use the TelemetryClient class provided by the Application Insights SDK to send custom events. This class provides methods to send custom events, metrics, and exceptions to Application Insights.

Ensure that you have the Microsoft.ApplicationInsights.AspNetCore package installed in your project, and then add the following code to your Program.cs file.

using Microsoft.ApplicationInsights;

var builder = WebApplication.CreateBuilder(args);

// Add Application Insights telemetry services to the container.
builder.Services.AddApplicationInsightsTelemetry();

// Build the ASP.NET Core web application.
var app = builder.Build();

app.MapGet("/{id}", (HttpRequest request, int id, TelemetryClient telemetry) => telemetry.TrackEvent("ViewProduct", new Dictionary<string, string> { { "id", id.ToString() } }));

app.MapPost("purchase/{id}", (HttpRequest request, int id, TelemetryClient telemetry) => telemetry.TrackEvent("PurchaseProduct", new Dictionary<string, string> { { "id", id.ToString() } }));

// Run the application.
app.Run();

In the above code, the TrackEvent method sends custom events to Application Insights. The ViewProduct event is sent when a user views a product, and the PurchaseProduct event is sent when a user purchases a product. Both events include a custom property named id that represents the product identifier. We will use these events to track the users who view a product and then purchase the product vs. those who view the product but do not purchase it.

Save the changes and run the application. Now, when different users navigate to the application URL with the product identifier in the URL, the ViewProduct event will be sent to Application Insights. Similarly, when users make a POST request to the /purchase/{id} endpoint, the PurchaseProduct event will be sent to Application Insights.

Creating Funnels in Application Insights

After sending the custom events to Application Insights, you can create funnels to analyze user behavior and monitor step-by-step conversion rates. To create a funnel, navigate to the Application Insights resource in the Azure portal and then click on the "Funnels" option in the "Usage" section. In the "Funnels" blade, enter the "ViewProduct" event as the first step and the "PurchaseProduct" event as the second step of the funnel, as shown below.

Creating Funnel

Click on the "View" button to view the funnel. The funnel will display the number of users who completed each step of the funnel and the conversion rate between each step, as shown below.

View Funnel

The result shows that 3 users viewed the product, and 2 of them purchased it, resulting in a 66.67% conversion rate.

Further Analysis

To identify the products that are not being purchased, click on the "Logs" option in the "Usage" section and then run the following query to view the products that are being viewed but not purchased.

let viewProductEvents = customEvents
| where name == "ViewProduct"
| project productName = tostring(customDimensions["id"]);
let purchaseProductEvents = customEvents
| where name == "PurchaseProduct"
| project productName = tostring(customDimensions["id"]);
viewProductEvents
| join kind=anti purchaseProductEvents on $left.productName == $right.productName
| summarize count() by productName
| order by productName asc
| render piechart;

The query uses the id custom property to join the ViewProduct and PurchaseProduct events and then displays the products that are being viewed but not purchased in a pie chart as shown below.

Analysis

From the results, you can identify the product with the identifier. 102 has the most views but no purchases. You can then use this information to optimize the user experience and increase the conversion rate for this product.

Conclusion

In this article, you learned to create funnels using custom events in Application Insights to analyze user behavior and monitor step-by-step conversion rates. You learned to send custom events from a .NET application to Application Insights to represent the steps in your application and then use these events to create funnels. You also learned to analyze the funnel results further to identify bottlenecks and optimize the user experience.


Similar Articles