Clarity session recording fails to start on SPA navigation
0 reputation · 11 May 2026, 00:12 UTC
0 reputation · 11 May 2026, 00:12 UTC
When integrating Microsoft Clarity into a single‑page application, the goal is to capture a continuous user session across route changes without interruption. The Clarity JavaScript snippet is inserted once in the document head, and the project tracking ID is the only required identifier. However, the documentation does not explicitly state how Clarity detects navigation events in an SPA; this omission creates uncertainty about whether a new session is automatically started or whether the existing session continues.
The current behavior observed in some projects suggests that the session may reset on each route transition, implying that Clarity might rely on a full page reload or a specific navigation event that is not triggered by client‑side routing. If this is indeed the case, the performance impact and data fragmentation could be significant for analytics.
To clarify the integration, the following questions need to be answered:
35275 reputation · 11 May 2026, 05:58 UTC
Clarity starts a session when its snippet is first executed on a page load. In a SPA the snippet runs only once – the initial load of index.html (or the root shell). Subsequent client‑side route changes do not trigger a new session unless the snippet is re‑executed or the page is fully reloaded.
No, Clarity’s public API only exposes clarity('set', 'key', 'value') for custom tags and clarity('set', 'userId', id) for user identification. There is no documented method to force a session restart or to signal a virtual page view that would keep the same session alive across routes.
public/index.html in React, app.vue root in Vue, or the shell in Angular). Ensure the router never re‑injects the script.history.pushState or the framework’s router to change the URL. Clarity will automatically record a virtual page view via its internal listener on popstate and pushState events, but the session remains the same.clarity('set', 'page', window.location.pathname) or a similar tag. This keeps the session continuous while still providing per‑page attribution.https://www.clarity.ms/.... The sessionId in the payload should stay constant across route changes.<script>...clarity('set', ...)</script> after navigation. If the snippet appears again, remove it from routed components.clarity.js file in the network panel. Confirm it is loaded only once.sessionId indicates a new session has started.To refine the recommendation, please confirm whether your SPA framework re‑mounts the root component (and thus re‑runs the snippet) on each navigation. If that is the case, the solution would involve moving the snippet out of the component tree.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.