I set it up so that every time you view a product a value is updated in a database table daily_product_stats. It should only count your view once per session per product. So to see how many times product 435 was viewed today we can easily do that, and easily see which product is getting looked at the most today.
But I started noticing strange results, like product 2324 was looked at the most at 36 times by 9am. I was suspicions something was not right since thats not a very popular product.
So I decided to also track each individual view with an ip to make sure each view was unique. So everytime I update daily_product_stats I am inserting a new row into product_ip_tracking with the IP.
So I did this for a few minutes and the results got stranger.
First, here is the code on the page that is doing the tracking:
include_once("php/packages/pdomanager/PDOManager.php");
$pdo = PDOManager::getConnection();
$product_stat_qry = '
INSERT INTO daily_product_stats
SET product_stat = 1, product_id = :pid, first_entry = :date
ON DUPLICATE KEY UPDATE product_stat = product_stat+1
';
// Update temp stat table
$product_ip_tracking_qry = '
INSERT INTO product_ip_tracking
SET product_id = :pid, date = :date, user_ip = :ip
';
$stat_data = array(
"pid" => $_GET['pid'],
"date" => date('Ymd')
);
$track_data = $stat_data;
$track_data["ip"] = $_SERVER['REMOTE_ADDR'];
$stat_sth = $pdo->prepare($product_stat_qry);
$track_sth = $pdo->prepare($product_ip_tracking_qry);
if(!isset($_SESSION['daily_product_stats'][$_GET['pid']]))
{ // only update once per person per session
$stat_sth->execute($stat_data);
$track_sth->execute($track_data);
$_SESSION['daily_product_stats'][$_GET['pid']] = 1;
}
Looking at the 2 different queries that get executed together, you should be able to assume that whatever product_stat is for a product_id on the daily_product_stats table there should be that many rows for the same product_id on the product_ip_tracking table.
So I let it run for a few minutes and then I ran this query on the database:
SELECT product_id, COUNT( user_ip ) AS times, user_ip, DATE
FROM `product_ip_tracking`
GROUP BY user_ip
ORDER BY times DESC , product_id
and I found that product_id 8151 was viewed 84 times today from the same IP address 66.249.65.123. I thought my code should prevent the same IP from being tracked more than once per day. I did some research on the IP and it looks like it most likely a Google bot, so I thought maybe SESSIONS can’t be set with bots, so thats why its getting tracked multiple times…
So I then expected to look into the daily_product_stats table for product_id 8151 and for its product_stat to be 84. It was not, it was only 2. I find this very bizarre because I do not see how that is possible. I am not sure at all what is going on here.
Does anyone have any ideas?
your select query looks a little funky, so that probably explains your funky result. Intuitively you’re not just grouping on IP but also on Product and Date, so you should add product_id and date to your GROUP BY clause. Thus, your query should be more like this:
I’ll defer to someone who is more knowledgeable with the PDO MySQL library to help you out with your app logic.