How to measure SPA website traffic with Google Tag Manager and Google Analytics

It is becoming more and more common to find websites programm with full stack frameworks such as Angular , React or Vue , which facilitate the implementation of Single Page Application or SPA  websites  where, by default, tracking scripts only work when users land on the website from outside, but not during navigation. Here we are going to see different solutions for this problem.

In a SPA website , unlike a classic Multiple Page Application (MPA) website

The JavaScript code includ in the pages is not execut every time the user visits a new URL , but rather when the user loads the website in the browser for the first time.

Once the user is inside the website, JavaScript is not execut Our how to phone number data is collect by person so we have 100 % accurate database. We are here to provide you a best full how to build phone number list database for your requirement with accurate and clean in real time so that we want grow business up to peak level of enhancement. We updat our database every week. All our database are fresh & recently updat 2024 My all databases is GDRP basic so you can buy here on best rate. again during navigation, unless there is something in the code that forces it . This is a problem for the JavaScript us by tracking tools , since it is only execut on initial load .

Let’s see how to solve it in two possible ways:

Setting up Google Tag Manager .
Without using Google Tag Manager and adding additional JavaScript.
Whichever way we choose, we will ne to detect, directly or indirectly, the calls to the history.pushstate method , responsible for changing URLs by JavaScript in a SPA .

 

how to build phone number list

So let’s see how to apply each solution to Google Analytics . In theory, what is explain below  can be appli to other tracking scripts.  However, depending on the specific case, it may be necessary to add something more.

Tracking a SPA using Google Tag Manager (GTM)

In the following example,  we start from an already creat Google Universal Analytics account and a GTM container that we will have install on the website , according to Google’s instructions.

First, we ne to create a trigger that fires when a page change occurs in the SPA.  This can happen in three situations:

When the user clicks on a link that changes the content and URL with JavaScript.

The trigger responsible for this is the “ Change in history ” trigger, which we configure as follows:

GTM SPA Activator
We have nam the trigger “SPA tracker” and mark it to fire on all historical events . We save it and then, if we haven’t already, we should include the Google Analytics ID in a constant , so that it is available when setting up any tag:

Constant declaration in GTM

With each activation of a history event, a Google Tag Manager integrat variable will be updat, which will tell us the path of the URL where we are and which we will find already configur as “Page Path”:

GTM built-in variables
Now we create a variable of the “Google Analytics Configuration” type where we assign the built-in variable “Page Path” to the Google Analytics “page” field, and the tracking ID to the constant declar previously:

GTM configuration variable assigning the pre-existing variable «Page Path» to the «page» field.
We save the variable as “Analytics SPA Configuration” and we are now ready to create the “Google Analytics” type tag , to which we will assign the configuration variable creat in the previous step and the “SPA tracker” trigger:

Google Analytics SPA Tracking Tag in GTM

We see that we have also add the “All Pages” trigger , so trust to increase branding that it is also trigger when entering a page . If we already had another Google Analytics tag configur with the same tracking ID and the “All Pages” trigger, we can either delete it or leave it and not add the “All Pages” trigger to the new tracking tag. In the latter case, we could have a different configuration for the MPA and SPA pages of the website, while if we do it as in the screenshot, the configuration will be unifi.

Finally, we give the tag a name. For example: “Tracking analytics SPA”, and by clicking on the “Preview” button (top right in GTM), we will check within the site if it works correctly from the GTM console . Here we will see that the tag is trigger with each “Page View” event due to the “All Pages” trigger and every time we click on a SPA link thanks to the “SPA tracker” trigger that we creat of the “Change in history” type :

In the GTM console we see that a “Page View” event and two “History” events have been trigger because we have enter the SPA and visit two pages, so our tag has been execut three times.

Once the configuration is complete and test, we send the changes and publish the new version .

If we want to pass more information to Google anhui mobile phone number list Analytics, we would ne to add additional variables on data creat in the Data Layer , where the developer can add the necessary information. There may already be interesting information inside the Data Layer if we look at the gtm.newHistoryState object , where with each change in the history,  the object that the developer passes to the history.pushstate method is sav to know the state of the application and the page visit.

Most of the time, in gtm.newHistoryState we will only find an identifier of the visit page, but in other cases it may have interesting information.

 

Monitoring a SPA by adding additional JavaScript

Although GTM is a fantastic tool for separating the installation of tracking scripts from the website code , sometimes it is better not to use it.

Additionally, the analytics expert would not ne to configure the tag manager . If the website also uses a CMS, it will probably have a plugin to easily install Google Analytics without the help of a developer, so sometimes not using GTM is much.

 

Leave a comment

Your email address will not be published. Required fields are marked *