Detect unique active visitors on your website
To track unique users using your website, you can use the code below. In the example below I am using jQuery to send the HTTP get request. The idea is to send a Ajax get request to record any mouse movement on the webpage. This helps to decrease the amount of bot traffic stats etc.
Just include the small JavaScript on every page and create a server-side page to record the hit. I use a PHP server-side script to insert the IP address and browser info of users visiting my website into a SQL database.
var mousemoved = false;
window.onload = function(){
document.body.onmousemove = function() {
if(!mousemoved){
$.ajax({type: "GET",url: "track.php",timeout: 180000,data: "ajax=true&req=somepage",dataType: "HTML",success: function(data) {},error: function(XMLHttpRequest, textStatus, errorThrown) {}});
}
mousemoved = true;
}
}
A updated version is below, but it may not work in all browsers. So, test before use. Detach the event with null or undefined after the mouse movement. This version works better.
window.onload = function(){
document.body.onmousemove = function() {
$.ajax({type: "GET",url: "track.php",timeout: 180000,data:
"ajax=true&req=somepage",dataType: "HTML",success: function(data)
{},error: function(XMLHttpRequest, textStatus, errorThrown) {}});
document.body.onmousemove = undefined;
}
}
You could also use the built in way to detach an event from the body element. For example:
jpmpopup.prototype.disconnectEvent = function(e,a,func) { (e.removeEventListener) ? e.removeEventListener(a,func,false) : e.detachEvent('on'+a,func);
}
Comments
Post a Comment