网站分析统计的JS源码 - 私人订制版本,可以根据需要来修改

之前公司做了一个分析云平台,用来跟踪收集海量的用户行为的相关数据,供运营人员实时监控网站访问量,统计PV,UV,独立IP,访问时段,访问时长,热点追踪等多类信息,我用JS写了一个小插件,只需要再页面加载这个js文件即可,供有需要的朋友参考,该插件分为两部分组成,一部分是分析的主JS,另一部分功能是用来提供热点地图绘制信息。

/**
 *  分析主JS,该JS部署时放在页面</boday>标签之前,所有引入的其他JS位置之后
 *  该JS文件会自动加载hot.js文件,进行行为追踪,无需配置。
 *  配置说明:
 *  如果追踪页面加载速度,需要在页面标签<head>内靠前位置Javascript语句块中嵌入代码: var _speedMark = new Date(); 如不配置,该功能不可用,这种方式实现比较传统,为的是兼容更多的浏览器,如果只考虑高版本的浏览器,可以用很简单很直接的方式来解决。这里就不累赘讲述了。
 */
/**************************参数说明********************************
 *
 *  bs      浏览器及版本,中间固定用“/”隔开: 如   Firefox/29
 *  fl    Flash版本:如 13.0 r0
 *  hp    是否设置为首页,y表是,否则为空
 *  jv    是否禁用脚本:0表否,1表是
 *  lg    浏览器语言:如 zh-cn
 *  ps      页面加载用时:如 271 单位为毫秒,如没配置该属性,ps没值,网页首次加载有值,其它情况下值为0
 *  pvi      用户标识:如 5699369775 第一次访问页面时产生,只要用户不清除客户端cookie,该值永远存在
 *  rurl  上一个页面地址
 *  scl   屏幕颜色深度:如 24-bit
 *  scr      屏幕分辨率: 如 1600x900
 *  sh      滚动条可滚动高度:如 3259
 *  si      本地一次会话标识: 如 s479879256
 *  tz      区时:如 8
 *  ui      登陆的用户ID,未登陆时没有该值:如 7155113994790596
 *  url      当前的URL
 *  x     鼠标点击事件时,相对body中轴线的x坐标, 中轴线左边为负,右边为正,此值只有开启热点追踪时才会产生,页面初始化时无值
 *  y     鼠标点击事件时的y坐标,此值只有开启热点追踪时才会产生,页面初始化时无值
 *
 **************************参数说明********************************/

/**************************使用方法********************************
*
* 配置方式:
* 在页面</body>标签之前其他外部文件引入之后的位置加入该js引用
*
*    <script type="text/javascript" src ="ea.js" > </script>
*
 **************************使用方法********************************/

; (function () {
    function ea() {
        this.url = [];
        this.init()
    }
    var h, k, j, l, p, r, q, n, m, o,
        f = {};
    ea.prototype = {
        init: function () {
            f ? l = f : l = {};
            h = document;
            k = h.location;
            j = h.body;
            m = navigator;
            p = m.platform;
            q = this.getCookieSetDomain();
        },
        run: function () {
            g.init();
            this.url.push(this.getUrl());
            this.url.unshift("http://180.163.187.8:1080/go.php?");
            this.url.push(this.getRefInfo());
            this.url.push(this.getPvi());
            this.url.push(this.getSi());
            this.url.push(this.getMainEnvInfo());
            this.url.push(this.getExtendEnvInfo());
            this.url.push(this.getBrowserInfo());
            this.url.push(this.getPageSpeed());
            this.url.push(this.getScrollHeight());
            this.url.push(this.getUi());
            g.save();
            this.sendInfo(this.url.join(""));
            //this.loadHotClick(this);
        },
        loadHotClick: function (a) {
            u && u.getScript({
                url: "hot.js",
                callback: function () {
                    (new hot(a)).watchClick();
                }
            })
        },
        inArray: function (a, c) {
            for (i = 0; i < a.length && a[i] != c; i++);
            return i != a.length;
        },
        getUi: function () {
            var a = "",
                b = "";
            a = unescape(g.get("pi=", !0));

if ("-" != a) {
                for (var a = a.split(";"), c = 0; c < a.length; c++) {
                    b = a[c];
                    break;
                }
            }
            return "&ui=" + b;

},
        getUrl: function () {
            return "&url=" + escape(h.URL);
        },
        getRefInfo: function () {
            return "&rurl=" + escape(h.referrer.substr(0, 100));
        },
        getPageSpeed: function () {
            return "undefined" == typeof _speedMark ? "&ps=" : "&ps=" + (new Date - _speedMark);
        },
        getPvi: function () {
            try {
                return m.cookieEnabled ? "&pvi=" + g.setCookie("pgv_pvi", !0) : "&pvi=NoCookie";
            } catch (a) {
                return "&pvi=NoCookie";
            }
        },
        getSi: function () {
            return "&si=" + g.setCookie("ssi");
        },
        getBrowserInfo: function () {
            var a = b.detect();
            return "&os=" + a.os + "&bs=" + a.browser + "/" + a.version;
        },
        getScrollHeight: function () {
            return "&sh=" + Math.max(j.scrollHeight, h.documentElement.scrollHeight);
        },
        getMainEnvInfo: function () {
            var a = "";
            try {
                var c = "-",
                    b = "-",
                    d = "-",
                    e = "-",
                    o = 0;
                self.screen && (c = screen.width + "x" + screen.height, b = (screen.colorDepth || 0) + "-bit");
                m.language ? d = m.language.toLowerCase() : m.browserLanguage && (d = m.browserLanguage.toLowerCase());
                o = m.javaEnabled() ? 1 : 0;
                e = -((new Date).getTimezoneOffset()) / 60;
                a = "&scr=" + c + "&scl=" + b + "&lg=" + d + "&jv=" + o + "&tz=" + e
            } catch (g) { } finally {
                return a
            }
        },
        getExtendEnvInfo: function () {
            var a = "";
            try {
                var c = k.href,
                    b = "",
                    a = a + ("&fl=" + this.getFlashInfo());
                j.addBehavior && (j.addBehavior("#default#homePage"), j.isHomePage(c) && (a += "&hp=y")) || (a += "&hp=");
                j.addBehavior && (j.addBehavior("#default#clientCaps"), b = j.connectionType);
            } catch (d) { } finally {
                return a
            }
        },
        getFlashInfo: function () {
            var a = "-",
                c = navigator;
            try {
                if (c.plugins && c.plugins.length)
                    for (var b = 0; b < c.plugins.length; b++) {
                        if (-1 < c.plugins[b].name.indexOf("Shockwave Flash")) {
                            a = c.plugins[b].description.split("Shockwave Flash ")[1];
                            break
                        }
                    } else if (window.ActiveXObject)
                        for (b = 12; 5 <= b; b--) try {
                            if (eval("new ActiveXObject(‘ShockwaveFlash.ShockwaveFlash." +
                                b + "‘);")) {
                                a = b + ".0";
                                break
                            }
                        } catch (d) { }
            } catch (e) { }
            return a
        },
        getParameter: function (a, c) {
            if (a && c) {
                var b = c.match(RegExp("(\\?|#|&)" + a + "=([^&^#]*)(#|&|$)"));
                return b ? b[2] : ""
            }
            return ""
        },
        getCookieSetDomain: function () {
            var a =
                window.location.host,
                c = {
                    "com.cn": 1,
                    "net.cn": 1,
                    "gov,cn": 1,
                    "com.hk": 1
                },
                b = a.split(".");
            2 < b.length && (a = (c[b.slice(-2).join(".")] ? b.slice(-3) : b.slice(-2)).join("."));
            return a
        },
        sendInfo: function (a) {
            n = new Image(1, 1);
            n.onload = n.onerror = function () {   
                n.onload = n.onerror = null;
                n = null;
            }
            n.src = a;
        }
    };
    var u = {
        config: {
            url: "",
            charset: "utf-8",
            callback: function () { }
        },
        merge: function (a, c) {
            for (var b in c) a[b] = c[b];
            return a
        },
        getScript: function (a) {
            var c;
            this.config = this.merge(this.config, a);
            a = document.createElement("script");
            a.setAttribute("type", "text/javascript");
            a.setAttribute("charset", this.config.charset);
            a.setAttribute("src", this.config.url);
            var b = document.getElementsByTagName("script")[0];
            b.parentNode.insertBefore(a, b);
            c = this.config.callback;
            a.onload = a.onreadystatechange = function () {
                ("undefined" == typeof this.readyState || "loaded" == this.readyState || "complete" == this.readyState) && c()
            }
        }
    },
    d = {
        GetRandomNum: function (a, b) {
            var g = b - a;
            var r = Math.random();
            return (a + Math.round(r * g));
        }
    },
    g = {
        sck: [],
        sco: {},
        init: function () {
            var a = this.get("pgv_info=", !0);
            if ("-" != a) {
                for (var a = a.split("&"), c = 0; c < a.length; c++) {
                    var b = a[c].split("=");
                    this.set(b[0], unescape(b[1]))
                }
            }
        },
        get: function (a, c) {
            var b = c ? h.cookie : this.get("pgv_info=", !0),
                d = "-",
                e;
            e = b.indexOf(a);
            if (-1 < e) {
                e += a.length;
                d = b.indexOf(";", e); -1 == d && (d = b.length);
                if (!c) {
                    var f = b.indexOf("&", e); -1 < f && (d = Math.min(d, f))
                }
                d = unescape(b.substring(e, d))
            }
            return d
        },
        set: function (a, c) {
            this.sco[a] = c;
            for (var b = !1, d = this.sck.length, e = 0; e < d; e++)
                if (a == this.sck[e]) {
                    b = !0;
                    break
                }
            b || this.sck.push(a)
        },
        setCookie: function (a, c) {
            var b = g.get(a + "=", c);
            if ("-" == b) {
                c ? b = "" : b = "s";
                var d = (new Date).getUTCMilliseconds(),
                    b = b + Math.round(2147483647 * Math.abs(Math.random() + 1)) * (d + 1) % 1E10
            }
            c ? this.saveCookie(a + "=" + b, "expires=Sun, 18 Jan 2038 00:00:00 GMT;") : this.set(a, b);
            return b
        },
        save: function () {
            if (l.sessionSpan) {
                var a = new Date;
                a.setTime(a.getTime() + 6E4 * l.sessionSpan)
            }
            for (var c = "", b = this.sck.length, d = 0; d < b; d++) c += this.sck[d] + "=" + this.sco[this.sck[d]] + "&";
            c = "pgv_info=" + c.substr(0, c.length - 1);
            b = "";
            a && (b = "expires=" + a.toGMTString());
            this.saveCookie(c, b)
        },
        saveCookie: function (a, c) {
            h.cookie = a + ";path=/;domain=" + q + ";" + c
        }
    },
    b = {
        detect: function () {
            var ret = {
                browser: this.search(this.data.bs),
                version: this.search(navigator.userAgent),
                os: this.search(this.data.os)
            };
            if (ret.os == ‘Linux‘) {
                var distros = [‘CentOS‘, ‘Debian‘, ‘Fedora‘, ‘Gentoo‘, ‘Mandriva‘, ‘Mageia‘, ‘Red Hat‘, ‘Slackware‘, ‘SUSE‘, ‘Turbolinux‘, ‘Ubuntu‘];
                for (var i = 0; i < distros.length; i++) {
                    if (navigator.userAgent.toLowerCase().match(distros[i].toLowerCase())) {
                        ret.distro = distros[i];
                        break;
                    }
                }
            }
            return ret;
        },
        search: function (data) {
            if (typeof data === "object") {
                for (var i = 0; i < data.length; i++) {
                    var dataString = data[i].string,
                    dataProp = data[i].prop;
                    this.version_string = data[i].versionSearch || data[i].identity;
                    if (dataString) {
                        if (-1 != dataString.indexOf(data[i].subString)) {
                            return data[i].identity;
                        }
                    } else if (dataProp) {
                        return data[i].identity;
                    }
                }
            } else {
                var index = data.indexOf(this.version_string);
                if (index == -1) return;
                return parseFloat(data.substr(index + this.version_string.length + 1));
            }
        },
        data: {
            bs: [
            { string: navigator.userAgent, subString: "Chrome", identity: "Chrome" },
            { string: navigator.userAgent, subString: "OmniWeb", versionSearch: "OmniWeb/", identity: "OmniWeb" },
            { string: navigator.vendor, subString: "Apple", identity: "Safari", versionSearch: "Version" },
            { prop: window.opera, identity: "Opera", versionSearch: "Version" },
            { string: navigator.vendor, subString: "iCab", identity: "iCab" },
            { string: navigator.vendor, subString: "KDE", identity: "Konqueror" },
            { string: navigator.userAgent, subString: "Firefox", identity: "Firefox" },
            { string: navigator.vendor, subString: "Camino", identity: "Camino" },
            { string: navigator.userAgent, subString: "Netscape", identity: "Netscape" },
            { string: navigator.userAgent, subString: "MSIE", identity: "Explorer", versionSearch: "MSIE" },
            { string: navigator.userAgent, subString: "Gecko", identity: "Mozilla", versionSearch: "rv" },
            { string: navigator.userAgent, subString: "Mozilla", identity: "Netscape", versionSearch: "Mozilla" }
            ],
            os: [
            { string: navigator.platform, subString: "Win", identity: "Windows" },
            { string: navigator.platform, subString: "Mac", identity: "Mac" },
            { string: navigator.userAgent, subString: "iPhone", identity: "iPhone/iPod" },
            { string: navigator.userAgent, subString: "iPad", identity: "iPad" },
            { string: navigator.userAgent, subString: "Android", identity: "Android" },
            { string: navigator.platform, subString: "Linux", identity: "Linux" }
            ]
        }
    };
    ea && (new ea).run();
})();

第二部分,热点追踪,主要记录鼠标操作的页面位置信息,通过主jS自动加载,当然需要配置,如果不要记录热点追踪,可以在上面主JS配置不要加载。

以下是hot.js文件的内容

/**
 *  热点跟踪js,将通过ea.js按需加载,目前限制了整个文档跟踪,仅仅跟踪html文档中以下几种标签类型:"A"/"IMG"/"INPUT"/"BUTTON"/"SELECT"/"OBJECT"/"TEXTAREA"/"EMBED"
 */
function hot(a) {
    this.t = a
}
hot.prototype = {
    sendHeat: function (a) {
        var b, d = [],
            q = document.body,
            s = document.documentElement;

if (a.r && 1 == a.r) {
            d.push("&x=" + a.x);
            d.push("&y=" + a.y);
            a = this.t.url.join("") + d.join("");

b = this.t.getParameter("ps", a);
            a = a.replace("ps=" + b, "" == b ? "ps=" : "ps=0");
            a = a.replace("sh=" + b, "sh=" + Math.max(q && q.scrollHeight, s && s.scrollHeight));
            this.t.sendInfo(a);
        }
    },
    getPos: function (a, d, b, c) {
        var b = b || 0,
            c = c || 0,
            d = d || document,
            e = a || window.event,
            t = e.srcElement || e.target,
            r = 0,
            a = {},
            a = e.pageX || e.pageY ? {
                x: e.pageX,
                y: e.pageY
            } : {
                x: e.clientX + Math.max(d.documentElement.scrollLeft, d.body.scrollLeft) - d.body.clientLeft,
                y: e.clientY + Math.max(d.documentElement.scrollTop, d.body.scrollTop) - d.body.clientTop
            };
        a.x += b;
        a.y += c;
        d = Math.max(Math.max(document.body.clientWidth, document.body.offsetWidth), Math.max(document.body.scrollWidth, document.documentElement.scrollWidth)) / 2;
        a.x = a.x - d + window.screen.width / 2 - (Math.max(document.body.scrollHeight, document.documentElement.scrollHeight) > ("undefined" == typeof window.innerHeight ? document.documentElement.clientHeight : window.innerHeight) ? 8.5 : 0);

a.x = a.x > d ? a.x - d : -(d - a.x);

switch (t.tagName) {
            case "A":
            case "IMG":
            case "INPUT":
            case "EMBED":
            case "BUTTON":
            case "SELECT":
            case "OBJECT":
            case "TEXTAREA":
                a.r = 1;
                break;
            default:
                a.r = r;
        }
        return a
    },
    clickHeat: function (a) {
        this.sendHeat(this.getPos(a))
    },
    watchClick: function (a) {
        var d = function (a, b, c) {
            var d = function (a) {
                a = window.event || a;
                target = a.srcElement || a.target;
                c(a, target)
            };
            a.attachEvent ? a.attachEvent("on" + b, d) : a.addEventListener(b, d, !1)
        },
            b = this;
        if (a) b.clickHeat(evt);
        else {
            a = document;
            d(a, "click", function (a) {
                b.sendHeat(b.getPos(a))
            });
            for (var c = a.getElementsByTagName("iframe"), e = 0, a = c.length; e < a; e++) try {
                (function () {
                    var a = c[e],
                        f = a.contentWindow.document;
                    d(f, "click",
                        function (d) {
                            var c = b.getElementPos(a);
                            b.sendHeat(b.getPos(d, f, c.x, c.y))
                        })
                })()
            } catch (f) { }
        }
    },
    getElementPos: function (a) {
        if (null === a.parentNode || "none" == a.style.display) return !1;
        var d = navigator.userAgent.toLowerCase(),
            b = null,
            c = [];
        if (a.getBoundingClientRect) return d = a.getBoundingClientRect(), a = Math.max(document.documentElement.scrollTop, document.body.scrollTop), b = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft), {
            x: d.left + b - document.body.clientLeft,
            y: d.top + a - document.body.clientTop
        };
        if (document.getBoxObjectFor) d = document.getBoxObjectFor(a), c = [d.x - (a.style.borderLeftWidth ? Math.floor(a.style.borderLeftWidth) : 0), d.y - (a.style.borderTopWidth ? Math.floor(a.style.borderTopWidth) : 0)];
        else {
            c = [a.offsetLeft, a.offsetTop];
            b = a.offsetParent;
            if (b != a)
                for (; b;) c[0] += b.offsetLeft, c[1] += b.offsetTop, b = b.offsetParent;
            if (-1 < d.indexOf("opera") || -1 < d.indexOf("safari") && "absolute" == a.style.position) c[0] -= document.body.offsetLeft, c[1] -= document.body.offsetTop
        }
        for (b = a.parentNode ? a.parentNode : null; b && "BODY" !=
            b.tagName && "HTML" != b.tagName;) c[0] -= b.scrollLeft, c[1] -= b.scrollTop, b = b.parentNode ? b.parentNode : null;
        return {
            x: c[0],
            y: c[1]
        }
    }
};
window.hot = hot;

时间: 2024-11-14 02:00:34

网站分析统计的JS源码 - 私人订制版本,可以根据需要来修改的相关文章

jqueryui.position.js源码分析

最近要写前端组件了,狂砍各种组件源码,这里分析一款jqueryui中的posistion插件,注意,它不是jqueryui widget,首先看下源码总体结构图 1.看到$.fn.position 是不是很熟悉?嗯,就是将position方法挂载到原型上,然后控件就可以直接调用了, 2.$.ui.position 这个对象是,用来进行冲突判断的,什么冲突?就是元素与父容器所拥有的空间以及当前可用窗口的控件,默认情形下,如果冲突则采用反转方向的方式显示:对这一点不要惊讶,一切都是为了正常显示而用的

[软件测试]网站压测工具Webbench源码分析

一.我与webbench二三事 Webbench是一个在linux下使用的非常简单的网站压测工具.它使用fork()模拟多个客户端同时访问我们设定的URL,测试网站在压力下工作的性能.Webbench使用C语言编写,下面是其下载链接: http://home.tiscali.cz/~cz210552/webbench.html 说到这里,我赶脚非常有必要给这个网站局部一个截图,如下图: 第一次看到这张图片,着实吃了一精!居然是2004年最后一次更新,我和我的小伙伴们都惊呆了.不过既然现在大家还都

MyVoix2.0.js 源码分析 WebSpeech与WebAudio篇

楔 子 随着移动互联网时代的开启,各种移动设备走进了我们的生活.无论是日常生活中人手一部的手机,还是夜跑者必备的各种智能腕带,亦或者是充满未来科技感的google glass云云,它们正渐渐改变着我们的生活习惯以及用户交互习惯.触摸屏取代了实体按键,Siri开始慢慢释放我们的双手,而leap motion之类的硬件更是让我们彻底不需要接触IT设备便能通过手势控制它们.在这样的大背景下,前端的交互将涉及越来越多元的交叉学科,我们正如十几年前人们经历Css的诞生一样,见证着一场带动整个行业乃至社会的

axios.js 源码分析

axios.js 源码分析 axios.js --> Axios.js --> InterceptorManager.js --> dispatchRequest.js --> transformData.js --> adapters axios 为什么可以在浏览器和服务器上同时运行? 答:因为axios使用了适配器模式,在node中引入http模块请求,在浏览器中使用xhr对象进行请求 axios 如何实现请求和相应的修改 答:通过一个拦截器(InterceptorMan

html5 Sortable.js 源码分析

最近公司项目经常用到一个拖拽 Sortable.js插件,所以有空的时候看了 Sortable.js 源码,总共1300多行这样,写的挺完美的.拖拽的时候主要由这几个事件完成, ondragstart 事件:当拖拽元素开始被拖拽的时候触发的事件,此事件作用在被拖曳元素上    ondragenter 事件:当拖曳元素进入目标元素的时候触发的事件,此事件作用在目标元素上    ondragover 事件:拖拽元素在目标元素上移动的时候触发的事件,此事件作用在目标元素上    ondrop 事件:被

MVVM大比拼之vue.js源码精析

VUE 源码分析 简介 Vue 是 MVVM 框架中的新贵,如果我没记错的话作者应该毕业不久,现在在google.vue 如作者自己所说,在api设计上受到了很多来自knockout.angularjs等大牌框架影响,但作者相信 vue 在性能.易用性方面是有优势.同时也自己做了和其它框架的性能对比,在这里.今天以版本 0.10.4 为准 入口 Vue 的入口也很直白: ? 1 var demo = new Vue({ el: '#demo', data: { message: 'Hello V

underscore.js源码解析

一直想针对一个框架的源码好好的学习一下编程思想和技巧,提高一下自己的水平,但是看过一些框架的源码,都感觉看的莫名其妙,看不太懂,最后找到这个underscore.js由于这个比较简短,一千多行,而且读起来容易一些,所以就决定是它了,那废话不多说开始我们的源码学习. underscore.js源码GitHub地址: https://github.com/jashkenas/underscore/blob/master/underscore.js 本文解析的underscore.js版本是1.8.3

underscore.js源码解析(二)

前几天我对underscore.js的整体结构做了分析,今天我将针对underscore封装的方法进行具体的分析,代码的一些解释都写在了注释里,那么废话不多说进入今天的正文. 没看过上一篇的可以猛戳这里:underscore.js源码解析(一) underscore.js源码GitHub地址: https://github.com/jashkenas/underscore/blob/master/underscore.js 本文解析的underscore.js版本是1.8.3 _.each 1

MVVM架构~knockoutjs系列之从Knockout.Validation.js源码中学习它的用法

说在前 有时,我们在使用一个插件时,在网上即找不到它的相关API,这时,我们会很抓狂的,与其抓狂,还不如踏下心来,分析一下它的源码,事实上,对于JS这种开发语言来说,它开发的插件的使用方法都在它的源码里,只要你踏下心去看,一切就都有了! Knockout.Validation.js是为Knockout插件服务的,它可以为Knockout对象进行验证,就像你使用MVC模型验证 一样,而这种绑定的验证方式对于开发人员来说是很容易接受的,也是一种趋势,它在验证过程中,会将出现异常的点记录下来,然后在