Simple script to count website impressions

Posted on November 24, 2008 Categories: Search

post author

Written by: Jan

Jan is an eccentric Slovakian SEO wizard. When he's not researching search, optimising sites, building inbound links, or working on content creation, he's a part-time professor, teaching PHP to his students at university.


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

Follow us:

Leave a Reply

Archive

July 2010

June 2010

May 2010

April 2010

March 2010

February 2010

January 2010

December 2009

November 2009

May 2009

April 2009

March 2009

February 2009

January 2009

December 2008

November 2008

October 2008

September 2008

July 2008

June 2008

March 2008

February 2008

January 2008

December 2007

November 2007

October 2007

About Us

A team of nerds, creatives and strategy ninjas based in central London, building websites, social networks, widgets and social media apps.

We have a portfolio that is good enough to make a male peacock blush, and some killer outside-the-box products...in a box.
Ask us a Question

Blog posts