Hubro
Hubro

Reputation: 59323

How do I measure how many visitors my page has right now?

For a web application I'm writing it's going to be key to measure at a frequent interval (every minute or so) how many visitors are in a specific part of my application right now. Say my application has 10 subsections that are switched between using JavaScript and made persistent using #!/page8 style hash anchors. What would be the best way of doing this accurately?

My current plan is to just save the amount in a database, adding and subtracting every time a user clicks on the page or leaves the page. This doesn't really seem like a very good solution to me though. Events to track page leaves could be cancelled due to closing browsers, people could be screwing with the data by running the API calls for visiting/leaving etc. It would be a lot of work to get reliable.

Edit: To specify, my application won't have user accounts. Also I'm not just trying to log how many visitors my pages have had. I'm trying to accurately know how many visitors are on my page right now. Which means that I'll need some reliable method of knowing then a visitor is no longer on my page. That's where the problem lies. I just got a bright idea that I could have the JavaScript on their page notify my server every 10 seconds or so that they are still there, and have their visit be timed out and removed if they don't. Would that work? Couldn't this be a problem for my puny server if I were to get thousands of people using my app simultaneously?

Any thoughts?


I'm tagging this with a lot of common web development tags since it's a very open question and any of these tags could contain the answer

Upvotes: 1

Views: 564

Answers (6)

Michal
Michal

Reputation: 3368

Depending on the server capacity you have you could employ recurring requests to a .php file that would keep a custom session track alive. But beware - this causes wast load on the server, so only go this way if you are aware of the consequences!

With session_start(); you could write a custom session identifier in a database table with fields like ID, ip_addr, session_track_id, timestamp. Don't use the session_id itself as that might be a security issue. Update the timestamp in each .php you call.

Additionally create a custom .php where only the timestamp is updated. Call this via jQuery AJAX calls every 60 seconds (or whatever interval you need). You could employ some security to this step too to avoid direct API call you mentioned. For example issue a next request id that is returned in the AJAX calls and needs to be a parameter of the next one, if it's not, the timestamp won't be updated.

Create a cron that runs every 90 seconds (or whatever interval you need - but longer than in the AJAX calls obviously). This cron would look for entries in the table that are older than X seconds and delete them.

To find out the number of active users simply count the rows in the table.

There are additional security options that could be employed in this approach.

This would solve your situation - when a user would close his window, no AJAX calls would keed the session alive anymore and it would get deleted from the database after some seconds (90 in the scenario above).

However - consider that when you issue a request every 60 seconds and you have 1000 users online, it's at least 60 000 requests per hour to you database! Strong optimalization is a must here...

This solution worked fine for me in one project however.

Upvotes: 1

jrumbinas
jrumbinas

Reputation: 426

Basically you need a table with 3 fields:

user_activity - user_id - last_access_timestamp - last_visited_page

Every time user asks for data last_access_timestamp and last_visited_page MUST be updated. Note that you need to submit #page8 part manually, as it does NOT reach the server!

Then you do need a to define when user is considered inactive. Let's say: 15min.

#To get total users:
SELECT * FROM user_activity WHERE last_access_timestamp > NOW() - 15*60;

#To get total users for page #page8:
SELECT * FROM user_activity WHERE last_visited_page LIKE '%page8' AND last_access_timestamp > NOW() - 15*60;

Upvotes: 0

Ravi Vanapalli
Ravi Vanapalli

Reputation: 9942

Best is to use Google Analytics

http://www.google.com/analytics/

Upvotes: 0

ikromm
ikromm

Reputation: 523

I have seen some options around the net, but I personally use this one:

I store a timestamp in the users table in the database that is updated each time a user acts (clicks a link or so)

What you need to do then is just get all users that their last action was X seconds ago, by checking the timestamp. That way you can have the names of the users that are active along with their number.

Also the time you can say a person is active is totally configurable.

That method I use for the whole site. For different pages you could have a different table to store activity and page id or something. Use your imagination. :)

Upvotes: 0

ChrisLively
ChrisLively

Reputation: 88044

A different approach would be to simply log the user, date/time and page for each request.

Then, at the specified interval run a query that rolls up that data showing page and user count. Store those results in a separate table.

This way you could easily graph the visitor history as well as give an up to the minute response on the number of "current" visitors.

Once the data is rolled up you could obviously delete the data from the log table.

Another advantage is that the page requests won't be locking the table for each request. Whereas incrementing/decrementing a counter would. Depending upon the traffic level the counter approach would have a negative performance impact.

By using the log you avoid the locking issue and essentially move the roll up into an external process such as a SQL job.

Upvotes: 0

Jakob W
Jakob W

Reputation: 3377

Haven't tried it myself, but you could checkout some tool like http://mixpanel.com/

Upvotes: 1

Related Questions