{"id":4373,"date":"2020-06-02T09:58:30","date_gmt":"2020-06-02T06:58:30","guid":{"rendered":"https:\/\/trackingchef.com\/?p=4373"},"modified":"2020-12-14T14:57:49","modified_gmt":"2020-12-14T11:57:49","slug":"generic-pageview-tracking-for-single-page-applications-spa-using-google-tag-manager","status":"publish","type":"post","link":"https:\/\/trackingchef.com\/google-analytics\/generic-pageview-tracking-for-single-page-applications-spa-using-google-tag-manager\/","title":{"rendered":"Generic Pageview tracking for Single Page Applications (SPA) using Google Tag Manager"},"content":{"rendered":"\n
Whoa, I think this one easily ranks for both longest and most boring post title I’ve written. But heck, it’s a super valuable post \ud83e\udd37\ud83c\udffb\u200d\u2642\ufe0f<\/p>\n\n\n\n
When working with clients that use a modern development framework such as Angular or React, standard tracking methods just don’t work. Similar issues happen when working with Progressive Web Apps (PWA).<\/p>\n\n\n\n
These frameworks use a mechanism called Single Page Applications (SPA) that effectively loads only a single “page” when visiting a site. Additional pages and content are loaded in a manner that only loads the relevant content on the page, without executing a full page load.<\/p>\n\n\n\n
As a result, the Google Tag Manager script loads once and there aren’t additional Page Load events to trigger new pageviews beyond the first.<\/p>\n\n\n\n
GTM has a built-in solution to support this, with the initial pageview triggered from a page load event, and additional pages triggered using the History Change event.<\/p>\n\n\n\n
While this solution can work to some extent, I prefer to proactively have the developers trigger a pageview event with accurate data. This provides far more reliable data for analysis.<\/p>\n\n\n\n
To simplify the setup, I usually create a generic set of a Custom Event and Data Layer Variables that can be reused across any platform I want to track. This also enables me to add additional platforms down the line without altering the current GTM setup.<\/p>\n\n\n\n
The first step is creating a Custom Event that will be used as a Trigger. I like to name this event ‘pageview’, but it can be named anything you like. The important thing is to be consistent in its naming across whichever events you trigger.<\/p>\n\n\n\n
To create the trigger, navigate to the Triggers page in GTM and click “New.<\/p>\n\n\n\n
Select the trigger type is ‘Custom Event’ and set the Event name to ‘pageview’. The trigger should fire on all custom events.<\/p>\n\n\n\n