I'm writing a web analytics script and it's working great, with graphs, detailed stast, etc... but I' having a problem coming up with the query to display the correct number of "visits" or "unique" hits... I can display total page views just fine...
on the stats page I show the lifetime unique visits, and the lifetime total page views like so:
LIFETIME UNIQUE VISITS:
SELECT COUNT(DISTINCT(ip)) as unique_hits FROM tracking
WHERE project_id = '2'
LIFETIME TOTAL PAGE VIEWS:
SELECT COUNT(ip) as total_hits FROM tracking
WHERE project_id = '2'
Those both work fine... but when I run the stats for the week, the "unique visits" for the week are GREATER than the lifetime unique visits...
BECAUSE... I have a function that generates an array of dates in between the start and end parameters, and I loop through the array, and grab the total, and unique for that day.... but if someone visists the site each day, he will counted 7 time, once for each day, since the loop looks for the unique for each day...
WEEKLY STATS:
<?php
//$range is the array of dates.
foreach($range as $day=>$xdate){
$sql_Counter = "SELECT COUNT(DISTINCT(ip)) as unique_hits,
COUNT(ip) as total_hits,
'".$xdate."' as raw_date,
date_format(date,'%Y-%m-%d') as date_only ,
date_format('".$xdate."','%m-%d') as xdate_only
FROM tracking
WHERE
project_id = '".$proj."' AND date_format(date,'%Y-%m-%d') = date_format('".$xdate."','%Y-%m-%d')";
$q_Counter = mysql_query($sql_Counter) or die(mysql_error());
while($r2=mysql_fetch_assoc($q_Counter)){
// add each day's value to the total values for the week...
}
}
?>
How can I update the lifetime unique visits query to count the number of EACH unique ip's and add them together?
Thanks a lot.