HOWTO: Track user activity on your PHPBB forum
Introduction
I run a music forum called Small Night In using PHPBB forum software. Like any website owner, I’m interested in the user activity on my site. That’s why I run Google Analytics, which covers visitor numbers, referring sites, and a wealth of other useful data. Additionally, PHPBB reports the number of users, topics and posts in total and on average.
However, what PHPBB doesn’t report is potentially more useful: long-term trends in growth or decline of active users and new posts. Tracking visits in Google Analytics isn’t sufficient, because some visitors are casual and don’t return – active, registered users are the most valuable to a forum. Tracking the average daily number of posts is no good either, because it gives an average and not an ongoing series of activity snapshots.
To address both these issues, I’ve written a useful script which will allow someone with a minimal amount of PHP/MySQL knowledge to track the long-term number of active users and daily posts on their website.
The script, in brief:
- Get the time and date 24 hours ago
- Count how many users last visited later than that time
- Count how many posts were created later than that time
- Output the results
The script output is (according to the user’s needs) either a simple PHP echo() of the number to a webpage, or written to a .CSV file for easy long-term tracking and charting.
The script
The first part of the code is just database settings, which you should personalise as needed:
// Change these to your MySQL details
$host = "localhost"; // you probably won't need to change this setting
$user = "your_php_username";
$pass = "your_php_password";
$database = "your_phpbb_database_name";
The next section grabs the current timestamp and subtracts one day’s worth of seconds, as well as yesterday’s date in YYYY-MM-DD format:
$time24hoursago = time() - 86400; // Get the timestamp for a day ago by subtracting 24*60*60 seconds
$date = date('Y-m-d', $time24hoursago);
In the next section, we get the number of entries (users) in `phpbb_users` where the timestamp in `user_lastvisit` is more recent than the $time24hoursago timestamp:
// Database query, recent users
$conn = mysql_connect( $host, $user, $pass ) or die ("Could not connect to MySQL");
$rs = @mysql_select_db($database, $conn) or die("Could not connect to database");
$sql="SELECT COUNT(user_id) FROM `phpbb_users` WHERE `user_lastvisit` > '$time24hoursago'";
$rs = mysql_query( $sql,$conn );
$activeusers = mysql_fetch_array($rs);
Then we do the same thing for recent posts:
// Database query, recent posts
$sql="SELECT COUNT(`post_id`) FROM `phpbb_posts` WHERE `post_time` > '$time24hoursago'";
$rs = mysql_query( $sql,$conn );
$posts = mysql_fetch_array($rs);
Now the arrays $activeusers and $posts contain the number of users and posts. If you just want to see the results, a simple echo command will do the trick:
echo $date . ',' . $activeusers["0"] . ',' . $posts["0"];
Of course, you might be like me, and want to automate the entire process using cron. This next bit of script will write the date and number to a .CSV file called “forum_activity.csv” in the format YYYY_MM_DD,users,posts. For the following process, I’ve modified the file append tutorial on Tizag.com:
$myFile = "public_html/scripts/forum_activity.csv"; // this is the file we'll put the results in
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = $date . ',' . $activeusers["0"] . ',' . $posts["0"] . "\n";
fwrite($fh, $stringData);
fclose($fh);
Mind the file path $myFile. If you’re running this script through a web browser, you’ll want to make the reference local – that is, have the file path as just “forum_activity.csv” and place the file in the same folder as the script. But if you run this script through cron – for instance daily at midnight, as I do – you’ll want to put the full path to the .csv file or cron won’t be able to find it. Always test before leaving this running for a week and finding it doesn’t work!
Leave a comment