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

[BUG] Application insight to monitor intranet sites through browser and viva connections

Open abdjyg opened this issue 1 year ago • 1 comments

Description/Screenshot[BUG] I have deployed application insight spfx extenstion to 50 intranet sites(sharepoint sites). This is capturing the data properly when users access the site from browser. But it doesn't work properly when users access intranet sites through viva connections in teams app

Steps to Reproduce

  • OS/Browser:
  • SDK Version [e.g. 22]:
  • How you initialized the SDK:

Expected behavior

Additional context When users access sitepages through viva connections, it can't track the pageviews until the user refreshes the page. Also if they click like button or post a comment, that isn't being tracked properly.

abdjyg avatar Sep 06 '24 06:09 abdjyg

HI @abdjyg, we have no expertise in this team (the SDK team) on specifically running inside of viva connections... But based on your comments let me take a stab at what is going on as I think / suggest that you will need to reach out to viva support to find out how they are implemented.

Basically, the SDK is designed to run in a browser "environment" (that is any Javascript environment that provides the same basic runtime API that you would see in a browser (and to a more limited extent node) -- something like electron is an example of this.

As such we rely on the normal page life cycle and events to initialize, track clicks and for page view specifically we "listen" to the load (or onload) event (unless you have also enable enableAutoRouteTracking in which case we also listen to the navigator location history events -- used for SPA's but by default the duration of these is 0 as we don't have access to any "standard" metrics for calculating how long the framework took to "change the page" (ie. hide / remove <div's> on the page.

Based on this comment

When users access sitepages through viva connections, it can't track the pageviews until the user refreshes the page.

It sounds like when running in viva there is "no" onload event being fired unless the physical page is reloaded, when it's not it sounds (or seems) like viva is fetching the page via a fetch or XMLHttpRequest and then dynamically adding the content to the current page (ie no page load event), (if) they are changing the Browser URL (even with a # then enabling the above config "might" help you track the "pageview", if not then some additional manual coding is required to call the trackPageView(), but I don't anything about SPX or Viva to suggest where this (if any) hooks might exist to do this.

Also if they click like button or post a comment, that isn't being tracked properly. For this one it's a little problematic, as for this to "work" 2 things need to be true

  • The buttons NEED to be running in the same context (frame) as the SDK, if they are using iFrame to host these buttons then that frame will also need to have an SDK instance loaded and initialized, otherwise we won't see the click events
  • (more likely and common), to avoid adding "click" events listeners to every object on the page and detecting when components are added / removed, for tracking these click events we "listen" (hook) for them at the document / window level. This means that for us to "detect" the clicks the events need to "bubble" all of the way up to the window / document otherwise we don't see them. Practically, this means that if the event handler for the element on the page is calling the event stopPropagation() then we will never get the event, assuming you can't change the framework the only solution to this is for the existing event handle to either send it's own "trackEvent" or to somehow add your own event handler before the default one (or you won't get called either, depend on the event functions that it is calling)

MSNev avatar Sep 06 '24 16:09 MSNev

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 May 05 '25 23:05 github-actions[bot]