Skip to content

Conversation

@tjementum
Copy link
Member

Summary & Motivation

Fix duplicate page view tracking that occurred when navigating between pages in the single-page application. Application Insights' enableAutoRouteTracking feature was causing duplicate tracking with TanStack Router because both systems independently detected navigation events.

  • Disable enableAutoRouteTracking in Application Insights configuration
  • Create custom PageTracker component that integrates properly with TanStack Router
  • Track initial page load and subsequent navigations without duplicates
  • Maintain existing JavaScript error tracking functionality

The enableAutoRouteTracking feature is designed for React Router and listens to browser history changes (popstate, pushState/replaceState, hashchange). Since TanStack Router also manipulates browser history, both were detecting the same navigation event and sending duplicate page views. The custom PageTracker component uses TanStack Router's onLoad event and tracks pathname changes to ensure each navigation is tracked exactly once.

Downstream projects

  1. Add the PageTracker component to your-self-contained-system/__root.tsx:
import { queryClient } from "@/shared/lib/api/client";
+import { PageTracker } from "@repo/infrastructure/applicationInsights/PageTracker";
import { AuthenticationProvider } from "@repo/infrastructure/auth/AuthenticationProvider";

function Root() {
  return (
    <QueryClientProvider client={queryClient}>
      <ThemeModeProvider>
        <ReactAriaRouterProvider>
          <AuthenticationProvider navigate={(options) => navigate(options)}>
+           <PageTracker />
            <Outlet />
          </AuthenticationProvider>
        </ReactAriaRouterProvider>
      </ThemeModeProvider>
    </QueryClientProvider>
  );
}

Checklist

  • I have added tests, or done manual regression tests
  • I have updated the documentation, if necessary

@tjementum tjementum self-assigned this Jul 29, 2025
@tjementum tjementum added the Bug Something isn't working label Jul 29, 2025
@tjementum tjementum moved this to 🏗 In Progress in Kanban board Jul 29, 2025
@sonarqubecloud
Copy link

@tjementum tjementum merged commit 2ff6d50 into main Jul 29, 2025
27 checks passed
@tjementum tjementum deleted the error-tracking-application-insights branch July 29, 2025 20:07
@github-project-automation github-project-automation bot moved this from 🏗 In Progress to ✅ Done in Kanban board Jul 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Something isn't working

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants