Simple Ajax with PHP click tracker
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
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<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 language=”javascript” type=”text/javascript”>
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(null);
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
We will write data dynamically using AJAX to a simple text file, If you are not sure how to read/write data using
a text file in php then
Code:
<?php
$src = isset($_GET['src']) ? $_GET['src'] : “-”;
$dst = isset($_GET['dst']) ? $_GET['dst'] : “-”;
$f = fopen(’Trackdata.txt’,”a+”);
fwrite ($f, date(’Y-m-d H:i’));
fwrite ($f, ” : ” + $src + ” : ” + $dst + “\r\n”);
fclose($f);
?>
Thats it finished.Now you can track your website clicks, Thanks ajaxf1
Related posts:
- Ajax Page With PHP
- Disabling right click menu using javascript Enhanced Version
- Get textbox value from dropdown using ajax and php
- Automatic Ajax Loading Images With Prototype
- Context Right Click Menu Using Javascript
- Get youtube video screenshot using simple php and javascript
- Simple Pagination Using PHP Script
