Calculate Actual time spent on an app using React

I have to create a functionality where I need to capture the total time spent on a module in the application. Now we have 5 modules and each module has different pages and flows.

Say, we have a module to capture customer information. So the total time spent on this module would be the sum of time spent on each page (basic information, address details, education background, etc) in this flow.

We also need to calculate the time for sub-modules. For example, address details have a current and permanent address. In that case, we need to know the time spent on each of these pages and the total time should be assigned to the time spent in address details

Users can also close the page midway or go back to the previous page, and in that case, the time for that particular page should be started or resumed.

Would like to know how can I go about this and how the backend APIs should be designed for this as I am not able to wrap my head around this.

Many thanks in advance