
A hit counter is the best way to track your compaign and calculate each compaign Click-through rate (CTR).
A compaign or a product with higher CTR is the one that will bring more sales and more leads.
Table of Contents
How the hit counter works ?
Lets suppose you will show a Newsletter pop-up to your customers , and you wanna track its conversion to subscription.
Link your pop-up message to a specific url that will trigger an additional click then redirect to the Newsletter subscription.
The conversion rate is the number of subscription devided by the total count of hits on the Newsletter pop-up link.
Link the pop-up or the menu of newsletter subscription to Yourdomain/hit.php?u=1
in our website it is linked to :
https://promoteyourproject.com/hit.php?u=1
Each click on the Newsletter menu will be counted in the file
https://promoteyourproject.com/1.txt
However, the HTML that will show you the statistics for this link is :
https://promoteyourproject.com/1.html
The result of the first link will be like this :
You can add a hit counter using a simple PHP code, the code will be split to multiple phase , and here they are :
Create the “hit.php” file
Open notepad or go to your Cpanel account that your host provided for you.
Insert the PHP code that we will provid to you above , and save it .
In case you are working on a live file save it then upload it using the upload area on the Cpanel account.
Catch the hit URL
this code will catch the data transfered using a php file with a variable in the url like ?u=
example of a tracking link
yourdomain/hit.php?u=1
Make your own url link list to track
Using this method you will affect a specific code for each url to track that will trigger a counter using a simple and smooth url , like :
Yourdomain/hit.php?u=1
The above url will trigger the counter and redirect the first url link by clicking it.
$url[1] = “https://prmoteyourproject.com/newsletter”;
$url[2] = “https://prmoteyourproject.com/about”;
$desitnation=$url[$u];
Create a Counter file for each URL
if (!file_exists($stat)) {
touch($stat);
}
This code will generate a txt file for each tracked url.
If a link line is number 1 the tracking file that will be holding the number of clicks will be
yourdomain.com/1.txt
Implement the counter of hits
$counter = file_get_contents($stat) + 1;
}
file_put_contents($stat, $counter);
This code will read the content of the hit file’s counter and implement 1 additional visit and re-create a new file contain the new count of hits.
This will happen for each file before any redirection action.
Add a redirection function
echo “<script language=JavaScript> window.location.href = ‘”;
echo addslashes($destination).”‘;</script>”;
}
This function will redirect to any set variable in the $url parameter.
Finale code
Here is the full hit.php file :
$u=$_REQUEST[‘u’];
$stat=”$u.txt”;
$url=[];
$url[1] = “https://prmoteyourproject.com/newsletter”;
$url[2] = “https://prmoteyourproject.com/about”;
$destination=$url[$u];
$counter = 1;
if (!file_exists($stat)) {
touch($stat);
}
if (file_exists($stat)) {
$counter = file_get_contents($stat) + 1;
}
file_put_contents($stat, $counter);
file_put_contents($u.”.html”,”<meta name=’viewport’ content=’width=device-width, initial-scale=1.0′><h1> $counter clicks </h1> destination : <b>$destination</b>”);
if($u!=””){
echo “<script language=JavaScript> window.location.href = ‘”;
echo addslashes($destination).”‘;</script>”;
}
?>