Let's say an user with user_id(1000) and email([email protected]) logged into the reactjs based web application and browsed a few pages for 2mins and then moved to other application tabs/windows for 30mins and came back to the web application and browsed the app for 5more mins on April 1st 2021.
I would like track/get this user's time spent report in the Google Analytics report saying [email protected] with user_id(1000) has spent 7mins on April 1st 2021. Is there a way to track the same via GA if possible with react-ga, if it is possible how can we do it?
As of now with react-ga I'm tracking the userid property like the below:
ReactGA.set({userId});
If it is not possible via Google Analytics, is there any service provider that has this kind of feature?
Note: I have gone through existing q/a but unable to find/figure out the solution.
I was able use another tracker Riveted, which by it's definition:
More on it's page.
While Riveted is written with a direct global variable, we need to work around to make it available for the react project using exports-loader.
Here is what I could achieve:
Get the riveted.js locally as a file
Ensure to install the exports-loader via
npm install exports-loader --saveImport the same with the location of
revited.jsas:import riveted from 'exports-loader?exports=riveted!./riveted.js';After you have initialised
ReactGA.initialize(configs);If you check the source code of riveted, you will notice it's using the same
gaobject ofwindow.gaand thus the google analytics once initialised viaReactGA.initializeshould be enough.The
react-gaprovides an ability to extendga. They stategacan be accessed viaReactGA.ga()method.This gives developers the flexibility of directly using ga.js features that have not yet been implemented in ReactGA. No validations will be done by ReactGA as it is being bypassed if this approach is used.Then the
gaallows writing a custom pluginSo with all that here is what the code looks like:
Here is how the events are sent to GA from my local:
And the GA Dashboard showing
Time Spent: