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

[BUG] trackEvent return undefined

Open madebymt opened this issue 2 years ago • 4 comments

Description/Screenshot Trying to track click event but appInsight.trackEvent(eventName) return undefined, and will not show up at the log

Steps to Reproduce

  1. npm install package
  2. AppInsight file, so I can call it for places have button click

AppInsight.js

import { ApplicationInsights } from '@microsoft/applicationinsights-web';

export class TrackAppInsights {
  constructor() {
    this.appInsight = new ApplicationInsights({
      config: {
        connectionString:  'staging.url',
        enableAutoRouteTracking: true,
      },
    });
  }

  initAppInsights() {
    this.appInsight.loadAppInsights();
  }

  trackAppEvent(event) {
    console.log('app', this.appInsight)  // log result in image 1
    console.log('app-2', this.appInsight.appInsights); // log result in image 2
    console.log('this.appInsight', this.appInsight.trackEvent(event));   // <---- show undefined
    this.appInsight.trackEvent(event);
  }
}

test.js

import { AppInsightsService } from './AppInsight';
const appInsights = new AppInsightsService();

.....
// When button click I use this code
appInsights.trackAppEvent({
   name: 'click-test',
});
  • OS/Browser: Chrome 120.0
  • SDK Version [e.g. 22]: "@microsoft/applicationinsights-web": "^3.0.0",
  • How you initialized the SDK: npm

Expected behavior

  • Should show up in the app insight report --> transaction search ---> custom event select it

Image 1 Screenshot 2023-05-16 at 3 06 04 PM

image 2 Screenshot 2023-05-16 at 3 05 57 PM

Thanks for any suggestion.

madebymt avatar May 16 '23 20:05 madebymt

I'm not sure I'm following... console.log('this.appInsight', this.appInsight.trackEvent(event)); // <---- show undefined This is expected as the this.appInsight.trackEvent(event); doesn't return anything so it will always be undefined

Depending on the content of the event your passing in it might be that it's considered to be invalid, for some reason but...

Should show up in the app insight report --> transaction search ---> custom event select it I Agree, and the usage you have with the name looks fine.

You might want to install the chrome debug extension https://github.com/microsoft/ApplicationInsights-JS/tree/main/tools/chrome-debug-extension which not only shows the sent events but will also show some internal error messages and failures return from the endpoint.

MSNev avatar May 16 '23 20:05 MSNev

Thanks! I'll check that.

madebymt avatar May 16 '23 20:05 madebymt

I download it, do see any particular error still don't see custom event log in app insight. Screenshot 2023-05-16 at 3 48 37 PM Screenshot 2023-05-16 at 3 50 32 PM

madebymt avatar May 16 '23 20:05 madebymt

The screen shot is only showing the notifications (telling me that the internal network requests are being sent), as the events are not showing I assume that you have the "Network" unchecked.

image

If you enable that you can use the "Filter..." text box to just type in your "click-test" (without the quotes) to find all references in the instance. If it's not being sent (and as you are not seeing any errors) then will probably need to debug into the code to see why the event is getting dropped / ignored (as the provided code fragments should work)

MSNev avatar May 16 '23 20:05 MSNev