user1899125
user1899125

Reputation: 125

How do I know how long a user or a single IP address spent on a page of my site?

How do I know how long a user or a single IP address spent on a page of my site? Can I use any event of JavaScript?

Upvotes: 3

Views: 402

Answers (4)

Nick
Nick

Reputation: 1

Use Ajax to send keep-alive information to your backend. Save it as session data in your database.

The session data can save the current page the user is watching and was active last time. Send the keep-alive information using, for example, setInterval every x seconds (depending on resolution needs).

There is no need for cron...

Upvotes: 0

napolux
napolux

Reputation: 16084

I will go for a Redis installation that keeps tracks of a specific hash created by a combination of IP address, USERAGENT and login information (if you have it).

Save the stuff in Redis with a timestamp (use hashes or whatever you like) and then add a "farewell" timestamp when the user logs out or leaves the site for an external link or unloads the page.

Upvotes: 0

Techie
Techie

Reputation: 45124

To implement inside your application

  • When a user loads a page, their "last page load time" is updated in the database.
  • A cron script runs every minute. It looks for users whose "last page load time" was in the last minute.
  • Increment the "time spent on site" for each user found.

In this way, I can keep track of users accurately to the minute. Just divide by 60 to get the hours.

Else

Google Analytics. You can check out it's features here.

Upvotes: 0

John Conde
John Conde

Reputation: 219834

This is what Google Analytics is for. It keeps track of visitors to your website and reports statistics to you like such as length of visit. They even offer an API to help you get this information.

Upvotes: 5

Related Questions