Track Time on Site

I am trying to do a simple time tracker on my site to see how long a user stayed from page to page. I have it keeping track of the appropriate time spent on the page, but I can’t seem to store it in the database :confused: . What I am trying to do is do a quick Ajax call to the database before the page unloads to store how many seconds that user was on the page. But the browser unloads and reloads and loads to quick for the Ajax call to complete finishing its request.

Does anyone have possible ideas on how to accomplish what I am trying to tackle? I am sure there is a way since Google Analytics is doing an ok job at it

I appreciate any help, thank you in advance