<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Lomen Analytics]]></title><description><![CDATA[Lomen Analytics]]></description><link>https://lomenanalytics.hashnode.dev</link><image><url>https://cdn.hashnode.com/uploads/logos/6af4d6ad602d4b6bb2151195/3c076d65-1b1d-40ca-9f1b-13c93521ac41.png</url><title>Lomen Analytics</title><link>https://lomenanalytics.hashnode.dev</link></image><generator>RSS for Node</generator><lastBuildDate>Sat, 21 Nov 2026 18:04:54 GMT</lastBuildDate><atom:link href="https://lomenanalytics.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Why your analytics may miss pageviews in a single-page app, and how to test it in five minutes]]></title><description><![CDATA[A Network-tab test that shows whether your analytics counts route changes in a single-page app, plus a small hook to report them.]]></description><link>https://lomenanalytics.hashnode.dev/why-your-analytics-may-miss-pageviews-in-a-single-page-app-and-how-to-test-it-in-five-minutes</link><guid isPermaLink="true">https://lomenanalytics.hashnode.dev/why-your-analytics-may-miss-pageviews-in-a-single-page-app-and-how-to-test-it-in-five-minutes</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[React]]></category><category><![CDATA[analytics]]></category><category><![CDATA[spa]]></category><dc:creator><![CDATA[Lomen analytics]]></dc:creator><pubDate>Wed, 18 Nov 2026 20:04:11 GMT</pubDate><content:encoded><![CDATA[<p>Disclosure up front: we make Lomen, website analytics with a cookie-free tracking script. This post is not about Lomen. The test below works the same way for any analytics script, and you can run it on whatever you use today.</p>
<h2>The problem</h2>
<p>A classic analytics script sends one pageview when the page loads. In a multi-page site, every click on a link loads a new document, so every page gets counted.</p>
<p>A single-page app (React Router, Vue Router, SvelteKit, Next.js client navigation and so on) works differently. The first page loads once. After that, the router swaps content and updates the URL with the History API (<code>history.pushState</code> or <code>history.replaceState</code>). No new document loads.</p>
<p>If your analytics script only listens for page loads, every navigation after the first one is invisible to it. A visitor who opens five pages looks like a visitor who opened one.</p>
<p>The symptoms in your dashboard:</p>
<ul>
<li>The landing page gets almost all pageviews, deeper pages get very few.</li>
<li>Pages per visit look suspiciously close to one.</li>
<li>Exit pages are almost always the same as entry pages.</li>
</ul>
<p>No analytics tool can count a pageview the script never reports. So the first thing to check is the browser, not the dashboard.</p>
<h2>The five-minute test</h2>
<ol>
<li>Open your production site (not localhost; see the dev-mode note below).</li>
<li>Open DevTools, go to the Network tab and tick "Preserve log".</li>
<li>Find the request your analytics script sends. Filter by the script's domain or by a word from its endpoint path. Reload once to see what a pageview request looks like.</li>
<li>Click an internal link that the router handles. Do not reload.</li>
<li>Watch the Network tab.</li>
</ol>
<p>What you want to see: exactly one new request per navigation, with the new path in it.</p>
<p>Then test the edge cases:</p>
<ul>
<li><strong>Back and forward buttons.</strong> These fire a <code>popstate</code> event, not <code>pushState</code>. Some setups catch one but not the other.</li>
<li><strong>Query string changes.</strong> Filters, search boxes and pagination often call <code>replaceState</code> with a new <code>?page=2</code>. Decide whether you want those counted as pageviews. Usually you do not.</li>
<li><strong>Hash links.</strong> <code>#section</code> anchors fire <code>hashchange</code>. If your app uses hash routing (<code>/#/settings</code>), those are real pages. If they are just in-page anchors, they are not.</li>
<li><strong>Redirects inside the app.</strong> A route that immediately redirects (for example <code>/</code> to <code>/dashboard</code>) can produce two pageviews for one visit.</li>
</ul>
<h2>Three outcomes and what to do</h2>
<p><strong>One request per navigation.</strong> Your script already handles client-side routing. Nothing to do.</p>
<p><strong>No request after navigation.</strong> The script only tracks page loads. Check its docs for an SPA or history mode option, or for a manual function to call on route change. If neither exists, hook route changes yourself (below).</p>
<p><strong>Two requests per navigation.</strong> You are double counting. This usually happens when the script tracks history changes automatically and you also added a manual call in your router. Remove one of them.</p>
<h2>A minimal route-change hook</h2>
<p>If you need to report navigations yourself, the pattern is the same in every framework: listen for URL changes, skip duplicates, then call your tool's pageview function.</p>
<pre><code class="language-js">function onRouteChange(callback) {
  let lastPath = location.pathname;

  const notify = () =&gt; {
    // Ignore query-string and hash-only changes. Adjust if you want those.
    if (location.pathname === lastPath) return;
    lastPath = location.pathname;
    callback(location.pathname);
  };

  for (const method of ['pushState', 'replaceState']) {
    const original = history[method];
    history[method] = function (...args) {
      const result = original.apply(this, args);
      notify();
      return result;
    };
  }

  window.addEventListener('popstate', notify);
}

onRouteChange((path) =&gt; {
  // Replace with your analytics tool's documented pageview call.
  console.log('pageview', path);
});
</code></pre>
<p>If your router gives you a way to react to navigation (React Router's <code>useLocation</code>, Vue Router's <code>router.afterEach</code>, <code>usePathname</code> in the Next.js App Router), use that instead of patching <code>history</code>. It is cleaner and runs after the route has resolved.</p>
<h2>Two details that trip people up</h2>
<p><strong>Report after the new page renders.</strong> If you send the pageview the instant the URL changes, <code>document.title</code> may still hold the old page's title. Router hooks that run after navigation avoid this.</p>
<p><strong>Dev mode can lie.</strong> React's StrictMode runs effects twice in development, so a pageview call inside <code>useEffect</code> can fire twice locally and once in production. Always run the Network-tab test on the production build before deciding something is broken.</p>
<h2>Checklist</h2>
<ul>
<li>Run the Network-tab test on production.</li>
<li>One request per route change, including back and forward.</li>
<li>Decide on purpose whether query-string changes count.</li>
<li>No duplicate requests from an automatic and a manual call at the same time.</li>
<li>Redirect routes do not produce an extra pageview.</li>
</ul>
<p>If your deep pages suddenly show traffic after the fix, that traffic was always there. Your script just was not reporting it.</p>
<hr />
<p><em>Drafted with AI assistance and reviewed before publishing.</em></p>
]]></content:encoded></item></channel></rss>