Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customize automatic Track Page View events #323

Open
Shaker96 opened this issue Sep 30, 2024 · 0 comments
Open

Customize automatic Track Page View events #323

Shaker96 opened this issue Sep 30, 2024 · 0 comments

Comments

@Shaker96
Copy link

Shaker96 commented Sep 30, 2024

Hello,

I am trying to customize the automatic track page view events. Need to modify the name param from "not_specified" to something else and also add some custom data to the properties param of the IPageViewTelemetry

In an attempt at this I created a custom telemetryInitializer class

using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;

public class CustomTelemetryInitializer : ITelemetryInitializer
{
    private readonly IServiceProvider _serviceProvider;

    public CustomTelemetryInitializer(IServiceProvider serviceProvider)
    {
        _serviceProvider = serviceProvider;
    }

    public void Initialize(ITelemetry telemetry)
    {
        if (telemetry is PageViewTelemetry pageViewTelemetry)
        {
            if (string.IsNullOrEmpty(pageViewTelemetry.Name) || pageViewTelemetry.Name == "not_specified")
            {
                pageViewTelemetry.Name = "QA Name";
            }

            pageViewTelemetry.Properties["CompanyName"] = "Pancakes";
        }
    }
}

// in program.cs
builder.Services.AddBlazorApplicationInsights(config =>
{
    config.ConnectionString = builder.Configuration["ApplicationInsights:ConnectionString"];
    config.AutoTrackPageVisitTime = true;

}, async applicationInsights =>
{
    var telemetryItem = new TelemetryItem()
    {
        Tags = new Dictionary<string, object?>()
        {
            { "ai.cloud.role", "SPA" },
            { "ai.cloud.roleInstance", "Blazor Server" },
        }
    };

    await applicationInsights.AddTelemetryInitializer(telemetryItem);
});

builder.Services.AddSingleton<ITelemetryInitializer, CustomTelemetryInitializer>();

This does intercept some telemetry from the app, but not the ones triggered by this library, I realize that BlazorApplicationInsights' implementation of AddTelemetryInitializer receives a TelemetryItem instead of a ITelemetryInitializer.

Thank you in advance for any assistance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant