JS埋点整理


/*
*www.ijson.com
*网站分析(Web Analytics) 是种网站访客行为的研究。
*使用方式非常简单
*1.在页面上引入Monitor.js
*2.替换baseUrl: 'BehaviorHandler.ashx' 你的异步保存日志的url
*3.后台记录日志,
*4.如果要记录客户操作事件,需要在操作事件中调用
        var post = {};
        post.FuncName = "这是一个点击事件";
        post.p_Remark = "主动请求记录操作";
        post.....具体需要哪些参数,自己定义
        window.upLogger.clickLog(post);
*5.数据记录下了,报表怎么呈现,那就看你的需求了
*/
(function (window, document, undefined) {

    var BehaviorUtil = {//日志记录工具类
        jsName: 'Monitor.js',//程序名称
        defaultVer: 20150629,//版本日期
        getVersion: function () {//获取版本号
            var e = this.jsName;
            var a = new RegExp(e + "(\\?(.*))?$");
            var d = document.getElementsByTagName("script");
            for (var i = 0; i < d.length; i++) {
                var b = d[i];
                if (b.src && b.src.match(a)) {
                    var z = b.src.match(a)[2];
                    if (z && (/^[a-zA-Z0-9]+$/).test(z)) {
                        return z;
                    }
                }
            }
            return this.defaultVer;
        },
        setCookie: function (sName, sValue, oExpires, sPath, sDomain, bSecure) {//设置cookie信息
            var currDate = new Date(),
                sExpires = typeof oExpires == 'undefined' ? '' : ';expires=' + new Date(currDate.getTime() + (oExpires * 24 * 60 * 60 * 1000)).toUTCString();
            document.cookie = sName + '=' + sValue + sExpires + ((sPath == null) ? '' : (' ;path=' + sPath)) + ((sDomain == null) ? '' : (' ;domain=' + sDomain)) + ((bSecure == true) ? ' ; secure' : '');
        },
        getCookie: function (sName) {//获取cookie信息
            var regRes = document.cookie.match(new RegExp("(^| )" + sName + "=([^;]*)(;|$)"));
            return (regRes != null) ? unescape(regRes[2]) : '-';
        },
        getRand: function () {// 生产页面的唯一标示
            var guid = "";
            for (var i = 1; i <= 32; i++) {
                var n = Math.floor(Math.random() * 16.0).toString(16);
                guid += n;
                if ((i == 8) || (i == 12) || (i == 16) || (i == 20))
                    guid += "-";
            }
            return guid;
        },
        parseError: function (obj) {
            var retVal = '';
            for (var key in obj) {
                retVal += key + '=' + obj[key] + ';';
            }
            return retVal;
        },
        getParam: function (obj, flag) {// 参数转化方法
            var retVal = null;
            if (obj) {
                if (BehaviorUtil.isString(obj) || BehaviorUtil.isNumber(obj)) {
                    retVal = obj;
                } else {
                    if (BehaviorUtil.isObject(obj)) {
                        var tmpStr = '';
                        for (var key in obj) {
                            if (obj[key] != null && obj[key] != undefined) {
                                var tmpObj = obj[key];
                                if (BehaviorUtil.isArray(tmpObj)) {
                                    tmpObj = tmpObj.join(',');
                                } else {
                                    if (BehaviorUtil.isDate(tmpObj)) {
                                        tmpObj = tmpObj.getTime();
                                    }
                                }
                                tmpStr += key + '=' + tmpObj + '&';
                            }
                        }
                        tmpStr = tmpStr.substring(0, tmpStr.length - 1);
                        retVal = tmpStr;
                    } else {
                        if (BehaviorUtil.isArray(obj)) {
                            if (BehaviorUtil.length & BehaviorUtil.length > 0) {
                                retVal = obj.join(',');
                            }
                        } else {
                            retVal = obj.toString();
                        }
                    }
                }
            }

            if (!retVal) {
                retVal = '-';
            }

            if (flag) {
                retVal = encodeURIComponent(retVal);
                retVal = this.base64encode(retVal);
            }
            return retVal;
        },
        getDomain: function () {//获取网站的域名
            return document.URL.substring(document.URL.indexOf("://") + 3, document.URL.lastIndexOf("\/"));
        },
        getSessionID: function () {//获取临时会话ID、
            var sessionid = this.getCookie("temporarysessionid");
            if (sessionid == undefined || sessionid == null || sessionid == '' || sessionid == '-') {
                sessionid = this.getRand();
                this.setCookie("temporarysessionid", sessionid);
            }
            return sessionid;
        },
        getProtocol: function () {//请求的协议例如http://
            var reqHeader = location.protocol;
            if ('file:' === reqHeader) {
                reqHeader = 'http:';
            }
            return reqHeader + '//';
        },
        getFloorPage: function () {// 获取落地页面-客户访问的第一个页面
            //落地页cookie  
            var FloorPage = this.getCookie("FloorPage");
            if (FloorPage == undefined || FloorPage == null || FloorPage == '' || FloorPage == '-') {
                FloorPage = this.getCurrentUrl();
                this.setCookie("FloorPage", FloorPage);
            }
            return FloorPage;
        },
        getRefer: function () {// 获取上游页面信息
            var reqRefer = document.referrer;
            reqRefer == location.href && (reqRefer = '');
            try {
                reqRefer = '' == reqRefer ? opener.location : reqRefer;
                reqRefer = '' == reqRefer ? '' : reqRefer;
            } catch (e) {
                reqRefer = '';
            }
            return reqRefer;
        },
        getCurrentUrl: function () {// 获取当前页面信息
            return window.location.href.toString();
        },
        getTitle: function () {// 获取当前页面Title
            return document.title;
        },
        isString: function (obj) {// 判断是不是String类型
            return (obj != null) && (obj != undefined) && (typeof obj == 'string') && (obj.constructor == String);
        },
        isNumber: function (obj) {// 判断是否是数组
            return (typeof obj == 'number') && (obj.constructor == Number);
        },
        isDate: function (obj) {// 判断是否是日期
            return obj && (typeof obj == 'object') && (obj.constructor == Date);
        },
        isArray: function (obj) {//判断是否是数组
            return obj && (typeof obj == 'object') && (obj.constructor == Array);
        },
        isObject: function (obj) {//判断是否是对象
            return obj && (typeof obj == 'object') && (obj.constructor == Object)
        },
        trim: function (str) {// 去除左右两边空格
            return str.replace(/(^\s*)|(\s*$)/, "");;
        }
    },
    beacon_vist_num = isNaN(beacon_vist_num = +BehaviorUtil.getCookie('up_beacon_vist_count')) ? 1 : beacon_vist_num + 1;// 从cookie里获取访问次数
    BehaviorUtil.setCookie('up_beacon_vist_count', beacon_vist_num);//记录新的访问次数

    var setUpBeaconId = function () {
        var sUpBeaconId = BehaviorUtil.trim(BehaviorUtil.getCookie('temporarysessionid'));
        if (sUpBeaconId == undefined || sUpBeaconId == null || sUpBeaconId == '' || sUpBeaconId == '-') {
            BehaviorUtil.setCookie('temporarysessionid', BehaviorUtil.getSessionID());
        }
    }(),
    bLogMethod = {
        tempSessionId: BehaviorUtil.getSessionID(),
        pageTitle: BehaviorUtil.getTitle(),
        currentUrl: BehaviorUtil.getCurrentUrl(),// 
        referUrl: BehaviorUtil.getRefer(),// 
        floorPage: BehaviorUtil.getFloorPage(),
        baseUrl: 'BehaviorHandler.ashx',//记录访问日志的url
        tracking: function () {// 记录访问日志的方法(对外)
            this.beaconLog();
        },
        beaconLog: function () {// 记录访问日志方法
            try {
                var sparam = {};
                sparam.Action = "RecordNormalLog";//执行操作
                this.sendRequest(this.baseUrl, sparam);
            } catch (ex) {
                this.sendError(ex);
            }
        },
        clickLog: function (sparam) {// 记录点击日志
            try {
                sparam.Action = "RecordClickLog";//执行操作
                this.sendRequest(this.baseUrl, sparam);
            } catch (ex) {
                this.sendError(ex);
            }
        },
        sendRequest: function (url, sparam) {// 日志发送方法
            try {
                sparam.p_title = this.pageTitle;//当前页面title
                sparam.p_CurrentUrl = this.currentUrl;//当前页面url
                sparam.p_ReferUrl = this.referUrl;//上一级页面url(来源页面url)
                sparam.p_FloorPage = this.floorPage;//落地页面
                sparam.TempSessionId = this.tempSessionId;//临时会话ID
                this.doPost(url, sparam, true, function (jsonResult) { });
            } catch (e) {
                this.sendError(e);
            }
        },
        sendError: function (ex) {// 发送错误日志
            try {
                var sparam = {};
                sparam.Action = "RecordErrorLog";//执行操作
                sparam.p_Remark = ex.ResponseText;
                this.sendRequest(this.baseUrl, sparam);
            } catch (ex) {
                ;
            }
        },
        doPost: function (url, data, isAsync, callback) {//发送异步请求
            $.ajax({
                url: url,
                data: data,
                type: 'post',
                cache: false,
                dataType: 'json',
                async: isAsync,
                success: callback,
                error: function (error) { document.write(error.responseText); }
            });
        }
    };
    bLogMethod.tracking();
    window.upLogger = bLogMethod;//构建window的upLogger对象
})(window, document);

 

您还没有登录,请先 登录或者 注册后,添加评论