Field notes Field note 12

GA4 on a Single-Page App: Tracking Every Route Change Without Double Counting

GA4 single page app tracking for React, Lovable and Bolt sites: one page_view per route change, correct titles and referrers, no double counting, via GTM.

· 9 min read

GA4 single page app tracking works when exactly one page_view reaches GA4 for every route change, with the new URL and the new title. You get there in one of two ways: GA4's built-in history tracking, or a manual page_view sent from Google Tag Manager (GTM) or gtag.js. Pick one, never both.

It matters because a single-page app (SPA) loads one HTML page, then the router changes the URL with the browser's History API (pushState) and redraws the content without a new page load. GA4's default tag only sends a page_view on that first load. React apps built with Lovable or Bolt work this way, and so does Next.js navigation with <Link>, which updates the page without a full reload. Without the right setup, GA4 sees one page per session and you can't tell which page sent the lead. Google's own GTM guide for single-page apps is the reference for what follows.

Checked on 2026-11-04 against: Google's GA4 and GTM help for single page apps, GTM tag sequencing, gtag.js page views, and the React Router, Next.js and TanStack Router docs. Not run on a live property.

Symptoms and likely causes

SymptomLikely causeHow to test
One page per session, almost nothing past the homepageNo page_view on route changes. History tracking is off, or the router does not trigger history events GA4 can seeClick through three routes with DebugView open. Count page_view events
Two page_view events per clickEnhanced measurement history tracking is on and a GTM or gtag page_view also fires. Or one click fires several history eventsIn Tag Assistant, check which tags send page_view on one route change
Right URL, previous page's titleThe page view fires on the history change, before the app updates document.titleIn DebugView, open page_view and compare page_title with the page you are on
Many (not set) landing pagesSessions that start without a page_view, for example after a timeout on a tab that stays openIn Explore, filter landing page (not set) and look at the first events of those sessions

Option 1: enhanced measurement history events

The zero code option. GA4's enhanced measurement sends page_view on page load and, through an advanced setting, on browser history changes. Google says it listens for pushState, popState and replaceState.

To check it, go to Admin, Data collection and modification, Data streams and open your web stream. Under Enhanced measurement, click the settings icon, then under Page views open Show advanced settings and look at Page changes based on browser history events (steps from Google's SPA guide).

It is enough when the router uses pushState for every route change and the title is already updated when the history event fires. It causes trouble when:

  • **You also send page_view from GTM or gtag.** Every route change counts twice. Google's page view guide states that with enhanced measurement on, history based page views still fire even if you set send_page_view to false. Google's SPA guide also says not to enable automatic history tracking if you configure your tags in GTM.
  • The title updates late. Many React apps set the title after the route renders, so GA4 records the previous title.
  • Your router fires more than one history event per click, for example a pushState followed by a replaceState to tidy the URL.

If any of these apply, switch the setting off and use Option 2.

Option 2: send page_view manually from GTM

Here GTM decides when a page view happens, with one of two triggers.

Approach A: History Change trigger (Google's GTM guide)

No code changes in the app.

  1. Turn off the browser history events setting in GA4, as above, to avoid double counting.
  2. Trigger. In GTM, create a trigger of type History Change. Google's guide makes filtering optional. To keep URL tidying out, enable the built-in History Source variable (Variables, Built-In Variables, Configure) and use Some History Changes with the condition History Source does not equal replaceState. Google does not list the possible values, so confirm the exact value your router produces in the Variables tab of GTM preview first.
  3. Update tag. Create a Google tag with your measurement ID. Under Configuration settings, add these parameters: page_location = {{Page URL}}, page_title = {{Page Title}}, update = true. Per Google, update merges these values into the existing tag configuration without sending a duplicate page_view. Do not add page_referrer here. Leave Triggering empty. It runs through tag sequencing.
  4. Page view tag. Create a Google Analytics: GA4 Event tag with event name page_view. Under Advanced Settings, Tag Sequencing, tick "Fire a setup tag before [tag name] fires" (GTM's tag sequencing help) and select the update tag. Trigger it on your History Change trigger.

Your normal Google tag still sends the first page_view. The weak spot is timing: {{Page Title}} is read when the URL changes, so if the app sets the title a beat later, you get the right URL with the old title.

Approach B: a dataLayer event from the router

The app tells GTM that a page is ready, after the title is set. It needs a small code change and is more reliable when titles matter.

If your app uses React Router (check package.json), push the event after the new title is in place:

import { useEffect } from "react";
import { useLocation } from "react-router"; // "react-router-dom" in v6 and earlier

export function RouteTracker() {
  const location = useLocation();

  useEffect(() => {
    // Runs after the route has rendered and the page set its title
    window.dataLayer = window.dataLayer || [];
    window.dataLayer.push({
      event: "virtual_page_view",
      page_location: window.location.href,
      page_title: document.title
    });
  }, [location.pathname, location.search]);

  return null;
}

Render <RouteTracker /> once inside the router. If your pages set their title with a helmet library or a later effect, check in DebugView that the title is current. If it isn't, push the event from the place where the title is set. In development, React StrictMode runs effects twice on mount, so you may see two pushes on the first page. Judge the count on a production build.

Some newer Lovable projects use TanStack Start, which routes with TanStack Router. Look for @tanstack/react-router in package.json; the Lovable SEO guide covers the stack. There, subscribe to the router's resolved navigation and push the same event. TanStack's router events guide names onResolved as the default for analytics, because it fires after navigation has fully resolved:

// where you create the router
router.subscribe("onResolved", () => {
  window.dataLayer = window.dataLayer || [];
  window.dataLayer.push({
    event: "virtual_page_view",
    page_location: window.location.href,
    page_title: document.title
  });
});

If DebugView shows the previous title, switch to onRendered, which TanStack recommends for work that needs the new route in the DOM. TanStack's guide also notes that fromLocation can be undefined on the initial load, so guard for that if you read it, and check in DebugView whether the listener runs for the first page in your setup. That decides the send_page_view setting below.

In GTM:

  1. Create Data Layer Variables for page_location and page_title.
  2. Create a Custom Event trigger with event name virtual_page_view.
  3. Point the same update tag pattern (Google tag with update = true) and GA4 Event page_view tag at this trigger instead of History Change, using the Data Layer Variables for page_location and page_title.
  4. Turn off enhanced measurement history events in GA4.

Pick one approach. If you run a History Change trigger and a router event at the same time, every route change counts twice.

Set send_page_view to false when the router also covers the first load

The React effect above also runs on the first render, so your Google tag and the router event would both send the first page. In that case, add the parameter send_page_view with the value false to the Google tag that fires on page load in GTM. With gtag.js, it goes in the config call, as shown in Google's page view guide:

gtag("config", "G-XXXXXXX", { send_page_view: false });

// on each route change, after the title is set
gtag("event", "page_view", {
  page_location: window.location.href,
  page_title: document.title
});

If you use Approach A, leave send_page_view alone. History Change doesn't fire on the first load, so the Google tag's own page view is the one you want. Some routers call replaceState on load, which fires a History Change straight away. The History Source filter from step 2 keeps that out.

page_location and page_referrer

page_location must be the full URL, with protocol, not just the path (page view guide). window.location.href and {{Page URL}} both qualify. Keep the query string, since UTM parameters on the first page are how GA4 attributes the session, but strip any personal data (emails or names in query strings) first, because GA4 doesn't allow it.

page_referrer is easy to get wrong: document.referrer and GTM's {{Referrer}} variable do not change on SPA route changes, so they keep the external source. Google's GTM SPA guide leaves page_referrer out of the update tag on purpose, so that GA4 fills in the previous virtual page itself. Don't pass document.referrer or {{Referrer}} as page_referrer. Then check in DebugView, as Google's SPA guide asks, that each page_view has an updated page_referrer and page_location compared with the one before. If yours keeps showing the external referrer, store the previous URL in a custom variable or your router code and pass it as page_referrer with each event.

Why landing pages show (not set)

Google says (not set) can appear as the landing page when a session has no page_view event (What (not set) means). On SPAs, two setups are the likely suspects:

  • No page view on the first screen. send_page_view is false but the router event never fires on the first load, or it fires before GTM has loaded.
  • The session times out on an open tab. A visitor returns after the session timeout (30 minutes by default, per Configure your Google tag settings) and clicks a button. That can start a new session with no page_view until the next route change.

Fix the first by checking that the first page sends exactly one page_view. For the second, you can lengthen the session timeout in the data stream under Configure tag settings, Show all, Adjust session timeout (same page). A small share of (not set) is normal.

How to check it in Tag Assistant and DebugView

  1. Open Tag Assistant and connect to your site with GTM preview.
  2. Load the first page. Confirm one page_view from one tag.
  3. Click through three routes. For Approach A, look for History Change events in the left column. For Approach B, look for virtual_page_view. Each should fire the GA4 page_view tag once.
  4. In GA4 Admin, DebugView, for each route change, open page_view and check page_location, page_title and page_referrer.
  5. Use the back button. It fires a history event too, and should produce one page view, not two.

What you should see: one page_view per route change, each with the new page_location, the new page_title, and a page_referrer that differs from the previous event. Two page_view events for one click means two methods are running. A right URL with the old title means the title updates after the event fires.

When to get help

If your app is live, takes paid traffic and the reports still look wrong after these checks, the problem is usually in how the router, the title logic and the container interact. Lead events need the same care: see Calendly conversion tracking and HubSpot meetings conversion tracking for booking widgets. Our conversion tracking services cover SPA page views and revenue events, and AI built websites done properly covers routing and indexing. A Tracking Audit lists what is broken and what to fix first. To see what your site loads today, Scan your site free.

Checklist

  • One method only: enhanced measurement history events, or a manual page_view
  • If manual, history events switched off in the GA4 data stream
  • One trigger only: History Change or a router dataLayer event
  • send_page_view set to false if the router also sends the first page
  • Full URL in page_location, title current at send time
  • page_referrer equals the previous page in DebugView
  • First page and back button each send one page_view
  • (not set) landing pages checked against session starts

Before you go

Is your tracking actually working?

Paste your URL. We check whether GTM, GA4, Meta Pixel and Consent Mode are on the page, and whether Google can read it without JavaScript. The results come by email.

Free. We only read what any visitor's browser can see.