Track single page apps with GA

Business Benefits

Track user behavior in single page apps using Google Analytics.


Ask your developers to create custom dataLayer events to track transitions when users navigate from one page to another.

A single-page app is typically very tightly integrated to the backend stack. This means that the best way to make sure that events trigger at the correct moments is to add dataLayer.push() calls using the stack itself; best left to your developers rather than created through Google Tag Manager.

Ask the developers to include the current page URL or path as well as the current page title in the dataLayer.push() call.

Flush the dataLayer between transitions if you need to avoid variables from one page leaking to the next.

One way to do this is to call window.google_tag_manager[''].dataLayer.reset() after the user leaves the page. This allows the app to behave like a regular site, and you avoid a huge amount of information accumulating in the data layer. The reset() call clears everything in GTM’s data model, so you can start building the variables used on the page the user navigated to from scratch.

If you want to flush individual keys like ecommerce, push their value as undefined after they’re used by tags.

Send the original page location - the one in the address bar when the page was first loaded - to Universal Analytics as the location field value to avoid rogue referral issues.

Sending the original page location to Universal Analytics as the location field ensures that campaign attribution uses the original page URL. Virtual page paths are sent using the page field.

Set the siteSpeedSampleRate field to 0 for virtual page views to avoid collecting page timing data on virtual page loads and inflating page timings because the same timings have been collected more than once.

Navigate to More Settings > Fields to set it, and add a new field to your Virtual Page View tags that fire on custom events, not on Page View triggers.

Field Name: siteSpeedSampleRate
Value: 0

Last edited by @hesh_fekry 2023-11-14T11:10:16Z