Reputation: 227
I am working on a hybrid application in which login page is written in php and rest application is written on react. I want to implement google analytics for the same SPAs. I am pushing data layer in componentWillMount and my gtm script is being added via php code in index.php file.
The problem that I am facing is on first time and whenever page is transitioning from non-react to react pages, my GTM layer is not pushing data into gtm. if move with in react pages, everything is working fine.
Any help is highly appreciated.
Thanks
Upvotes: 0
Views: 1290
Reputation: 426
The problem you are facing is very common when using SPAs.
Let me give you an example:
How can you solve this?
The first option is to enable the built-in variables in the "History" (Variables > Configure > History) section:
Second, create a trigger (Triggers > New > History Change) that fires on all History changes:
In preview mode, you can check if your SPA pick up on history changes. Read more information about the variables here: https://support.google.com/tagmanager/answer/7182738?hl=en&ref_topic=7182737#history and about the trigger here https://support.google.com/tagmanager/answer/7679322?hl=en.
If that doesn't work you need to develop "virtual page view" events in the SPA when transitions are completed. Let me give you some more perspective. When a transition from page A to page B is completed fire the following event:
dataLayer.push({
'virtualPageTitle': 'My Login Screen',
'virtualPageURL': 'https://www.my-example/login',
'event': 'VirtualPageview'
});
The above will push virtualPageTitle and virtualPageURL to the dataLayer as and event VirtualPageview. GTM will be able to pick this up but you need to create 2 user-defined variables virtualPageTitle and virtualPageURL. The trigger will be VirtualPageview.
Hopefully, this clarifies things for you.
Upvotes: 1