User.com on Single Page Application
Learn how to properly implement User.com on SPA
Remember! This guide is to be treated as an addition to a standard integration process.
Single Page Applications work differently than standard websites and therefore you need to take different approach while integrating User.com.
As on Single Page the page does not reload at any point in the process, you need to have a way to send Page Views while user navigates through your website and update User information upon Log in/Log out and Registration. Here are the two of possible ways:
Google Tag Manager
First, please do this check on your Website:
When you navigate from page A to page B, does the URL change in your browser?
If the answer is Yes, continue reading
If the answer is No, skip to the section below
Making GTM acknowledge the change of the URL.
Open your GTM Container and go to Variables
Click Configure and select all positions under History section
Close the slide-in window and go to Triggers
Click on New and enter trigger configuration
Here click on History Change and configure it as shown on the picture below and Save
Now everytime URL changes, the history event will be fired and can be used as a new Trigger to send data to User.com for example.
Open the preview mode, go to your Website and once loaded navigate to another part of the Website, for example About Us. If you see History event in the Debug window then all is set!
Now go back to GTM and create new Custom HTML Tag. The goal is to send a Page View whenever User navigates to another subpage. We will use UE.pageHit for that.
Code of the UE.pageHit should look somewhat like this:
Correct apiKey can be found in your application, go to Settings -> Setup & Integrations. Finished tag will look similar to this:
Now refresh the GTM, refresh your website and try navigating through your website. Then check if you see correct Page Views in the user timeline. If yes, congratulations, this part is done!
Implementing UE.pageHit to update users profile on Log in or Register
The goal here is to correctly update user tracked as anonymous in User.com app when he/she logs in or registers.
You need to think about unique happening when your user successfuly logs in or registers, for example url has signup#success at the end or you send a custom event to GTM? Here is the article covering that matter.
You need to create a new Custom HTML tag containing UE.pageHit but now populated with all the user information that you gather during the log in/registration process.
The trigger you should use is either your custom event or, again, history change. If you've chosen the latter, create a new trigger of type History Change and make it fire only in specific case, for example if the new URL contains signup#success
Now everytime someone logs in or registers, anonymous user in User.com will get updated with correct data. Please test it using GTM Preview mode and if everything updates correctly, congratulations again!
This part will be useful if your Single Page does not change URLs at all or it does it in a way that will not be useful to send to User.com.
Here is a list of resources for the Developer:
Sending correct amount of Page Views
Our script send data and page views on a page load, since SPA loads only once, upon initial visit, you will lose all of the page views when visitors navigate from page A to page B.
To overcome this, send UE.pageHit on every route change, it will pass new URL to the User.com
Updating anonymous user upon Log in/Register
In a similar fashion, when your user logs in or registers, the profile in User.com will not be created until the page reloads.
To overcome this, send UE.pageHit populated with all of the users data on log in and register, this way new user will automatically update anonymous one
Good practice is sending populated script or UE.pageHit only when user Logs in or Registers and update page views with basic UE.pageHit afterwards(containing only apiKey)