This tutorial shows you how to set up Google Analytics measurement for a single-page application (SPA) using Google Tag Manager. You will learn how to configure your Google Analytics property, enable the necessary GTM variables, and create tags that trigger on history changes.
Before you begin
This tutorial assumes that you have:
- A Google Analytics account and property.
- A Google Tag Manager container installed on your SPA.
- The Editor (or above) role for both Google Analytics and Google Tag Manager.
Step 1: Prepare the GA4 data stream
To manually track page_view events in a SPA, you must first disable automatic
history-based page views in GA4 to avoid double-counting.
- Open Google Analytics.
- In Admin, under Data collection and modification, click Data streams.
- Select your web data stream.
- Under Enhanced measurement, click the settings icon
.
- Under Page views > Show advanced settings, clear Page changes based on browser history events.
- Click Save.
Step 2: Enable built-in History variables in Tag Manager
To use history-based triggers or variables in GTM, you must first enable the built-in History variables.
- Open Google Tag Manager.
- In your workspace, navigate to Variables.
- Under Built-In Variables, click Configure.
- In the History section, enable the following variables:
History New URL FragmentHistory Old URL FragmentHistory New StateHistory Old StateHistory Source
- Close the configuration panel.
Step 3: Create the History Change trigger
Next, create a trigger that fires whenever a history change event (like
pushState or replaceState) occurs in your SPA.
- In your Tag Manager workspace, navigate to the Triggers menu.
- Click New.
- In Trigger Configuration, choose the History Change trigger.
- (Optional) If you only want to track certain screen changes, update the trigger to fire on Some History Changes.
- Name the trigger (e.g., "History Change") and click Save.
Step 4: Create the Google tag
Now, create a Google tag that updates the page_location parameter whenever a
history change occurs.
- In the Tags menu, click New.
- In Tag Configuration, choose the Google tag.
- Enter your Tag ID (the same ID used for your initial Google tag).
Under Configuration settings, add the following parameters:
page_location:{{Page URL}}page_title:{{Page Title}}update:true
Name the tag (for example, "Google Tag - SPA Update") and click Save.
Step 5: Send the virtual page_view event
Finally, create a GA4 Event tag to send the actual page_view event for each
virtual page load.
- Click New in the Tags menu.
- In Tag Configuration, choose Google Analytics: GA4 Event.
- Enter your Measurement ID.
- Set the Event Name to
page_view. - Under Advanced Settings > Tag Sequencing, select Fire a tag before GA4 Event - Virtual Page View fires and select the Google Tag - SPA Update tag that you created in Step 4.
- Under Triggering, select the History Change trigger you created in Step 3.
- Name the tag (for example, "GA4 Event - Virtual Page View") and click Save.
Step 6: Verify your setup
To ensure your virtual page views are being recorded correctly:
- In Tag Manager, click Preview to enter the Preview mode.
- Navigate through your SPA.
- In Google Analytics
DebugView, verify that a new
page_viewevent appears for each screen change. - Check that the
page_locationvalues match your expectations.