ApplicationInsights-JS icon indicating copy to clipboard operation
ApplicationInsights-JS copied to clipboard

[BUG] Custom properties added with addTelemetryInitializer are ignored for first PageVisitTime track event

Open Nosfit opened this issue 2 years ago • 2 comments

Description/Screenshot When I set autoTrackPageVisitTime to true in the ApplicationInsights configuration, the first payload for PageVisitTime does not contain any custom parameters that I added using the addTelemetryInitialiser method.

Screenshot 2023-10-26 at 15 01 54

Steps to Reproduce init telemetry with custom properties:

const appInsights = new ApplicationInsights({
      config: {
        appId: 'test-bug-report',
        connectionString: this.connectionString,
        autoTrackPageVisitTime: true,
      },
    });
    appInsights.loadAppInsights();
    appInsights.trackPageView();
    appInsights.addTelemetryInitializer(this.enrichTelemetry);

    ///

   enrichTelemetry = (item: ITelemetryItem): void => {
      if (item.tags == null) {
        item.tags = [];
      }
      item.tags['ai.cloud.role'] = 'test-bug-report-roleName';
      item.tags['ai.cloud.roleInstance'] = 'test-bug-report-roleInstance';
    };

then visit your application and see on first track request in network section.

  • OS/Browser: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36'
  • SDK Version [e.g. 22]: "@microsoft/applicationinsights-web": "^3.0.4"
  • How you initialized the SDK:

Expected behavior

Additional context Add any other context about the problem here.

Nosfit avatar Oct 26 '23 12:10 Nosfit

Yes, this is because you are adding the telemetry initializer "AFTER" you are triggering the page view

flip the order of these 2 functions

    appInsights.trackPageView();
    appInsights.addTelemetryInitializer(this.enrichTelemetry);

to

    appInsights.addTelemetryInitializer(this.enrichTelemetry);
    appInsights.trackPageView();

MSNev avatar Oct 26 '23 19:10 MSNev

Thanks. That helps

Nosfit avatar Oct 30 '23 17:10 Nosfit