Last updated on May 30th, 2014 at 05:04 am

VN:F [1.9.22_1171]
Rating: 7.4/10 (70 votes cast)

This tutorial shows how to track your vistors click using simple php and Ajax To implement the click tracking tool we need to create 2 files:
Demo.html: This file contains the html with the links and the Ajax code.
clickTracker.php: This files will be called by Ajax and records the click event.
DEMO.HTML will look like this

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Ajax click tracking example</title>
<script>
function getHTTPObject(){
if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
else if (window.XMLHttpRequest) return new XMLHttpRequest();
else {
alert("Your browser does not support AJAX.");
return null;
}
}
// Change the value of the outputText field
function setOutput(){
return true;
}
// Implement business logic
function doTrack(element){
httpObject = getHTTPObject();
if (httpObject != null) {
dst = element.href;
src = document.location.href;
httpObject.open("GET", "clickTracker.php?src="+src+"&dst="+dst, true);
httpObject.send();
httpObject.onreadystatechange = setOutput;
}
}
var httpObject = null;
var src = null;
var dst = null;
</script>
</head>
<body>
<a href="http://www.mistonline.in/wp" onclick="doTrack(this);" > Test Click </a>
</body>
</html>

Now our clickTracker.php file

<?php 
$src = isset($_GET['src']) ? $_GET['src'] : "-";
$dst = isset($_GET['dst']) ? $_GET['dst'] : "-";
$f = fopen('clickReport.txt',"a+");
fwrite ($f, date('Y-m-d H:i'));
fwrite ($f, " : " + $src + " : " + $dst + "");
fwrite ($f, "\n");
fclose($f);
?>

We will write data dynamically using AJAX to a simple text file,
If you are not sure how to read/write data usinga text file in php then GO HERE
Don’t forget to create a file with name clickReport.txt,

Now you can track your website clicks, Thanks ajaxf1
VN:F [1.9.22_1171]
Rating: 7.4/10 (70 votes cast)
6 thoughts on “Simple Ajax with PHP click tracker”
    1. Hi Cleo,

      All the broken links has been updated now. Please have a check. Thanks for bringing this up.

      Regards
      Admin

      VN:F [1.9.22_1171]
      Rating: 0.0/5 (0 votes cast)
  1. It is not working,by the way,it doesnot write in clickreport.txt

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    1. Hello Sibo,

      Please make sure you create txt file before running the script. This script doesn’t have the ability to create the text file. Please let me know if you still face any issues.

      VN:F [1.9.22_1171]
      Rating: 0.0/5 (0 votes cast)

Leave a Reply

Your email address will not be published. Required fields are marked *