Analytics snippets
How to Track Virtual Page Views in GA4 During an A/B Test
★★★ Technical level 3 of 3
Written by Neil Webley · Last updated
Track GA4 virtual page views in single-page applications without duplicates, then attribute meaningful page states to A/B test variants.
Quick answer
Track a virtual page view when a single-page application presents a genuinely new screen without reloading the document. Send one GA4 page_view with the new page location and title, preserve the previous virtual URL as referrer, and avoid combining automatic history tracking with a second manual implementation.
What is a virtual page view?
A traditional page view occurs when the browser loads a new HTML document. A single-page application can replace the visible screen and update the URL with pushState, replaceState or a hash while keeping the original document. Analytics needs an additional signal if that state should appear as another page in journeys, landing analysis and engagement reporting.
Not every interaction is a page. Tabs, accordions, filters and modal windows are usually events unless they represent a distinct navigable screen that a visitor would recognise as a new step.
How to send virtual page views to GA4
Choose one route:
- Enhanced measurement history changes: suitable when the SPA consistently updates browser history.
- Google Tag Manager history trigger: useful when GTM owns analytics.
-
Manual
gtag.jsevent: useful when the application knows exactly when a screen becomes ready.
gtag('event', 'page_view', {
page_title: 'Checkout – Delivery',
page_location: location.href,
page_referrer: previousVirtualUrl
});
Update previousVirtualUrl only after sending. Page location should be a valid URL representing the state; do not include personal information in paths, fragments or query strings.
Avoid duplicate page views
If GA4 Enhanced Measurement already records history changes, a manual history listener can double every SPA page view. Likewise, do not configure the same trigger in GTM and application code. Inspect DebugView before adding anything.
Track an SPA page view with Google Tag Manager
- Enable GTM's built-in History Source, New History Fragment, Old History Fragment and URL variables.
- Create a History Change trigger for the routes that represent virtual pages.
- Update the Google tag configuration for the new location without automatically sending an extra page view.
-
Fire a GA4 Event tag named
page_view. - Verify the new location and previous virtual referrer in Preview and GA4 DebugView.
When using this manual GTM approach, disable overlapping automatic history page views in the GA4 stream.
Virtual page views during an A/B test
First decide whether the experiment creates a real virtual page or merely changes content on the existing page. If variant B turns a multi-page form into an inline stepper, tracking each meaningful step may be necessary for a fair funnel comparison. Use the same semantic step names across variants even when their URLs differ.
Keep global site navigation measurement separate from experiment-specific conversion events. The standard GA4 page_view maintains site journeys; an experiment event indicates that an assigned visitor reached the particular state being compared.
Use firePageVisit in FreeCROTool
cro_tests.firePageVisit({
name: 'checkout_delivery_step'
});
This sends a FreeCROTool-attributed freecro_page event, not the site's standard GA4 page_view. It is useful as an experiment conversion or diagnostic row because FreeCROTool adds the test and variant assignment. Use it alongside, not as an accidental replacement for, the site's SPA page-view implementation.
FreeCROTool reevaluates tests after pushState, replaceState, popstate and hashchange. Guard your own conversion so the same state is not sent repeatedly:
var state = location.pathname + location.search + location.hash;
if (window.__fcroLastCheckoutState !== state) {
window.__fcroLastCheckoutState = state;
cro_tests.firePageVisit({
name: 'checkout_delivery_step'
});
}
See the event-helper reference and custom event guide.
Virtual page-view testing checklist
- Initial load sends one page view.
- Each meaningful virtual screen sends exactly one additional page view.
- Tabs and cosmetic interactions do not inflate page counts.
- Back and forward navigation sends the correct state.
-
page_location, title and referrer reflect the virtual journey. - No URL contains personal information.
- Control and variants use comparable step definitions.
- Consent behaviour matches the rest of analytics.
Use the official GA4 single-page application measurement guide for the latest platform configuration details.