How to track single page apps in Google Analytics via GTM

Last Updated: August 20, 2022

What are single page applications (SPAs)?

SPAs are single page applications which look like a traditional website, but they work differently. They are basically web applications, or websites, which load all the resources on the first load of the page. 

As the user interacts with the website, the content is loaded dynamically. In general, for normal websites, when the user interacts with the website and navigates to the page, the page refresh happens and resources are loaded. However, this is not the case with a single page application. There would be no refresh when the users interact, the content is loaded dynamically on the page.

Since SPAs load all the HTML, CSS, and JavaScript resources on the initial load, this makes them fast and responsive. To achieve the desired results, SPAs use modern technology frameworks such as Angular and React.

Why is it hard to track single page applications?

In the case of traditional websites, Google Analytics works fine because the code snippet is loaded every time the page is loaded. But in the case of a single page application, content is loaded dynamically which means the page refresh happens only once and Google Analytics runs only once on the page. So, the Google Analytics tag fires only on the initial load of the page.

Because Google Analytics relies on browser events for tracking, in the case of single page applications the GA tag fires only once on the initial load. This is basically only one web page that you visit and reloads all the other content using JavaScript.

How does the history change trigger in GTM help in tracking SPAs?

If you open the GTM preview console and check the tag for any SPA page, you will notice only one page view. However, we have a solution for this in GTM. You can track SPA’s using the history change trigger in GTM. 

If the developer of the SPA page has implemented the browser history functionality, then you link this to GTM history, change the trigger and track the activity of the users on a single page application. 

You will also have to enable the inbuilt variables to use this trigger in GTM, which are called as history listeners.

How to implement the history change trigger in GTM

Let’s see how we can use GTM to track single page applications. Follow the below steps to track SPAs in GTM.

Step-1: Log in to your Google Tag Manager account.

Step-2: Select the container name in which you would like to implement tracking for SPA.

Container name

Step-3: Click on ‘Triggers’ from the left-hand side.

triggers 2

Step-4: Click on ‘New’ for a new trigger type from the right-hand side, as shown below:

new trigger type

Step-5: Click on the pencil icon in the trigger configuration to choose the trigger type.

trigger selection

Step-6: Select ‘History change’ as the trigger type in the ‘Other’ section.

History Change trigger

Step-7: Let’s name this trigger ‘History Change Trigger’.

Trigger name

Step-8: The trigger configuration will look like below, where you have the option to trigger on all history changes or some history changes. For now, we will select ‘All history changes’.

History change options

Step-9: Click on ‘Save’ on the top right-hand side.


Step-10: Now, navigate to the ‘Tags’ section in the left-hand side menu.

GTM Tags

Step-11: Select the ‘GA- Page view tag’, which triggers all the pages from the tags created.

GA Page View Tag

Step-12: In the trigger condition, click on ‘Edit’ and you will see the ‘+’ icon to add another trigger as shown below:

Add trigger condition

Step-13: Select the ‘History change trigger’ that we created.

trigger config for SPA

Step-14: Click on ‘Save’.

Steps to enable built-in history variables in GTM

Now in order for the history change trigger to work, we have to enable variables in GTM.  

Follow the below steps in GTM in order to enable history variables in GTM.

Step-1: Click on ‘Variables’ from the left-hand side.

GTM Variables

Step-2: From the ‘Built-in variables’ section, click on ‘Configure’ from the right-hand side.

Built in configuration

Step-3: Scroll down to the bottom and, from the history section, select all the options as shown below:

history buit in variables

Steps to validate SPA implementation in GTM preview mode

Step-1: Click on the ‘Preview’ mode in the GTM container from the top right-hand side.

GTM preview mode

Step-2: A new tab will open, when you click on GTM preview mode, enter the website URL to begin previewing the GTM container.

connect GTM container

Step-3: For this example, I enter the URL of a single page application (, and click on the ‘Connect’ button.

GTM container connect

Step-4: Once you click on ‘Connect’ the website is now in preview and debug mode.

Debug mode

Step-5: Additionally, in the Tag Assistant tab you will see that the GTM container is connected and is now in preview mode.

GTM Preview mode connected

Step-6: To check our implementation, let’s scroll on the website and validate if the GA page view is firing for the history change. 

In the Tag Assistant tab, you should see the history variable on the left hand side, as shown below:

history change preview

Step-7: Now, click on any of the history tabs from the left-hand side and check if the GA pageview tag is firing.

GA Pageview tag in preview mode

If the GA tag is firing for the history change, then it means that our implementation is working fine.

Step-8: In some SPAs you would like the GA pageview fire for only some history changes. In that case, check the variables in the preview mode and based on values in the history change variables, you can tweak the trigger condition.

history variables

Steps to publish the changes in GTM container

Step-1: Navigate to your GTM container and click on the ‘Submit’ button on the top right-hand side.

submit button

Step-2: Add a version name and version description. For example, you can enter SPA tracking enabled on the website.

GTM submission configuration

Step-3: Finally click on ‘Publish’.


Step-4:  You will now be able to see that the GA tag fires on the SPA page. Alternatively you can also validate in your GA real time reports.

Custom data layer for implementing tracking for SPAs

Apart from tracking SPAs using the history change trigger in GTM, you can also use the data layer approach. 

You may need developer help to implement a custom data layer on the website. For this solution, the developer has to use the custom data layer push method. This means they have to push data into the data layer with the help of JavaScript code.

Below is the code that the developer has to pass:

dataLayer.push({‘event’: ”Pageview”})

This snippet should be deployed on every new visible page to the user. Along with this, the developer can also pass information like page name, query path and URL in the data layer.

Once the data layer is implemented, you are able to see the values in the data layer correctly, based on that you can create a custom event in GTM.

Step-1: Log in to your Google Tag Manager account.

Step-2: Click on ‘Triggers’ from the left-hand side.

triggers 2

Step-3: Click on ‘New’ for a new trigger type from the right-hand side, as shown below:

new trigger type

Step-4: Click on the pencil icon in the trigger configuration to choose the trigger type.

trigger selection

Step-5: From the trigger type, select a ‘Custom event’.

custom event

Step-6: Let’s say your developer is passing the event name as ‘SPA pageview’, enter that as the event name and save the trigger.

custom event trigger condition

Step-7: Navigate to the GA pageview tag and add this trigger condition to fire the tag on a custom event.

SPA trigger condition

Note that, depending on your SPA, you can either keep the ‘all pages’ trigger or remove it based on whether a custom event triggers on the initial load of the page.

Common issues and solutions for single page application tracking

URL issue

In single page applications, because there is no URL change, some SPAs use the hash symbol in the URL when we navigate to a specific part of the website. 

However, in Google Analytics reports, it ignores the hash part of the URL so you will notice that all the pages are the same.

In this case, you can use the history change built in variables that will help us identify the visible pages the user navigated to on the SPA. 

To check if data is captured in these variables follow below steps:

Solution for the SPA URL issue

Step-1: Enable preview mode in the GTM container. 

Step-2: In Tag Assistant preview mode, click on the variables section and check if built in variables are capturing any data.

RztGro6 XgGr2W4V8MnkCgPAzU7CXNtajc9AG9afRrDFIGemJHsNjE6MRxGAgbPSZ9rBtnioF3tVNR9ZtTkWaQ ZCKHRmmIlA7U6iwUEVu IXe0u uSAxT0rNzBRm4uG37eq 7c

Step-3: Navigate to the GA pageview tag in GTM and click on ‘More settings’.

5azNFsQeT93grfCHrJesmuR7i2SU8PIisTYrDzjmfYFwx W8fhK4uIFbc4L0l047ZGxFQcaQkApgFTbNXqR7aMYwAV fdzn6idgg0O88YlWv3P0nFnBeIB5QGoGE5MzUuOkrlWU

Step-4: In the ‘Field to set’ section, select ‘page’ as the field name and as the field value select ‘New History Fragment’, as shown below:

4BN19A39ICSlggVB dYPv0BovBSXJw2OX965K9V9x

Step-5: If you are using a custom data layer approach, instead of history change, you have to create a data layer variable by selecting variables->user defined variables-> data layer variable.

178 BRR0VHMxfG31x8sAp8PDvxAtzisfu

Step-6: Just like how we added the history variable in the ‘Field to set’ section for the GA pageview tag, in the same way we have to add the data layer variable for custom data layer implementation as well. 

Step-7: Save and publish the changes and this will show the full URLs of the SPA in your Google Analytics reports.

Rogue referral issue

Another issue with single page applications is rogue referral. 

This is related to the wrong attribution of Google Ads as organic traffic in Google Analytics. 

Solution for the rogue referral issue in SPAs

As a fix for this issue, you need to insert the below code in your GTM container.

window.dataLayer = window.dataLayer || []; window.dataLayer.push({ originalLocation: document.location.protocol + ‘//’ + document.location.hostname + document.location.pathname + });

Create an HTML tag in GTM and fire it before the page view. 

To understand more about this issue and how to fix it refer to the Simo Ahava blog, who first reported this issue.

Congratulations! You have now learnt how to implement tracking for single page applications using Google Tag Manager.

Register for the FREE TRAINING...

"How to use Digital Analytics to generate floods of new Sales and Customers without spending years figuring everything out on your own."

Here’s what we’re going to cover in this training…

#1 Why digital analytics is the key to online business success.

​#2 The number 1 reason why most marketers are not able to scale their advertising and maximize sales.

#3 Why Google and Facebook ads don’t work for most businesses & how to make them work.

#4 ​Why you won’t get any competitive advantage in the marketplace just by knowing Google Analytics.

#5 The number 1 reason why conversion optimization is not working for your business.

#6 How to advertise on any marketing platform for FREE with an unlimited budget.

​#7 How to learn and master digital analytics and conversion optimization in record time.


My best selling books on Digital Analytics and Conversion Optimization

Maths and Stats for Web Analytics and Conversion Optimization
This expert guide will teach you how to leverage the knowledge of maths and statistics in order to accurately interpret data and take actions, which can quickly improve the bottom-line of your online business.

Master the Essentials of Email Marketing Analytics
This book focuses solely on the ‘analytics’ that power your email marketing optimization program and will help you dramatically reduce your cost per acquisition and increase marketing ROI by tracking the performance of the various KPIs and metrics used for email marketing.

Attribution Modelling in Google Analytics and BeyondSECOND EDITION OUT NOW!
Attribution modelling is the process of determining the most effective marketing channels for investment. This book has been written to help you implement attribution modelling. It will teach you how to leverage the knowledge of attribution modelling in order to allocate marketing budget and understand buying behaviour.

Attribution Modelling in Google Ads and Facebook
This book has been written to help you implement attribution modelling in Google Ads (Google AdWords) and Facebook. It will teach you, how to leverage the knowledge of attribution modelling in order to understand the customer purchasing journey and determine the most effective marketing channels for investment.

About the Author

Himanshu Sharma

  • Founder,
  • Over 15 years of experience in digital analytics and marketing
  • Author of four best-selling books on digital analytics and conversion optimization
  • Nominated for Digital Analytics Association Awards for Excellence
  • Runs one of the most popular blogs in the world on digital analytics
  • Consultant to countless small and big businesses over the decade

Learn and Master Google Analytics 4 (GA4) - 126 pages ebook

error: Alert: Content is protected !!