Single page apps
On a "normal" site, every click loads a new page, so counting each page load counts every page view. Single page apps don't work like that. Click a link in a React, Svelte or Vue app and the page changes, but nothing reloads.
Using the script tag
Good news: there's nothing to do. Add the script tag once, in your app's HTML, and it notices whenever your router changes page.
A few things it does for you:
- Only path changes count. Changing a query string or a
#hash(like opening a tab or filtering a list) isn't really a new page, so it's not counted. - The referrer and campaign tags are only sent once, with the page the visitor landed on. More on why below.
- Adding it twice doesn't count twice. If your framework puts the tag on the page again, the second copy does nothing.
Doing it yourself
If you can't use the script tag, or would rather keep the beacon in your own code, you can send it from your router instead. Don't use the script tag as well, or every page gets counted twice.
A beacon function
First, pull the beacon out into a function you can call from anywhere:
const TOKEN = "YOUR_TOKEN";// The referrer and campaign tags only mean something on the page the visitor// landed on. Later page views are clicks within your own site.let isLandingView = true;export const trackPageView = (): void => { const query = new URLSearchParams(isLandingView ? location.search : ""); const referrer = isLandingView ? document.referrer : ""; isLandingView = false; fetch("https://api.dev-team-tools.com/analytics/beacons", { method: "PUT", headers: { Authorization: `Bearer ${TOKEN}`, "Content-Type": "application/json", }, body: JSON.stringify({ path: location.pathname, referrer, utmSource: query.get("utm_source") || query.get("ref"), utmMedium: query.get("utm_medium"), utmCampaign: query.get("utm_campaign"), }), keepalive: true, }).catch(() => { // A failed beacon should never affect the page. });};
Why only send the referrer once?
This one has caught us out before. In a single page app, document.referrer doesn't change when you move between pages. It stays as wherever the visitor originally came from.
So if someone arrives from a newsletter and reads five pages, sending the referrer every time would credit the newsletter with five visits. Which is lovely for the newsletter's ego, but not very accurate. Sending it with the first page view only means each arrival is counted once.
The same goes for campaign tags. They're in the landing page's address, and most routers keep them there until the first click.
Calling it from your router
Then call trackPageView whenever the page changes. Here's how in a few popular frameworks.
SvelteKit
afterNavigate runs on the first load and after every navigation, so it covers both. It also runs when only the query string changes, so check the path actually moved:
<script lang="ts"> import { afterNavigate } from "$app/navigation"; import { trackPageView } from "$lib/trackPageView"; afterNavigate(({ from, to }) => { if (from?.url.pathname === to?.url.pathname) { return; } trackPageView(); });</script>
React Router
import { useEffect } from "react";import { useLocation } from "react-router";import { trackPageView } from "./trackPageView";export const PageViews = () => { const location = useLocation(); useEffect(() => { trackPageView(); }, [location.pathname]); return null;};
Pop <PageViews /> inside your router, once.
Next.js (App Router)
"use client";import { useEffect } from "react";import { usePathname } from "next/navigation";import { trackPageView } from "./trackPageView";export const PageViews = () => { const pathname = usePathname(); useEffect(() => { trackPageView(); }, [pathname]); return null;};
Add <PageViews /> to your root layout.
Vue Router
import { trackPageView } from "./trackPageView";router.afterEach((to, from) => { // from has no matched routes on the very first navigation. if (from.matched.length > 0 && to.path === from.path) { return; } trackPageView();});
Things to watch out for
- Don't use the script tag or the inline snippet as well. Otherwise every page gets counted twice.
- React's Strict Mode runs effects twice in development. That's only in development, and your local traffic isn't counted anyway (see the domain check), so don't panic if you see double beacons in your network tab.
- Only track path changes. Changing a query string or a
#hash(like opening a tab or filtering a list) isn't really a new page. Each example above only fires when the path changes, and the path is all that's recorded anyway.