網站分析統計JS源碼分享

以前公司作了一個分析雲平臺,用來跟蹤收集海量的用戶行爲的相關數據,供運營人員實時監控網站訪問量,統計PV,UV,獨立IP,訪問時段,訪問時長,熱點追蹤等多類信息,我用JS寫了一個小插件,只須要再頁面加載這個js文件便可,供有須要的朋友參考,該插件分爲兩部分組成,一部分是分析的主JS,另外一部分功能是用來提供熱點地圖繪製信息。javascript

/**
 *  分析主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();
})();php

第二部分,熱點追蹤,主要記錄鼠標操做的頁面位置信息,經過主jS自動加載,固然須要配置,若是不要記錄熱點追蹤,能夠在上面主JS配置不要加載。html

如下是hot.js文件的內容java

 

/**
 *  熱點跟蹤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;linux

相關文章
相關標籤/搜索