Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is Google analytics gtag supposed to work in a SPA?

From what I can tell, how google recommends to set up gtag, it's a function that is always pushing more variables onto an array:

window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}

Following the docs on how to use it in a SPA, it seems I'm actually creating a memory leak.

gtag('config', 'GA_TRACKING_ID', {'page_path': '/new-page.html'});
console.log('window.dataLayer', window.dataLayer);

My console shows that every time I navigate to a new page the window.dataLayer array gets larger.

I'm assuming there's something I'm not understanding, because I doubt that Google wants me to create a memory leak in my app, but I'm stumped now.

like image 815
Otto Avatar asked Jul 07 '18 01:07

Otto


People also ask

Does Google Analytics work with spa?

Google Analytics and Google Tag Manager (GTM), though designed with traditional round-trip-based websites and web applications in mind, can be configured to work properly with single-page applications (or SPAs). Common technical issues encountered when tracking SPAs with these tools are: Only the first page is tracked.

How do you track a spa?

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.

What is Google Analytics GTAG?

The Google tag (gtag. js) is a single tag you can add to your website to use a variety of Google products and services. Instead of managing multiple tags for different Google product accounts, you can use the Google tag across your entire website and connect the tag to multiple destinations.

How do you track user behavior on an SPA since it is missing other web pages?

To do this, go to Variables → Configure and activate all checkboxes under History. Once enabled, you will see those history variables in the Variables tab in the GTM preview pane. It depends on the architecture of your web SPA whether or not these history variables are useful for tracking.


1 Answers

The size of the dataLayer is capped at 300 (FIFO style) currently.

That's not currently documented and likely to change in the future. But the limit is there exactly to avoid blowing up memory in case repeated events happen inside the same page.

like image 143
Eduardo Avatar answered Oct 18 '22 06:10

Eduardo