Simple script to count website impressions
I created this simple script to compare my own statistics to those provided by Google Analytics. I am not saying that Google Analytics sucks because it doesn’t. However, sometimes data generation is delayed, so it’s good to have something to compare with. I’ll probably expand the script shown below and add further features. These could include: unique visits, time spent on-site, impressions per visit, and so on. At the moment one can only check statistics via phpMyAdmin. However, if you’re even a little skilled at PHP, you can create functions that allow you to see statistics via your own scripts. Bear in mind that this code is an example of how to create a script that counts all impressions done by humans. Since robots don’t execute JavaScript code, only human impressions count.
Firstly create the table in your MySQL database by executing this query:
CREATE TABLE IF NOT EXISTS `days_impressions` (
`day` date NOT NULL,
`impressions` int(11) NOT NULL,
PRIMARY KEY (`day`),
KEY `impressions` (`impressions`)
)
Then open your text editor and place this code:
<?php
$server = "localhost";
$user = "user";
$dbase = "database";
$passwd = "password";
$days_impr = "days_impressions";
//function that reports fatal errors in MySQL instead of showing them as output
if (!function_exists('fatal_error_report'))
{
function fatal_error_report($line,$file)
{
//fatal_error_report(__line__,__FILE__);
$to = "bugs@yourserver.com";
$subject = "Fatal error";
$ip = $_SERVER['REMOTE_ADDR'];
$messaget= '<html><body><h1>Fatal error has occured</h1><br />Details:<br />'.$file." on line ".$line." with this error ".mysql_error().'<br />Page accessed: <b>'.$_SERVER['REQUEST_URI'].'</b>. IP: <b>'.$ip.'</b></body></html>';
$postmast= "postmaster@yourserver.com";
$headers = "From: ".$postmast. "\n" .
"X-Mailer: PHP/" . phpversion() . "\n" .
"MIME-Version: 1.0\n" .
"Content-Type: text/html; charset=utf-8\n" .
"Content-Transfer-Encoding: 8bit\n";
mail($to, $subject, $messaget, $headers);
die ();
}
//end of error reporting function
}
$link = mysql_connect($server, $user, $passwd)
or fatal_error_report(__line__,__FILE__);
mysql_select_db($dbase)
or fatal_error_report(__line__,__FILE__);
$date = date("Y-m-d");
$found = 0;
$result = mysql_query("select * from $days_impr where day = '$date' limit 1;", $link)
or fatal_error_report(__line__,__FILE__);
if (mysql_num_rows($result))
while ($qry = mysql_fetch_array($result))
$found = 1;
if($found==1)
{
$result = mysql_query("update $days_impr set impressions = impressions + 1 where day = '$date' limit 1;", $link)
or fatal_error_report(__line__,__FILE__);
}
else
{
$result = mysql_query("insert into $days_impr values ( '$date' , '1')", $link)
or fatal_error_report(__line__,__FILE__);
}
mysql_close();
?>
Don’t forget to modify these variables’ values:
$server, $user, $dbase, $passwd, $postmast, $to
Once you have done this, save the file as, for example, “stats.php”. Then place this code in all pages that are available to visitors:
<script src="http://www.yourserver.com/statistics/stats.php" type="text/javascript">
</script>
I will likely upgrade the script and add more features as time goes by, so check back here from time to time.
Sponsored links

Where is the social media opportunity in your organisation?
...And Web Developers
WANTED: Web Designers
Technical Project Managers step forward.
How agencies help brands with "social"