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.
Step-3: Click on ‘Triggers’ from the left-hand side.
Step-4: Click on ‘New’ for a new trigger type from the right-hand side, as shown below:
Step-5: Click on the pencil icon in the trigger configuration to choose the trigger type.
Step-6: Select ‘History change’ as the trigger type in the ‘Other’ section.
Step-7: Let’s name this trigger ‘History Change Trigger’.
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’.
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.
Step-11: Select the ‘GA- Page view tag’, which triggers all the pages from the tags created.
Step-12: In the trigger condition, click on ‘Edit’ and you will see the ‘+’ icon to add another trigger as shown below:
Step-13: Select the ‘History change trigger’ that we created.
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.
Step-2: From the ‘Built-in variables’ section, click on ‘Configure’ from the right-hand side.
Step-3: Scroll down to the bottom and, from the history section, select all the options as shown below:
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.
Step-2: A new tab will open, when you click on GTM preview mode, enter the website URL to begin previewing the GTM container.
Step-3: For this example, I enter the URL of a single page application (https://muffingroup.com/), and click on the ‘Connect’ button.
Step-4: Once you click on ‘Connect’ the website is now in preview and 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.
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:
Step-7: Now, click on any of the history tabs from the left-hand side and check if the GA pageview tag is firing.
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.
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.
Step-2: Add a version name and version description. For example, you can enter SPA tracking enabled on the website.
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.
Step-3: Click on ‘New’ for a new trigger type from the right-hand side, as shown below:
Step-4: Click on the pencil icon in the trigger configuration to choose the trigger type.
Step-5: From the trigger type, select a ‘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.
Step-7: Navigate to the GA pageview tag and add this trigger condition to fire the tag on a custom event.
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.
Step-3: Navigate to the GA pageview tag in GTM and click on ‘More settings’.
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:
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.
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.
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, OptimizeSmart.com
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
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of all the cookies.
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
Cookie
Duration
Description
cookielawinfo-checkbox-analytics
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional
11 months
The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy
11 months
The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.