JS远程监测代码V0.1

来源:互联网 发布:淘宝金币怎么提现 编辑:程序博客网 时间:2024/04/27 22:20
/*****************************************************Datavaluable Tracking Core*Version 0.8*Date 2017/3/13*Author Long.Tang *****************************************************/var dtc={};var dtcData = [];(function(){    //减少对象多次检索    var _w=window;    var _d=document;    var _n=navigator;    var func={        _id:function(){            var cid="0";            for(var k in _w){              if(k.indexOf("_cid")>-1){                  cid=k.toString().substring(4);              }            }          return cid;        },        //uuid generate        _uuid:function(len, radix) {            var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');            var uuid = [], i;            radix = radix || chars.length;            if (len) {                for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random()*radix];            } else {                var r;                uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';                uuid[14] = '4';                for (i = 0; i < 36; i++) {                    if (!uuid[i]) {                        r = 0 | Math.random()*16;                        uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];                    }                }            }            return uuid.join('');        },        //获取事件对象的兼容性写法        _getEvent: function(event){            return event ? event : _w.event;        },        //获取事件对象目标的兼容性写法        _getTarget: function(event){            return event.target || event.srcElement;        },        //阻止事件冒泡        _stopPropagation: function(event){            if (event.stopPropagation){                event.stopPropagation();            } else {                event.cancelBubble = true;            }        },        //字符编码        _getCharCode: function(event){            if (typeof event.charCode == "number"){                return event.charCode;            } else {                return event.keyCode;            }        },//取当前日期        _pushTime:function(){            var now=new Date();            var year=now.getFullYear();            var month=now.getMonth()+1;            var day=now.getDate();            var hours=now.getHours();            var minutes=now.getMinutes();            var seconds=now.getSeconds();            return year+"-"+this._formatDate(month)+"-"+this._formatDate(day)+" "+this._formatDate(hours)+":"+this._formatDate(minutes)+":"+this._formatDate(seconds);        },        _formatDate:function(v){            return v.toString().length==1?"0"+v:v;        },        _jsonToStr:function(json){            var str = "";            for(var k in json){                //console.log("key:"+k+" value:"+ json[k]);                str+= ",\"" + k+ "\":\"" +json[k]+"\"";            }            if(str != "")            {                str = str.substring(1);            }            return "{" + str + "}";        },        _integrationData:function(v,type){            var requestData = {};            requestData.dateTime = this._pushTime();            requestData.globalEvent ="3"; //this._id();            requestData.dtc_site_id=this._id();            requestData.dtc_date =this._pushTime();            requestData.dtc_session_id=this._checkCookie("sessionId","");            requestData.dtc_visitor_id=this._formatVal(v.dtc_visitor_id);            requestData.dtc_cookie_id=this._checkCookie("dtcCookieId","");            if(type=="exit"){                requestData.dtc_endtime=this._pushTime();                requestData.dtc_exit_url=_d.location.href;            }else{                requestData.dtc_entrance_url=_d.location.href;                requestData.dtc_begintime=this._pushTime();            }            requestData.dtc_campaign=this._formatVal(v.dtc_campaign);            requestData.dtc_source=this._formatVal(v.dtc_source);            requestData.dtc_channel=this._formatVal(v.dtc_channel);            requestData.dtc_medium=this._formatVal(v.dtc_medium);            requestData.dtc_position=this._formatVal(v.dtc_position);            if(v.event=="click"){                requestData.dtc_click_num=this._formatVal(v.dtc_click_num);            }else{                requestData.dtc_pageviews="1";                requestData.dtc_pageUrlLevel="1";            }            requestData.dtc_page_domain=_d.domain;            requestData.dtc_page_url=_d.location.href;            requestData.dtc_referer_url=_d.referrer;            requestData.dtc_title=_d.title;            requestData.dtc_content=this._formatVal(v.dtc_content);            requestData.dtc_country=this._getPosition("country");            requestData.dtc_region=this._getPosition("province");            requestData.dtc_city=this._getPosition("city");            requestData.dtc_ip=this._getPosition("ip");            requestData.dtc_browser_type=this._myBrowser();            requestData.dtc_browser_version=_n.appVersion;            requestData.dtc_os_type=_n.platform;            requestData.dtc_language=_n.language;            return this._jsonToStr(requestData);        },        _pushData:function(v,type){//动态创建iframe用src 属性实现跨域            var iframe = _d.createElement('iframe');            var data=this._integrationData(v,type);            var str="/";var p="?";            iframe.src="http://192.168.0.126:8092"+str+"sts"+str+"webflume"+p+"logJsonArray"+"=["+data+"]";            iframe.setAttribute('height','0');            iframe.setAttribute('width','0');            iframe.setAttribute('frameborder','0');            _d.body.appendChild(iframe);        },        _bind:function(object,type,fn){            if(object.attachEvent){//IE浏览器                object.attachEvent("on"+type,(function(){                    return function(event){                        _w.event.cancelBubble=true;//停止时间冒泡                        object.attachEvent=[fn.apply(object)];//----这里我要讲的是这里                        //在IE里用attachEvent添加一个时间绑定以后。                        //this的指向不是到object对象本身所以。我们绑定的function里的this.id是无法正常工作的。                        //但是如果我们用fn.apply(object)                        //这里可以看出我们是把apply的第一个对象也就是this的指向变更给了object所以this.id就变成了                        //object.id 可以正常工作了。                    }                })(object),false);            }else if(object.addEventListener){//其他浏览器                object.addEventListener(type,function(event){                    event.stopPropagation();//停止时间冒泡                    fn.apply(this)                });            }        },        _myBrowser:function(){                    var userAgent = _n.userAgent; //取得浏览器的userAgent字符串                    if (-[1,]) {                        var isFF = userAgent.toLowerCase().indexOf("firefox") > -1; //判断是否Firefox浏览器                        if (isFF) {                            return "Firefox";                        }                        var isOpera = userAgent.toLowerCase().indexOf("opera") > -1; //判断是否Opera浏览器                        var isOpera2 = userAgent.toLowerCase().indexOf("opr") > -1; //判断是否Opera浏览器                        if (isOpera||isOpera2) {                            return "Opera";                        }                        var isChrome = userAgent.toLowerCase().indexOf("chrome") > -1; //判断是否Opera浏览器                        if(isChrome){                            return "chrome";                        }                        var isSafari = userAgent.toLowerCase().indexOf("safari") > -1; //判断是否Safari浏览器                        if(isSafari){                            return "Safari";                        }                        return "IE";                    }else{                        return "IE";                    }        },        _getPosition:function(v){                if(v=="ip"){                  try {                      return returnCitySN.cip;                  }catch(e){                      return "";                  }                }                try {                    if (remote_ip_info != null) {                        if (v == "country") {                            return this._reChangeCode(remote_ip_info.country);                        }                        if (v == "province") {                            return this._reChangeCode(remote_ip_info.province);                        }                        if (v == "city") {                            return this._reChangeCode(remote_ip_info.city);                        }                    }                }catch (e){                    return "";                }                return null;        },        _changeCode:function(pValue){            return pValue.replace(/[^\u0000-\u00FF]/g,function($0){return escape($0).replace(/(%u)(\w{4})/gi,"&#x$2;")});        },        _reChangeCode:function(pValue){            return unescape(pValue.replace(/&#x/g,'%u').replace(/\\u/g,'%u').replace(/;/g,''));        },        _createScript:function(url){            var c = _d.createElement('script');            c.type = 'text/javascript';            c.async = true;            c.charset = 'utf-8';            c.src = url;            var h = _d.getElementsByTagName('script')[0];            h.parentNode.insertBefore(c, h);        },        _formatVal:function (val) {            if(val==null||val==""||val==undefined||val=="undefined"){                return "";            }else{                return val;            }        },        _onload:function (fn) {            if (_d.all){                _w.attachEvent("onload",fn)//对于IE            }            else{                _w.addEventListener("load",fn,false);//对于FireFox            }        },        _setCookie:function(keyValue,exdays){            var date = new Date();            date.setTime(date.getTime()+(exdays*24*60*60*1000));            var expires = "expires="+date.toGMTString();            if(typeof keyValue=="string")            _d.cookie = keyValue  + expires;            else            _d.cookie=this._formatObj(keyValue)+expires;        },        _getCookie:function(cname){            var name = cname + "=";            var ca = _d.cookie.split(';');            for(var i=0; i<ca.length; i++)            {                var c = ca[i].trim();                if (c.indexOf(name)==0) return c.substring(name.length,c.length);            }            return "";        },        _checkCookie:function(type,init){            var dtcCookieId=this._getCookie("dtcCookieId");            var sessionId=this._getCookie("sessionId");            if (this._formatVal(dtcCookieId)=="")            {                var putData = {};                dtcCookieId=this._uuid(32,16);                putData.dtcCookieId=dtcCookieId;                sessionId=this._uuid(32,16);                putData.sessionId=sessionId;                this._setCookie(putData,365*10);            }            if(init=="load"){                sessionId=this._uuid(32,16);                this._setCookieValue("sessionId="+sessionId);            }            //取出cookieId            if(type=="dtcCookieId") {                return dtcCookieId;            }            if(type=="sessionId") {                return sessionId;            }        },        _setCookieValue:function(obj){            if(typeof obj =="string")            _d.cookie=obj;            else            _d.cookie=this._formatObj(obj);        },        _deleteCookie:function(name,value){        var date=new Date();        date.setTime(date.getTime()-10000);        _d.cookie=name+"="+value+"; expires="+date.toGMTString();        },        _formatObj:function (obj) {            var val="";            for (var k in obj) {                val += k + "=" + obj[k] + ";";            }            //console.log("=>"+val);            return val;        }    }//异步加载第三方插件func._createScript("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js");func._createScript("http://pv.sohu.com/cityjson");//重写数组 push方法Array.prototype.push = function(v){    if(typeof this.pushListener == 'function')this.pushListener.call(this,v);}dtcData.pushListener = function(v){    //添加新元素    //func._postData("http://192.168.0.16:8080/DTC/?res="+v+"&jsonp=callback");func._pushData(v,"start");}//退出浏览器收集数据_w.onbeforeunload = function () {    func._pushData(123,"exit");    setTimeout(function(){_w.onbeforeunload=null;},1000);};//初始化调用func._onload(function () {    func._checkCookie("dtcCookieId","load");    func._pushData("", "start");});//实现全局方法赋值 dtc.uuid=func._uuid(32,16); dtc.bind=func._bind;})();

0 0