博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

html:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>记录用户的在线时长</title>
</head>

<body>
</body>
</html>
<script type="text/javascript">
 var Statistics_Website_logo ={
                          'Website_logo_title':'学而思',
                          'Website_logo_Theme':'教育行业',
                          'Website_logo_Company':'好未来'
      };
</script>
<script type="text/javascript" src="js/OnlineTime.js"></script>

OnlineTime.js:

/***
****2015.1.4 img代替ajax心跳 主要计算用户在线时长****
***/

var xue = xue || {};
xue.statistics = xue.statistics || {};
xue.statistics.TimerFunction = null;//定时器函数
xue.statistics.RefreshTime = 5000;//定时器刷新时间
xue.statistics.Domain = document.domain;//域名
xue.statistics.URL = window.location.href;//URL
xue.statistics.Title = document.title;//页面标题
xue.statistics.Resolution = (window.screen.height)+'&&'+(window.screen.width);//分辨率
xue.statistics.ColorDepth = window.screen.colorDepth;//颜色深度
xue.statistics.Referrer = document.referrer;//Referrer
xue.statistics.ClientLanguage = navigator.language;//客户端语言
xue.statistics.AppName = navigator.appName;//客户端浏览器名称
xue.statistics.AppVersion = navigator.appVersion;//客户端浏览器版本号
xue.statistics.Timestamp = new Date().getTime();//页面刷新时间戳
xue.statistics.RefreshTimes = 0;//定时器响应的次数

xue.statistics.ImgAjax = document.createElement("img");
xue.statistics.ImgAjax.id="ImgOnlineTime";
xue.statistics.ImgAjax.style.width = "0";
xue.statistics.ImgAjax.style.height = "0";
document.body.appendChild(xue.statistics.ImgAjax);
/****
***设置cookie和获取cookie***
***/
xue.statistics.getsec = function(str){
    var str1=str.substring(1,str.length)*1;
    var str2=str.substring(0,1);
    if (str2=="s"){
        return str1*1000;
   }else if (str2=="h"){
       return str1*60*60*1000;
   }else if (str2=="d"){
       return str1*24*60*60*1000;
   }
}

xue.statistics.setCookie = function (name,value,time){
    var strsec = this.getsec(time);
    var exp = new Date();
    exp.setTime(exp.getTime() + strsec*1);
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString()+";path=/;domain=.xueersi.com";
}

xue.statistics.getCookie = function(name){
    var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
 
    if(arr=document.cookie.match(reg))
 
        return (arr[2]);
    else
        return null;
}
xue.statistics.randomNum = function(randomNum){
    this.Random="";
    for(var i=0;i<randomNum;i++)
    this.Random+=Math.floor(Math.random()*10);
    return this.Random;
}
//用户自定义的网站标识
xue.statistics.Websitelogo = '';
var Statistics_Website_logo;

try{
    for (var Key in Statistics_Website_logo){
           xue.statistics.Websitelogo =xue.statistics.Websitelogo+'&'+''+Key+'='+Statistics_Website_logo[Key]+'';
        }
}catch(e){
    xue.statistics.Websitelogo = '';
}
/****
***统计页面函数***
***/
xue.statistics.AjaxOnlineTime = function () {
    
    this.RefreshTimes++;
    this.Random = this.randomNum(5) + this.Timestamp;
    
    this.setCookie("xue_stat",this.Random,"d1000000000000000");//设置客户标识
    this.Visitorslogo = this.getCookie('xue_stat');
    
   this.Data = 'http://xeslog.xesv5.com/?Domain='+this.Domain+'&URL='+this.URL+'&Title='+this.Title+'&Resolution='+this.Resolution+'&ColorDepth='+this.ColorDepth+'&Referrer='+this.Referrer+'&ClientLanguage='+this.ClientLanguage+'&AppName='+this.AppName+'&AppVersion='+this.AppVersion+'&Timestamp='+this.Timestamp+'&xue_stat='+this.Visitorslogo+''+this.Websitelogo+'';

   document.getElementById('ImgOnlineTime').src = this.Data;
 
};


window.onload = function(){
 
   // if(navigator.userAgent.indexOf('WebKit')>0){
       xue.statistics.TimerFunction = window.setInterval("xue.statistics.AjaxOnlineTime()", xue.statistics.RefreshTime);//定时器每5s刷新一次
    //}
    
    
    window.onblur=function(){
        window.clearInterval(xue.statistics.TimerFunction);
        window.onfocus=function(){
        //window.clearInterval(xue.statistics.TimerFunction);
        xue.statistics.TimerFunction = window.setInterval("xue.statistics.AjaxOnlineTime()", xue.statistics.RefreshTime);//定时器每5s刷新一次
      };
    };
};