WordPress.org

Forums

Trying to add a live statistics on site (2 posts)

  1. shaileshtripathi
    Member
    Posted 2 years ago #

    I want to show the live statistics of total users, users signup today and online users on site Free Facebook Likes Can anyone tell me hows this gonna be possible?

  2. bcworkz
    Member
    Posted 2 years ago #

    I've never developed anything like this, and am unfamiliar with what others have done. Never the less, this is the approach I would take.

    The total users can be queried from the DB, but I would keep an interim cached value with the users today value to avoid more elaborate queries for every page load. It can be incremented with the users today count, then a scheduled task can be run to do an actual query to bring it back to a true count. This will correct for removed users, etc. If run once a day at midnight, it can reset the users today count as well.

    To keep the counts updated, you will need to find the proper hook that fires when a user is added. All it needs to do is increment the counts by one.

    The online user count is trickier. Servers have no way to know if a paged served will result in a new request (user online) or it is the last page requested (user soon to go offline) You pick an arbitrary time frame and if no new request is received in that time, assume the user is offline. So you need a table to store each unique IP and a time stamp of the last page request. All IPs that have a timestamp fresher than that arbitrary time period are considered online, that is the number you report.

    Rather frequently, you would run a task to remove all old timestamps to keep the table at a manageable size.

    How do you record every page request to keep track of users online? In WP, this is fairly easy, as nearly every page loads the header template, so just add a call to the 'wp_head' action. This call would locate the user IP in the table and update the time stamp. You only need to add a new IP to the table when a user logs in.

    I hope this gives you some ideas for a direction to take.

Topic Closed

This topic has been closed to new replies.

About this Topic