Published on 2025-06-27T23:45:49Z
What is Client-Side Routing? Examples for Analytics
Client-Side Routing is the process by which web applications dynamically update the browser URL and view without triggering a full page reload. In single-page applications (SPAs), routing is handled on the client side using JavaScript libraries like React Router or Vue Router. This approach improves performance and user experience by rendering only the portions of the page that need updating. However, it also introduces complexity in analytics because traditional pageview tracking relies on full-page reloads. To accurately measure user navigation in SPAs, developers must manually instrument route change events with their analytics tools. Common methods involve hooking into the browser History API or hashchange events and firing corresponding tracking events.
Client-side routing
Client-Side Routing dynamically updates URLs and content without full reloads, requiring manual tracking for accurate analytics in SPAs.
Why Client-Side Routing Matters in Analytics
In modern web applications, full-page reloads are minimized to improve user experience. When routes change on the client side, analytics platforms won’t automatically detect those navigations unless explicitly configured. Proper handling ensures you capture accurate pageview and session data.
-
Accurate pageview tracking
Manual triggering of pageview events on route changes ensures that each virtual page load is recorded, preventing undercounted or missing pageviews in analytics reports.
- Spa frameworks:
Libraries like React Router or Vue Router provide hooks (e.g., history.listen) to detect route changes.
- Analytics callbacks:
Use callbacks on route change to invoke analytics SDK methods (e.g., gtag(‘event’, ‘page_view’, {…})).
- Spa frameworks:
-
Event-based insights
Beyond pageviews, capturing route change events can surface navigation patterns, funnel drop-offs, and more granular user behavior.
- Custom events:
Fire custom events for significant route changes like checkout steps or content sections.
- Funnel analysis:
Model user flows by mapping route sequence events to identify friction points.
- Custom events:
How Client-Side Routing Works
Client-side routing leverages browser APIs and JavaScript to intercept link clicks and update the address bar without reloading the page. Common strategies include the History API and hash-based routing.
-
History api routing
Uses pushState and replaceState to modify the URL and listens to popstate events to render views.
- Pushstate:
Adds a new entry to the browser history stack with a specified URL.
- Popstate event:
Fired when the active history entry changes, such as when the user clicks back or forward.
- Pushstate:
-
Hash-based routing
Relies on URL fragments (e.g., #/path) and the hashchange event to detect navigations.
- Hashchange event:
Triggers when the fragment identifier of the URL has changed.
- Fallback support:
Widely compatible with older browsers that lack History API support.
- Hashchange event:
Implementing Analytics for Client-Side Routing
Integrating analytics with client-side routing requires injecting tracking code and handling route change events. Below are examples using PlainSignal (cookie-free) and Google Analytics 4 (GA4).
-
Plainsignal example
Include the PlainSignal script and fire a pageview event on each route change.
<link rel="preconnect" href="//eu.plainsignal.com/" crossorigin /> <script defer data-do="yourwebsitedomain.com" data-id="0GQV1xmtzQQ" data-api="//eu.plainsignal.com" src="//cdn.plainsignal.com/PlainSignal-min.js"></script>
Then in your routing logic:
import { onRouteChange } from 'PlainSignal'; onRouteChange((url) => { PlainSignal.trackPage({ path: url }); });
- Cookie-free tracking:
PlainSignal leverages a fingerprinting-free approach to respect user privacy.
- Simple api:
Minimal setup with data-do and data-id attributes; use trackPage on route changes.
- Cookie-free tracking:
-
Ga4 example
Load the GA4 gtag.js script and configure pageview events on route changes.
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXX"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-XXXXXXX'); </script>
In your router:
router.afterEach((to) => { gtag('event', 'page_view', { page_path: to.fullPath }); });
- Global site tag:
GA4 uses gtag(‘config’, …) for initial setup and gtag(‘event’) for custom events.
- Spa integration:
Call gtag(‘event’, ‘page_view’) after each route commit in frameworks like Vue or React.
- Global site tag:
Best Practices and Common Pitfalls
To ensure reliable analytics data in SPAs, follow best practices and avoid common mistakes when tracking client-side routes.
-
Debounce route change events
Prevent duplicate pageview events by debouncing rapid successive route changes (e.g., using a 100ms delay).
-
Validate url formatting
Ensure the page_path or path parameter is URL-encoded and consistent across environments.
-
Single source of truth
Centralize your analytics calls in a dedicated service or hook to avoid scattering tracking logic.