How to measure a time an user spent on a page?

I am doing a small task with brave-browser source code.
I would like to measure a time of an user spends on a page. I don’t know code flow when an url is loaded and changed or finish. Anyone help me? Thanks

I found a solution for this. If anyone need it, DM me.