Login   Register  
PHP Classes
elePHPant
Icontem

File: gStats.js

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Cesar D. Rodas  >  gStats  >  gStats.js  >  Download  
File: gStats.js
Role: Auxiliary data
Content type: text/plain
Description: Javascript that gather information
Class: gStats
Keep track of site visits and link clicks
Author: By
Last change: * Adding Ads Tracker
Date: 7 years ago
Size: 6,682 bytes
 

Contents

Class file image Download
<!--
/*
**	Cesar D. Rodas
**	gStats could be downloaded freely from 
**	http://cesars.users.phpclasses.org/gstats
*/
if(typeof(trackURL) == 'undefined') 
	var trackURL = "http://urstats.saddor.com/track.php";
if(typeof(userID) == 'undefined') 
	var userID = "noid";




function gStats_sleep(numberMillis){
	var now=new Date();
	var exitTime=now.getTime()+numberMillis;
	while(true){
		now=new Date();
		if(now.getTime()>exitTime) break;

	}
}

function isTag(tag,elem){
	return elem.nodeName.toUpperCase()==tag.toUpperCase();
}

/*
**	This function call other page and pass values by get.
**	also sleep a few of time
*/
function gStats_callPage(url,sleep) {
	var elem= document.createElement('IMG');
	elem.src=url;
	
	if (sleep) gStats_sleep(900);
}

/*
** Google-Ads and Yahoo! Ads tracker
**
*/
function isGoogleAdsIframe(iframe) {
	return iframe.src.indexOf('googlesyndication.com') > -1;
}
function isYahooAdsIframe(iframe) {
	return iframe.src.indexOf('ypn-js.overture.com') > -1;
}

var iAds = Array();
var isIE = is_ie=document.all?true:false;
var MouseX, MouseY;
function mouse_info(elem) {
	if(!elem) elem = window.event;
	if (elem.pageX || elem.pageY) {
		MouseX = elem.pageX;
		MouseY = elem.pageY;
	} else if ( elem.clientX || elem.clientY) {
		xbody = document.body;
		MouseX = elem.clientX + xbody.scrollLeft + document.documentElement.scrollLeft;
		MouseX = elem.clientY + xbody.scrollTop + document.documentElement.scrollTop;
	}
	
}

function getIframePos(elem) {
	var left, top;
	if (elem.offsetParent) {
		left = elem.offsetLeft;
		top = elem.offsetTop;
		while (elem=elem.offsetParent) {
			left += elem.offsetLeft;
			top += elem.offsetTop;
		}
	}
	return [left,top];
}
function mozilla_adsTrack() {
	try {	
		var mousepos = new Array(MouseX, MouseY);
		
		for(i=0; i < iAds.length; i++) {
			ads = iAds[i];
			adspos = getIframePos( ads[0] );
			valid = true;
			for (e=0; e < 2; e++) {
				if (e==0)
					suma = parseInt(adspos[e]) + parseInt(ads[0].width) +15;
				else 
					suma = parseInt(adspos[e]) + parseInt(ads[0].height) +15; 
				valid = valid && ( mousepos[e] > (adspos[e]-10) && mousepos[e] <  suma );
			} 
			if (valid)  {
				adsTrackIt(ads[0]);
				break;
			}
		}
	} catch (err) {  } 
}

function adsTrackIt(ads) {
	try {
		if (!ads && window.event.srcElement) 
			var ads = window.event.srcElement;
		for(i=0; i < iAds.length; i++) {
			if (iAds[i][0] == ads) {
				var now=new Date();
				var url = escape(document.location.href);
				var adsSize = iAds[i][0].width +'x' + iAds[i][0].height;	
				var newURL= trackURL +'?url=' + url + '&ads=' + escape(iAds[i][2]) + '&adsize=' + adsSize +'&no_cache=' + now.valueOf() + '&usrId=' + userID;
				gStats_callPage(newURL, true);
				break;
			}
		}
	} catch (err) {}
}
function adsTracker() {
	var ads=document.getElementsByTagName('iframe');
	
	/* now nav al_l the ads*/
	for(var i=0;i<ads.length;i++) {
		if( isGoogleAdsIframe(ads[i]) )	{
			iAds[iAds.length] = new Array(ads[i], 'http://pagead2.googlesyndication.com', 'Google AdSense');
			if (isIE) ads[i].onfocus= adsTrackIt;
		} else if  ( isYahooAdsIframe(ads[i]) ) {
			iAds[iAds.length] = new Array(ads[i], 'http://ypn-js.overture.com', 'Yahoo! Ads');
			if (isIE) ads[i].onfocus= adsTrackIt;
		}
		
	}
	if(iAds.length>0 && !isIE){
		gStats_RegisterEvent(window, 'beforeunload', mozilla_adsTrack);
		gStats_RegisterEvent(document, 'mousemove', mouse_info);
	}
}



/*
**	The following two functions register and unregister
**	events callback functions.
*/
function gStats_RegisterEvent(obj, event, action) { 
	if(window.addEventListener){ 
		obj.addEventListener(event, action, false); 
	} else if(window.Event){ 
		obj.attachEvent('on'+event, action); 
	}
}
function gStats_RegisterUnEvent(obj, event, action) {
	if(window.addEventListener){ 
		obj.removeEventListener(event, action, false); 
	} else if(window.attachEvent){ 
		obj.detachEvent('on'+event, action); 
	}
}


/*
**	This function create a IMG html element that send for get the 
**	parameters to the track collector
*/
function gStats_trackIt(url,text){
	try{
		var now=new Date();
		var url = escape(url);
		var text = escape(text);
		var from = escape(document.location.href);
		var newURL= trackURL +'?url='+url+'&text='+text+'&from='+from +'&no_cache='+now.valueOf() + '&usrId=' + userID;
		
		gStats_callPage(newURL,true);
	}catch(err){}
	
}

/*
**
**
*/
function searchClickedTag(obj, flag){
	for(var j=0;j<=5;j++){
		if(flag==1){
			if(obj&&obj.nodeName.toUpperCase()=='A'){
				return obj;
			}
		}
		if(flag==2){
			if(obj&&obj.nodeName.toUpperCase()!='BODY'&&obj.onclick){
				return obj;
			}
		}
		if(!obj.parentNode) {
			return null;
		}
		obj=obj.parentNode;
	}
	return null;
}

function getInfo(elem) {
	var url=null, text=null;
	if(elem){
		elemInfo=searchClickedTag(elem, 1);
		if ( elemInfo && !isTag('A', elem) )
			elem = elemInfo;
		if( isTag('A', elem) ){
			url =  elemInfo.href;
			if(elem.innerHTML) text=elem.innerHTML;
			else if(elem.innerText) text=elem.innerText;
			else if(elem.text) text=elem.text;
		
		}

		if (url || text)
			gStats_trackIt(url,text);
	}
}

/*
**	Sends information to the server about the page-referer
*/
function gStats_sendRefererInfo() {
	try{
		var now = new Date();
		var url = escape(document.location.href);
		var referer = document.referrer;
		
		var useragent = escape(navigator.userAgent.toLowerCase());
		
		if(referer !='' ){
			var referer = '&referer='+escape(referer);
		}else{
			var referer='&referer=direct';
		}
		
		var newURL = trackURL +'?'+ referer + '&url=' + url +'&no_cache='+now.valueOf()+ '&browser=' + useragent + '&usrId=' + userID;
		gStats_callPage(newURL,false);	
	}catch(err){ }
	
}

/*
**	Tracks every click.
**	Useful for know where user click and where he/she go
*/
function onClick_Tracker(elemtClicked) {
	gStats_Tracking(elemtClicked,true);
}

function Context_Tracker(elemtClicked) {
	gStats_Tracking(elemtClicked,false);
}
function gStats_Tracking(elemt) {
	var elem=null;

	if(elemt){
		elem=elemt.target;
	} else { 
		elem=window.event.srcElement;
	}
	getInfo(elem);
}


function gStats_init() {	

	if (document.body) 
		elem = document.body;
	else if (document) 
		elem = document;

	elem.onclick = onClick_Tracker;
	elem.oncontextmenu = Context_Tracker;	

	gStats_sendRefererInfo();
	gStats_RegisterEvent(window, 'load', adsTracker);
}



/*
**	Start tracking :)
*/
gStats_init();
-->