-
Notifications
You must be signed in to change notification settings - Fork 0
/
userBehavingReporter_1.0.0.js
222 lines (211 loc) · 6.94 KB
/
userBehavingReporter_1.0.0.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/**
* Created by tandajun on 16/11/18.
*/
;(function (name, definition) {
// 检测上下文环境是否为AMD或CMD
var hasDefine = typeof define === 'function';
// 检查上下文环境是否为Node
var hasExports = typeof module !== 'undefined' && module.exports;
if (hasDefine) {
define(definition);
} else if (hasExports) {
module.exports = definition();
} else {
// 将模块的执行结果挂在window变量中,在浏览器中this指向window对象
var context = this;
context[name] = definition();
}
})('_Reporter', function () {
/**
* 暴露出去的Actions规范
*/
var Actions = {
CLICK: 'clk',
CHANGE: 'chg',
SELECT: 'slt',
TOUCH: 'tch',
VISIT: 'vst',
BACK: 'bk',
LEAVE: 'lv',
ERROR: 'er',
LOGIN: 'lg',
ADD:'ad',
DELETE:'dl',
UPDATE:'ud',
QUERY:'qr'
};
var ActionTypes = {
CLICK: 'CLICK',
CHANGE: 'CHANGE',
SELECT: 'SELECT',
TOUCH: 'TOUCH',
VISIT: 'VISIT',
BACK: 'BACK',
LEAVE: 'LEAVE',
ERROR: 'ERROR',
LOGIN: 'LOGIN',
ADD: 'ADD',
DELETE: 'DELETE',
UPDATE: 'UPDATE',
QUERY: 'QUERY'
};
/**
* 构造函数
* @param options
*/
var UserBehavingReporter = function (options) {
this.host = options.host || 'http://localhost';
this.port = options.port || '80';
this.userId = options.userId;
this.applicationCode = options.applicationCode;
this.clientHeight = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
this.clientWidth = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
this.behavings = [];
};
/**
* 用于存放用户行为的队列
*/
UserBehavingReporter.prototype.track = function (options) {
var route = options.route;
var action = options.action;
var description = options.description;
var evt = options.evt;
try{
if(!route || route === {}) throw "Parameter cannot be empty : route !";
if(!action in Actions) throw "Parameter cannot be analyzed : action !";
if(!description || description === '') throw "Parameter cannot be analyzed : description !";
}catch(err){
console.log('%c'+err,'color:red');
return null
}
this.behavings.push({
routeCode: route.code,
routePath: route.path,
routeName: route.name,
action: Actions[action],
evt_el: evt ? evt.target.nodeName : '',
evt_x: evt ? evt.x : '',
evt_y: evt ? evt.y : '',
description: options.description,
stayTime: options.stayTime || 0,
recordTime: new Date().getTime()
})
};
/**
* 用于存放用户 点击 行为的队列
*/
UserBehavingReporter.prototype.click = function (route) { //route={code:'',path:'',name:''}
var evt = window.event;
try{
if(!route || route === {}) throw "Parameter cannot be empty : route !";
}catch(err){
console.log('%c'+err,'color:red');
return false
}
this.behavings.push({
routeCode: route.code,
routePath: route.path,
routeName: route.name,
action: Actions.CLICK,
evt_el: evt.target.nodeName,
evt_x: evt.x,
evt_y: evt.y,
recordTime: new Date().getTime()
});
return true;
};
/**
* 用于存放用户 触摸 行为的队列
*/
UserBehavingReporter.prototype.touch = function (route) { //route={code:'',path:'',name:''}
var evt = window.event;
try{
if(!route || route === {}) throw "Parameter cannot be empty : route !";
}catch(err){
console.log('%c'+err,'color:red');
return false
}
this.behavings.push({
routeCode: route.code,
routePath: route.path,
routeName: route.name,
action: Actions.TOUCH,
evt_el: evt.target.nodeName,
evt_x: evt.x,
evt_y: evt.y,
recordTime: new Date().getTime()
})
return true;
};
/**
* 用于存放用户 访问路由 行为的队列
*/
UserBehavingReporter.prototype.visit = function (route) { //route={code:'',path:'',name:''}
try{
if(!route || route === {}) throw "Parameter cannot be empty : route !";
}catch(err){
console.log('%c'+err,'color:red');
return null
}
this.behavings.push({
routeCode: route.code,
routePath: route.path,
routeName: route.name,
action: Actions.VISIT,
recordTime: new Date().getTime()
})
};
/**
* 用于存放用户 离开路由 行为的队列
*/
UserBehavingReporter.prototype.leave = function (route, stayTime) { //route={code:'',path:'',name:''}
try{
if(!route || route === {}) throw "Parameter cannot be empty : route !";
}catch(err){
console.log('%c'+err,'color:red');
return null
}
this.behavings.push({
routeCode: route.code,
routePath: route.path,
routeName: route.name,
action: Actions.LEAVE,
stayTime: stayTime || 0,
recordTime: new Date().getTime()
})
};
/**
* 清空收集的行为队列
*/
UserBehavingReporter.prototype.clear = function () {
this.behavings = [];
};
/**
* 发送 GET 请求。
*/
UserBehavingReporter.prototype.send = function () {
for (var i in this.behavings){
var url = this.host + ':' + this.port + '/log-server/user-behaving/create?u_id='
+ encodeURIComponent(this.userId)
+ '&a_c=' + encodeURIComponent(this.applicationCode)
+ '&r_c=' + encodeURIComponent(this.behavings[i].routeCode)
+ '&r_p=' + encodeURIComponent(this.behavings[i].routePath)
+ '&r_n=' + encodeURIComponent(this.behavings[i].routeName)
+ '&ac=' + encodeURIComponent(this.behavings[i].action)
+ '&el=' + encodeURIComponent(this.behavings[i].evt_el || '')
+ '&e_x=' + encodeURIComponent(this.behavings[i].evt_x || '')
+ '&e_y=' + encodeURIComponent(this.behavings[i].evt_y || '')
+ '&ds=' + encodeURIComponent(this.behavings[i].description || '')
+ '&r_t=' + encodeURIComponent(this.behavings[i].recordTime)
+ '&s_t=' + encodeURIComponent(this.behavings[i].stayTime)
+ '&c_w=' + encodeURIComponent(this.clientWidth)
+ '&c_h=' + encodeURIComponent(this.clientHeight);
(new Image).src = url;
}
this.behavings = [];
};
return {
'UserBehavingReporter' : UserBehavingReporter,
'ActionTypes' : ActionTypes
};
});