2 js 收集pc端website用户浏览数据(uuid等)

来源:互联网 发布:目标软件游戏 编辑:程序博客网 时间:2024/05/29 02:34

cookie 指某些网站为了辨别用户身份、进行session跟踪而储存在用户本地终端上的数据(通常经过加密)。简单点来说就是:浏览器缓存。
参见:http://www.cnblogs.com/foodoir/p/5914631.html

JsSDK设计规则

在js sdk中我们需要收集launch、pageview、chargeRequest和eventDuration四种数据,所以我们需要在js中写入四个方法来分别收集这些数据,另外我们还需要提供一些操作cookie和发送数据的的公用方法。

具体分析参照前面的需求分析章节。

新建maven项目

输入图片说明

设置web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns="http://java.sun.com/xml/ns/javaee"  xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"   id="WebApp_ID" version="2.5"><display-name>NEU_TRACK_JSSDK</display-name>  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>    <welcome-file>index.html</welcome-file>  </welcome-file-list></web-app>

在src/main/webapp下新建jsp文件 index.jsp ,批量替换字符集为 utf-8,测试web服务

<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Insert title here</title></head><body>     启动成功</body></html>

DEBUG:jsp报错

The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path解决方法:在pom.xml中添加配置<dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version><scope>provided</scope></dependency>

JS收集程序的编写

  1. JSSDK CookieUtil对象编写
    实现编写一个CookieUtil工具,用于cookie获取、设置和删除
  2. JSSDK tracker对象编写
    框架:
     var tracker = {                startSession: function(){                     // 加载js就触发的方法,进行判断                }                onPageView: function(){                     // 触发pageview 事件                },                onChargeRequest:function(){                     // 触发ChargeRequest事件                },                onEventDuration: function(){                     // 触发event事件                },                sendDataToServer: function(data){                     // 发送数据data到服务器,其中data是一个Map<String,Object>                }     };

webapp新建js文件夹,创建analytics.js文件
实现:

(function() {    // cookie工具:具体的操作方法 —— 设置、获取、删除    var CookieUtil = {        // get the cookie of the key is name 获取cookie        get : function(name) {            var cookieName = encodeURIComponent(name) + "=", cookieStart = document.cookie                    .indexOf(cookieName), cookieValue = null;            if (cookieStart > -1) {                var cookieEnd = document.cookie.indexOf(";", cookieStart);                if (cookieEnd == -1) {                    cookieEnd = document.cookie.length;                }                cookieValue = decodeURIComponent(document.cookie.substring(                        cookieStart + cookieName.length, cookieEnd));            }            return cookieValue;        },        // set the name/value pair to browser cookie        set : function(name, value, expires, path, domain, secure) {            // 所有键值对name和value进行编码,使用时进行解码            var cookieText = encodeURIComponent(name) + "="                    + encodeURIComponent(value);            // 如果过期            if (expires) {                // set the expires time                var expiresTime = new Date();                expiresTime.setTime(expires);                // 根据GMT时间将Date对象转换为字符串                cookieText += ";expires=" + expiresTime.toGMTString();            }            // 设置cookie路径,默认存储到URL上            if (path) {                cookieText += ";path=" + path;            }            if (domain) {                cookieText += ";domain=" + domain;            }            if (secure) {                cookieText += ";secure";            }            // 通过document.cookie读取域名下的cookie            // 是用分号隔开的键值对构成的字符串,类似于name=aa;age=15            document.cookie = cookieText;        },        setExt : function(name, value) {            this.set(name, value, new Date().getTime() + 315360000000, "/");        }    };    // 主体,其实就是tracker js    var tracker = {        // config        clientConfig : {            serverUrl : "http://192.168.175.110/BfImg.gif",            sessionTimeout : 360, // 360s, 6min 会话时间            maxWaitTime : 3600, // 不跳转的情况下,1个小时内,在同一个页面上的操作算作一个会话            ver : "1"        },        cookieExpiresTime : 315360000000, // cookie过期时间,10年        columns : {            // 发送到服务器的列名称            eventName : "en",            version : "ver",            platform : "pl",            sdk : "sdk",            uuid : "u_ud",            memberId : "u_mid",            sessionId : "u_sd",            clientTime : "c_time",            language : "l",            userAgent : "b_iev",            resolution : "b_rst",            currentUrl : "p_url",            referrerUrl : "p_ref",            title : "tt",            orderId : "oid",            orderName : "on",            currencyAmount : "cua",            currencyType : "cut",            paymentType : "pt",            category : "ca",            action : "ac",            kv : "kv_",            duration : "du"        },        keys : {            pageView : "e_pv",            chargeRequestEvent : "e_crt",            launch : "e_l",            eventDurationEvent : "e_e",            sid : "neutrack_sid",            uuid : "neutrack_uuid",            mid : "neutrack_mid",            preVisitTime : "neutrack_previsit",        },        /**         * 获取会话id         */        getSid : function() {            return CookieUtil.get(this.keys.sid);        },        /**         * 保存会话id到cookie         */        setSid : function(sid) {            if (sid) {                CookieUtil.setExt(this.keys.sid, sid);            }        },        /**         * 获取uuid,从cookie中         */        getUuid : function() {            return CookieUtil.get(this.keys.uuid);        },        /**         * 保存uuid到cookie         */        setUuid : function(uuid) {            if (uuid) {                CookieUtil.setExt(this.keys.uuid, uuid);            }        },        /**         * 获取memberID         */        getMemberId : function() {            return CookieUtil.get(this.keys.mid);        },        /**         * 设置mid         */        setMemberId : function(mid) {            if (mid) {                CookieUtil.setExt(this.keys.mid, mid);            }        },        startSession : function() {            // 加载js就触发的方法            if (this.getSid()) {                // 会话id存在,表示uuid也存在                if (this.isSessionTimeout()) {                    // 会话过期,产生新的会话                    this.createNewSession();                } else {                    // 会话没有过期,更新最近访问时间                    this.updatePreVisitTime(new Date().getTime());                }            } else {                // 会话id不存在,表示uuid也不存在                this.createNewSession();            }            this.onPageView();        },        onLaunch : function() {            // 触发launch事件            var launch = {};            launch[this.columns.eventName] = this.keys.launch; // 设置事件名称            this.setCommonColumns(launch); // 设置公用columns            this.sendDataToServer(this.parseParam(launch)); // 最终发送编码后的数据        },        onPageView : function() {            // 触发page view事件            if (this.preCallApi()) {                var time = new Date().getTime();                var pageviewEvent = {};                pageviewEvent[this.columns.eventName] = this.keys.pageView;                pageviewEvent[this.columns.currentUrl] = window.location.href; // 设置当前url                pageviewEvent[this.columns.referrerUrl] = document.referrer; // 设置前一个页面的url                pageviewEvent[this.columns.title] = document.title; // 设置title                this.setCommonColumns(pageviewEvent); // 设置公用columns                this.sendDataToServer(this.parseParam(pageviewEvent)); // 最终发送编码后的数据ss                this.updatePreVisitTime(time);            }        },        onChargeRequest : function(orderId, name, currencyAmount, currencyType,                paymentType) {            // 触发订单产生事件            if (this.preCallApi()) {                if (!orderId || !currencyType || !paymentType) {                    this.log("订单id、货币类型以及支付方式不能为空");                    return;                }                if (typeof (currencyAmount) == "number") {                    // 金额必须是数字                    var time = new Date().getTime();                    var chargeRequestEvent = {};                    chargeRequestEvent[this.columns.eventName] = this.keys.chargeRequestEvent;                    chargeRequestEvent[this.columns.orderId] = orderId;                    chargeRequestEvent[this.columns.orderName] = name;                    chargeRequestEvent[this.columns.currencyAmount] = currencyAmount;                    chargeRequestEvent[this.columns.currencyType] = currencyType;                    chargeRequestEvent[this.columns.paymentType] = paymentType;                    this.setCommonColumns(chargeRequestEvent); // 设置公用columns                    this.sendDataToServer(this.parseParam(chargeRequestEvent)); // 最终发送编码后的数据ss                    this.updatePreVisitTime(time);                } else {                    this.log("订单金额必须是数字");                    return;                }            }        },        onEventDuration : function(category, action, map, duration) {            // 触发event事件            if (this.preCallApi()) {                if (category && action) {                    var time = new Date().getTime();                    var event = {};                    event[this.columns.eventName] = this.keys.eventDurationEvent;                    event[this.columns.category] = category;                    event[this.columns.action] = action;                    if (map) {                        for ( var k in map) {                            if (k && map[k]) {                                event[this.columns.kv + k] = map[k];                            }                        }                    }                    if (duration) {                        event[this.columns.duration] = duration;                    }                    this.setCommonColumns(event); // 设置公用columns                    this.sendDataToServer(this.parseParam(event)); // 最终发送编码后的数据ss                    this.updatePreVisitTime(time);                } else {                    this.log("category和action不能为空");                }            }        },        /**         * 执行对外方法前必须执行的方法         */        preCallApi : function() {            if (this.isSessionTimeout()) {                // 如果为true,表示需要新建                this.startSession();            } else {                this.updatePreVisitTime(new Date().getTime());            }            return true;        },        sendDataToServer : function(data) {            // 发送数据data到服务器,其中data是一个字符串            var that = this;            var i2 = new Image(1, 1);            i2.onerror = function() {                // 这里可以进行重试操作            };            i2.src = this.clientConfig.serverUrl + "?" + data;        },        /**         * 往data中添加发送到日志收集服务器的公用部分         */        setCommonColumns : function(data) {            data[this.columns.version] = this.clientConfig.ver;            data[this.columns.platform] = "website";            data[this.columns.sdk] = "js";            data[this.columns.uuid] = this.getUuid(); // 设置用户id            data[this.columns.memberId] = this.getMemberId(); // 设置会员id            data[this.columns.sessionId] = this.getSid(); // 设置sid            data[this.columns.clientTime] = new Date().getTime(); // 设置客户端时间            data[this.columns.language] = window.navigator.language; // 设置浏览器语言            data[this.columns.userAgent] = window.navigator.userAgent; // 设置浏览器类型            data[this.columns.resolution] = screen.width + "*" + screen.height; // 设置浏览器分辨率        },        /**         * 创建新的会员,并判断是否是第一次访问页面,如果是,进行launch事件的发送。         */        createNewSession : function() {            var time = new Date().getTime(); // 获取当前操作时间            // 1. 进行会话更新操作            var sid = this.generateId(); // 产生一个session id            this.setSid(sid);            this.updatePreVisitTime(time); // 更新最近访问时间            // 2. 进行uuid查看操作            if (!this.getUuid()) {                // uuid不存在,先创建uuid,然后保存到cookie,最后触发launch事件                var uuid = this.generateId(); // 产品uuid                this.setUuid(uuid);                this.onLaunch();            }        },        /**         * 参数编码返回字符串         */        parseParam : function(data) {            var params = "";            for ( var e in data) {                if (e && data[e]) {                    params += encodeURIComponent(e) + "="                            + encodeURIComponent(data[e]) + "&";                }            }            if (params) {                return params.substring(0, params.length - 1);            } else {                return params;            }        },        /**         * 产生uuid         */        generateId : function() {            var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';            var tmpid = [];            var r;            tmpid[8] = tmpid[13] = tmpid[18] = tmpid[23] = '-';            tmpid[14] = '4';            for (i = 0; i < 36; i++) {                if (!tmpid[i]) {                    r = 0 | Math.random() * 16;                    tmpid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];                }            }            return tmpid.join('');        },        /**         * 判断这个会话是否过期,查看当前时间和最近访问时间间隔时间是否小于this.clientConfig.sessionTimeout<br/>         * 如果是小于,返回false;否则返回true。         */        isSessionTimeout : function() {            var time = new Date().getTime();            var preTime = CookieUtil.get(this.keys.preVisitTime);            if (preTime) {                // 最近访问时间存在,那么进行区间判断                return time - preTime > this.clientConfig.sessionTimeout * 1000;            }            return true;        },        /**         * 更新最近访问时间         */        updatePreVisitTime : function(time) {            CookieUtil.setExt(this.keys.preVisitTime, time);        },        /**         * 打印日志         */        log : function(msg) {            console.log(msg);        },    };    // 对外暴露的方法名称    window.__AE__ = {        startSession : function() {            tracker.startSession();        },        onPageView : function() {            tracker.onPageView();        },        onChargeRequest : function(orderId, name, currencyAmount, currencyType,                paymentType) {            tracker.onChargeRequest(orderId, name, currencyAmount,                    currencyType, paymentType);        },        onEventDuration : function(category, action, map, duration) {            tracker.onEventDuration(category, action, map, duration);        },        setMemberId : function(mid) {            tracker.setMemberId(mid);        }    };    // 自动加载方法    var autoLoad = function() {        // 进行参数设置        var _aelog_ = _aelog_ || window._aelog_ || [];        var memberId = null;        for (i = 0; i < _aelog_.length; i++) {            _aelog_[i][0] === "memberId" && (memberId = _aelog_[i][1]);        }        // 根据是给定memberid,设置memberid的值        memberId && __AE__.setMemberId(memberId);        // 启动session        __AE__.startSession();    };    autoLoad();})();

SDK测试

启动集群上的hdfs+nginx+flume进程,创建web项目加载js,然后发送数据到nginx服务器中,查看最终是否在hdfs中有数据的写入。
命令:
start-dfs.sh: 启动hdfs命令
su root:切换用户
service nginx restart: 启动nginx进程
启动flume进程:
进入flume安装根目录,执行命令:

flume-ng agent --conf ./conf/ --conf-file ./conf/test2.conf --name agent &

web 输入:

http://localhost:8080/neu_track_jssdk/demo4.jsp

结果:F12
输入图片说明

HDFS 目录下文件
输入图片说明

1 0