Track website referals - howto


What are the website referrals?
Referrals are links from search engine queries or websites which had passed the user to your website.

Why you need referral tracking?
Simply this way you'll know what are your most visited keywords and key phrases which will allow you to optimize your website content further.

Here is a simple working example on how one can easily track its website referrals and keywords. Just put this JavaScript code in your webpage:


<script type="text/javascript">
var q = parent.document.referrer;
document.write("<img height=\"1\" width=\"1\" src=\"decode.php?q=" + q +"\">");
</script>


Here is the analyzing decode.php code:

<?
if (empty($q)) {$q = getenv("HTTP_REFERER");}

/* filter some bad characters */
if (!empty($q)) {
$q = str_replace("+", "", $q); $q = str_replace("-", "", $q);
$q = str_replace(":", "", $q); $q = str_replace("!", "", $q);
$q = str_replace("?", "", $q); $q = str_replace(".", "", $q);
$q = str_replace(",", "", $q); $q = str_replace("'", "", $q);
$q = str_replace("\"", "", $q); $q = str_replace("\n", " ", $q);
$q = str_replace("\r", " ", $q); $q = preg_replace("(\s+)", " ", $q);
$q = preg_replace("/([0-9])/e", "", $q);
if (get_magic_quotes_gpc() == 1) {$q=addslashes($q);}
$q = strtolower($q); $q=trim($q);
if (iconv('UTF-8', 'UTF-8', $q) == $q) { $q=iconv("utf-8", "cp1251", $q); }
$q = htmlentities($q, ENT_QUOTES);
$q = html_entity_decode($q);

if (strstr($q,"put_your_domain_name_here")) {}
else {
//save_file($q, "referals.txt");//uncomment this if you want to save in file
//save_file($q); //uncomment this if you want to save in mysql database
}
?>


Use this function if you want to save the referrals in file. Fist create file referers.txt on your server and set it with 0777 rights (read,write,execute) :

<?
function save_file($message, $filename) {
trim($message);
if ($message!=""){
$message="\n".$message;
$fp = fopen($filename, "a") or die("error opening");
$write = fputs($fp, $message);
fclose($fp);
}
}
?>

Use this function if you want to save the referrals in a Mysql database. First create the table referers:

CREATE TABLE referers (
count int(4) NOT NULL default '0',
srch_word varchar(100) default NULL
) TYPE=MyISAM;


<?
function save_file($message)
{
include_once("mysql_oop.php");
$db1= new mysqldb;
$db1->setsql("SELECT srch_word FROM referers WHERE srch_word='$q'");
$db1->selectquery(); $numberrows=$db1->getnumberrows();

//if we have already that referrer -> update its count
if ($numberrows > 0) {
$db1->setsql("UPDATE referers SET count=count+1 WHERE srch_word='$q'"); $db1->selectquery();
}

//if it's new referrer -> record it
else
{
$db1->setsql("INSERT INTO referers(srch_word) VALUES('$q')"); $db1->selectquery();
}
}

?>

Here is the included object oriented MySql frontend :
mysql_oop.php


<?
class mysqldb {
//set up the object
var $dbhost;
var $db;
var $dbuser;
var $dbpassword;
var $sql;
var $result;
var $numberrows;
var $dbopenstatus;
var $dbconnection;
var $insert_id;


function get_insert_id(){
$this->insert_id=mysql_insert_id();
return $this->insert_id;
}

function setdbuser($req_dbuser){$this->dbuser = $req_dbuser;}
function setdbpassword($req_dbpassword){$this->dbpassword = $req_dbpassword;}
function sethost($req_dbhost){$this->dbhost = $req_dbhost;}
function getdb(){return $this->db;}
function setdb($req_db){$this->db = $req_db;}
function getsql(){return $this->sql;}
function setsql($req_sql){$this->sql = $req_sql;}
function getnumberrows(){return $this->numberrows;}
function setnumberrows($req_numberrows){$this->numberrows=$req_numberrows;}
function setdbconnection($req_dbconnection){$this->dbconnection = $req_dbconnection;}

function mysqldb(){
global $HOST,$DB,$WEBUSER,$WEBPASSWORD;
global $TRUE,$FALSE;

//set here your server settings: user,host,password,database_name
// example: $DB="your_database_name";
// $HOST="your_hostname";

//$this->sethost($HOST); //uncomment if you must specify mysql database host
$this->setdb($DB);
//$this->setdbuser($WEBUSER); //uncomment if you must specify mysql user
//$this->setdbpassword($WEBPASSWORD); //uncomment if you must specify mysql password
$this->setdbconnection($FALSE);
}

function opendbconnection(){
global $TRUE,$FALSE;
$this->dbconnection=mysql_connect("$this->dbhost","$this->dbuser","$this->dbpassword");

if ($this->dbconnection)//if we have connected select and return true
{
mysql_select_db($this->db) or die("Unable to select database");
$this->setdbconnection($TRUE);
return true;
}

else {$this->setdbconnection($FALSE); return false;}
}

function closedbconnection(){
if($this->dbconnection=$TRUE){mysql_close($this->dbconnection);}
}

function selectquery(){
global $TRUE,$FALSE;
if($this->dbconnection==$FALSE) {$this->opendbconnection();}//open connection if still not opened

$this->qry=@mysql_query($this->sql); if(!$this->qry){$this->numberrows="0"; return false;}//query error

else{//querry passed

$this->numberrows=@mysql_numrows($this->qry);
//if we have any result fill result array
if($this->numberrows>=0) { for($x=0;$x<$this->numberrows;$x++) {$this->result[$x]=@mysql_fetch_row($this->qry);} return true;}

else{$this->numberrows="0"; return false;}//if we don't have results give error

}//end query passed

}

}

?>

That's it! Just wait some time to track some referrers. You can look at your referrals either by querying the MySQL table referers or looking in your browser the file referals.txt
If you have any comments/suggestions please write them down.

0 коментара: