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

[BUG] Click analytics plug in sends same click event multiple times to Application Insight

Open abdjyg opened this issue 1 year ago • 5 comments

Description/Screenshot We have deployed App insight JS SDK to our Intranet application. It's a SharePoint hub site and multiple sites are associated to this hub site. We have added click analytics plug in to the SDK to track all click events especially to track if a user likes or comments a SharePoint page. But sometimes if we open a site page from news/announcement webparts and click like or comment button, it sends this data multiple times. So that it creates duplicate rows in App Insight customevents logs.

Steps to Reproduce

OS/Browser: Windows/ edge or Chrome SDK Version [e.g. 22]: How you initialized the SDK: Expected behavior

Additional context Add any other context about the problem here.

abdjyg avatar May 27 '24 04:05 abdjyg

This normally occurs because there are multiple SDK instances that have been loaded / initialized, each time the SDK is initialized it registers the event handles (the click, mouse down event for the click analytics) in this case.

Can you please check that this sequence is not occurring, as I suspect this is the root cause.

MSNev avatar May 28 '24 15:05 MSNev

The issue occurs when another site page is opened from a site's news web part. Does this mean that the SDK in both sites gets initialized multiple times? If so, how can we resolve this issue?

abdjyg avatar May 29 '24 15:05 abdjyg

The issue occurs when another site page is opened from a site's news web part. Does this mean that the SDK in both sites gets initialized multiple times? If so, how can we resolve this issue?

That does sounds like both are loading their own instance. I've not done any sharepoint development / configuration, but the high level options as I see it would be either (and I may be misrepresenting the sharepoint names because I don't know the environment)

  • If one webpart is always a child, then it shouldn't be loading the click analytics -- maybe it doesn't even need to include the AI SDK at all.
  • If you can run a script on the page before dropping on the SDK Loader / initializing the SDK then you could check some "global" value for the presence of your SDK instance (ie. don't necessarily rely on the window.appInsights (just in case someone else has loaded it).
    • I believe that SharePoint is also using our internal extension (called oneDs) which extends the ApplicationInsights code , so checking for the global presence of the "classes" would also not be recommended.
  • If there is some master "layout" template (thingy) then only add the SDK to that (assumes that it's used for everything).

MSNev avatar May 29 '24 15:05 MSNev

The issue occurs when another site page is opened from a site's news web part. Does this mean that the SDK in both sites gets initialized multiple times? If so, how can we resolve this issue?

That does sounds like both are loading their own instance. I've not done any sharepoint development / configuration, but the high level options as I see it would be either (and I may be misrepresenting the sharepoint names because I don't know the environment)

  • If one webpart is always a child, then it shouldn't be loading the click analytics -- maybe it doesn't even need to include the AI SDK at all.

  • If you can run a script on the page before dropping on the SDK Loader / initializing the SDK then you could check some "global" value for the presence of your SDK instance (ie. don't necessarily rely on the window.appInsights (just in case someone else has loaded it).

    • I believe that SharePoint is also using our internal extension (called oneDs) which extends the ApplicationInsights code , so checking for the global presence of the "classes" would also not be recommended.
  • If there is some master "layout" template (thingy) then only add the SDK to that (assumes that it's used for everything).

This is an SPFx extension which is deployed to the hub site and all associated sites.

abdjyg avatar May 30 '24 12:05 abdjyg

I'm seeing something similar. Not with click analytics, but with custom events (trackEvent). Not ALL events are getting duplicated, just an event that gets tracked immediately before opening a new tab. I'm 99% sure we're not calling trackEvent twice.

The events are perfect dupes-- no difference in any fields.

A workaround that works so far is to use window.setTimeout to call trackEvent after a short delay.

mrequenes avatar Jul 02 '24 17:07 mrequenes

This Issue will be closed in 30 days. Please remove the "Stale" label or comment to avoid closure with no action.

github-actions[bot] avatar Apr 29 '25 07:04 github-actions[bot]