熱線電話:13121318867

登錄
首頁精彩閱讀用戶行為分析研究之數據采集
用戶行為分析研究之數據采集
2017-12-18
收藏

用戶行為分析研究之數據采集

1.1用戶行為分析的重要性
  用戶行為分析的重要性,我想做個網站的人都會用很清晰的認識,本來我想談談自己想法,但感覺自己畢竟還是做技術的,很難清晰的從商業價值的角度來分析它的重要性,因此放棄了想闡述自己意見的想法。當我第一次見到百度統計,和谷歌分析網站,就有那種驚鴻一瞥的激動,很想自己也能寫出一套這樣的網站,這也是我持續研究用戶行為分析的初衷。
  對于大型網站而言,網站響應速度是網站是否優秀一個重要衡量標準,下面我引用一些權威機構的統計數據來說明網站響應速度的重要性:
  用戶行為分析的前提就是能準確的采集到用戶的相關數據,這就需要我們在網站頁面里添加采集數據的代碼,如果我們的采集代碼寫的不好,一定會對網站的性能產生一定的影響,更有甚者還會影響到網站的穩定性。因此設計一套性能卓越,安全性好,耦合度很低的日志采集程序是非常重要的。
  這里我提供一套采集數據方案,方案詳情如下:
  我是做java的程序員,經常使用到的web應用服務器是tomact,jboss,weblogic等等,我這里為什么不使用這些我非常熟悉的web應用服務器,而去選擇功能相對單一的apache或者是nginx呢?理由非常簡單,因為apache和nginx速度更快,更加輕量級,這個經驗來源于我做網站的經驗,大型網站的服務端設計是很復雜的,但基本都有一個共同的原則:當用戶一個請求提交到了服務端,服務端會先判斷這個請求,如果請求的是一些對靜態資源的訪問(比如圖片,不會變化的文字等),請求會直接提交到響應的靜態資源服務器集群,這樣速度會更快,而這些靜態資源服務器基本都是apache或者是像nginx這樣的輕量級web服務器集群。
1.3    采集系統之服務端
  本地開發,我就不去搭建集群了,有興趣的童鞋可以在網上查查相關的資料。本地開發我就搭建一個apache服務器。
  服務器的開發非常簡單,只要修改下apache下的conf文件(注意:我的開發平臺是window7),代碼如下:
<IfModule log_config_module>
    LogFormat "%h %l %u %t [%{%Y-%m-%d %T}t] \"%r\" [%q] [%U] %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t [%{%Y-%m-%d %T}t] \"%r\" [%q] [%U] %>s %b" common

    <IfModule logio_module>
      # You need to enable mod_logio.c to use %I and %O
      LogFormat "%h %l %u %t [%{%Y-%m-%d %T}t] \"%r\" [%q] [%U] %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    </IfModule>   

在htdocs文件夾里添加如下文件:

1)         a.gif。(1*1像素的透明文件)

2)         click.html。(用于記錄點擊日志)

3)         error.html。(記錄錯誤信息日志)

啟動apache服務器,我們在瀏覽器錄入如下地址:

http://127.0.0.1/a.gif?name=sharpxiajun&msg=test

在logs文件夾里找到2012_06_26.access.log文件,打開文件,我們會看到如下日志:

127.0.0.1 - - [26/Jun/2012:11:37:07 +0800] [2012-06-26 11:37:07] "GET /a.gif?name=sharpxiajun&msg=test HTTP/1.1" [?name=sharpxiajun&msg=test] [/a.gif] 200 43

訪問請求被完整的記錄下來了。
1.4     采集系統之客戶端

  采集系統的核心還是客戶端的采集腳本,這里我會貼出完整的采集腳本以及測試頁面,代碼的詳細解析我會在以后的博客里進行闡述。

  我的采集腳本可以記錄用戶訪問的日志,還能記錄用戶的點擊日志,不過點擊日志一般包含業務含義需要用戶根據自己的需求去定義。代碼如下:

up_beacon.js:
View Code
install_up_beacon.js文件,這個文件對外提供:
(function(window,document,undefined){
    /*安裝采集腳本的js程序*/
    // upLogger對象是采集腳本對外提供的操作對象
    if (window.upLogger){//如果不為空,直接返回,避免重復安裝
        return;
    }
    var cookieUtil = {//cookie操作工具類
        setCookie:function(sName,sValue,oExpires,sPath,sDomain,bSecure){
            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){
            var regRes = document.cookie.match(new RegExp("(^| )" + sName + "=([^;]*)(;|$)"));
            return (regRes != null)?unescape(regRes[2]):'-';
        }        
    };
    var btsVal = cookieUtil.getCookie('b_t_s'),//b_t_s的cookie作用1.標識該頁面是否已經安裝了采集腳本;2.記錄采集腳本的有效期
        startTime = 0,
        intervalTime = 3 * 24 * 60 * 60 * 1000,
        currIntervalTime = new Date().getTime() - 1200000000000,
        domainHead = (document.URL.substring(0,document.URL.indexOf('://'))) + '://';
    if (btsVal != '-' && btsVal.indexOf('t') != -1){
        var getBtsTime = btsVal.substring(btsVal.indexOf('t') + 1,btsVal.indexOf('x'));
            getCurrInterVal = currIntervalTime - getBtsTime;
        if (getCurrInterVal > intervalTime){
            startTime = currIntervalTime;
            cookieUtil.setCookie('b_t_s',btsVal.replace('t' + getBtsTime + 'x', 't' + currIntervalTime + 'x'), 10000, '/');
        }else{
            startTime = getBtsTime;
        }
    }else{
        if (btsVal == '-'){
            cookieUtil.setCookie('b_t_s','t' + currIntervalTime + 'x', 10000, '/');    
        }else{
            cookieUtil.setCookie('b_t_s',btsVal + 't' + currIntervalTime + 'x', 10000, '/');        
        }
        startTime = currIntervalTime;
    }
    document.write('<script src="' + domainHead + '127.0.0.1/up_beacon.js?' + startTime + '"><\/script>');//安裝采集腳本
})(window,document);
下面是測試頁面;

第一個測試頁面:testbeacon.html,代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>beacon test page</title>
</head>
<script type="text/javascript" src="install_up_beacon.js"></script>
<body>
<h1>日志測試</h1>
<input type="button" value="Click Button" id="clickBtn" name="clickBtn" onclick="clickLog('testClickBtn','MyTest')"/>
</body>
</html>
<script type="text/javascript">
// 用戶行為統計代碼
function recordStaticLogerr(authId,type,msg){
    if (window.upLogger){
        upLogger.authId = authId;
        upLogger.clickLog('type=' + type + '&clickTarget=' + msg);    
    }
}

// 記錄click日志的方法
function clickLog(clog_msg,clog_type){
    var clog_authId    = 'sharpxiajun';
    recordStaticLogerr(clog_authId,clog_type,clog_msg);    
}
</script>

第二個測試頁面:parent.html,代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>parent html</title>
</head>

<body>
<a href="testbeacon.html" target="_self">child.html</a>
</body>
</html>

1.5     測試結果

測試地址:

http://localhost/testbeacon.html

http://localhost/parent.html

我們查看cookies信息,如下圖:

日志信息如下:
127.0.0.1 - - [26/Jun/2012:10:01:52 +0800] [2012-06-26 10:01:52] "GET /parent.html HTTP/1.1" [] [/parent.html] 304 -
127.0.0.1 - - [26/Jun/2012:10:01:54 +0800] [2012-06-26 10:01:54] "GET /testbeacon.html HTTP/1.1" [] [/testbeacon.html] 304 -
127.0.0.1 - - [26/Jun/2012:10:01:54 +0800] [2012-06-26 10:01:54] "GET /install_up_beacon.js HTTP/1.1" [] [/install_up_beacon.js] 304 -
127.0.0.1 - - [26/Jun/2012:10:01:54 +0800] [2012-06-26 10:01:54] "GET /up_beacon.js?140675524644 HTTP/1.1" [?140675524644] [/up_beacon.js] 304 -
127.0.0.1 - - [26/Jun/2012:10:01:54 +0800] [2012-06-26 10:01:54] "GET /a.gif?logUrl={/localhost/testbeacon.html}&logHisRefer={http://localhost/parent.html}&logParams={subIsNew=0}&logQuery={pageId=1340676114790-42900296489937289847295051780050&title=beacon%20test%20page&charset=UTF-8&sr=1280*1024}&ver=140675524644&time=1340676114791 HTTP/1.1" [?logUrl={/localhost/testbeacon.html}&logHisRefer={http://localhost/parent.html}&logParams={subIsNew=0}&logQuery={pageId=1340676114790-42900296489937289847295051780050&title=beacon%20test%20page&charset=UTF-8&sr=1280*1024}&ver=140675524644&time=1340676114791] [/a.gif] 200 43
127.0.0.1 - - [26/Jun/2012:10:02:01 +0800] [2012-06-26 10:02:01] "GET /click.html?type=MyTest&clickTarget=testClickBtn&pageId=1340676114790-42900296489937289847295051780050&authId=sharpxiajun&ver=140675524644&time=1340676121252 HTTP/1.1" [?type=MyTest&clickTarget=testClickBtn&pageId=1340676114790-42900296489937289847295051780050&authId=sharpxiajun&ver=140675524644&time=1340676121252] [/click.html] 200 310
大家看到了吧,請求都被記錄下來,下面我們只要好好分析這些日志文件的信息就行了。

數據分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數據分析師資訊
更多

OK
客服在線
立即咨詢
日韩人妻系列无码专区视频,先锋高清无码,无码免费视欧非,国精产品一区一区三区无码
客服在線
立即咨詢