Sub topic 3
Page Views and Browser History Changes in Single-Page Sites
Prerequisites and the basic model
This chapter assumes that you have a GA4 property, a web data stream, and the Google tag installed through gtag.js or Google Tag Manager. You should also understand that GA4 records interactions as events with parameters.
A page view is a GA4 event named pageview. On a traditional website, the browser loads a new document for each page. The Google tag sees that load and sends one pageview event.
A single-page application (SPA) usually loads one document and replaces the visible content with JavaScript. Because the browser does not perform a full page load, GA4 needs another signal to recognize that the user viewed a new route.
How enhanced measurement handles page changes
The Page views setting in enhanced measurement collects page_view events. Its related option, Page changes based on browser history events, watches for route changes made through browser history APIs.
Common history operations include:
- pushState, which adds a new URL to the browser history.
- replaceState, which changes the current history entry.
- popstate, which occurs when the user moves backward or forward.
When GA4 detects a qualifying history change, it can send another page_view event. The event generally includes information such as the new page URL, page title, and referrer.
The event is still named pageview. GA4 does not normally create an event named historychange for this feature.
| Site behavior | Expected page_view behavior |
|---|---|
| A full page load occurs | One page_view is sent for the loaded document |
| An SPA route changes through browser history | A page_view can be sent when history-based page changes are enabled |
| Content changes without a URL or history change | Enhanced measurement may not detect a new page view |
The event flow
Configure the data stream
In Google Analytics:
- Open Admin.
- Select the web data stream.
- Open Enhanced measurement.
- Select the settings control.
- Turn on Page views.
- Confirm that Page changes based on browser history events is enabled.
- Save the settings.
The setting applies to the web data stream. It does not rewrite your application’s router or create a route change when your application only swaps content internally.
A common SPA configuration is to leave automatic page views enabled and let GA4 detect standard history changes. Test this behavior before adding manual tracking.
When automatic detection is not enough
Some routers use custom navigation logic. They might change application state without changing the URL, or they might update the URL in a way that GA4 does not detect. In that case, you can send a page_view explicitly from the router.
Use one method for each route transition. Do not send a manual page_view for a transition that enhanced measurement already tracks.
The following fallback disables the initial automatic page view and sends page views from application code:
window.dataLayer = window.dataLayer || [];
function gtag() {
window.dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', 'G-XXXXXXXXXX', {
send_page_view: false
});
function trackVirtualPageview(url, title) {
const fullUrl = new URL(url, window.location.origin).href;
gtag('event', 'page_view', {
page_location: fullUrl,
page_title: title || document.title
});
}
// Call this after your router completes a navigation.
trackVirtualPageview('/pricing', 'Pricing');This approach requires your router to call trackVirtualPageview after every successful navigation. It also requires careful testing of the initial route, back and forward buttons, redirects, and failed navigations.
Common errors
Duplicate page views
Duplicate data often comes from enabling history-based enhanced measurement and also sending a manual page_view for the same route. It can also happen when both Google Tag Manager and gtag.js contain page-view tags.
Missing page views
A route can be invisible to enhanced measurement when the URL does not change or when the router uses unsupported navigation behavior. Confirm whether the route creates a browser history event.
Incorrect page titles and URLs
The page title and page location must be updated before the page_view is sent. If the event fires first, reports can contain the previous route’s title or URL.
Debugging
Use Tag Assistant preview or GA4 DebugView while navigating through several routes. Check that each intended route creates exactly one pageview and that pagelocation matches the visible route.
Exercise
An SPA changes from /home to /docs with history.pushState. Enhanced measurement is enabled, but the route does not appear in DebugView. What should you check first?
Show answer
Check that Page views and Page changes based on browser history events are enabled for the correct web data stream. Then confirm that the router actually calls a supported history operation and that the Google tag is present on the page.