-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathuserbehavior.js
230 lines (210 loc) · 6.84 KB
/
userbehavior.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
223
224
225
226
227
228
229
230
var userLog = (function(){
//
// Private variables
//
var defaults = {
// Available functionality
clickCount: true,
clickDetails: true,
mouseMovement: true,
context: true,
keyLogger: true,
// Action Item
actionItem: {
processOnAction: false,
selector: '',
event: ''
},
processTime: 15,
processData: function(results){
console.log(results);
},
},
// End results, what is shown to the user
results = {
userInfo: {
appCodeName: navigator.appCodeName || '',
appName: navigator.appName || '',
vendor: navigator.vendor || '',
platform: navigator.platform || '',
userAgent: navigator.userAgent || ''
},
time: {
totalTime: 0,
timeOnPage: 0,
},
clicks: {
clickCount:0,
clickDetails: []
},
mouseMovements: [],
contextChange: [],
keyLogger: [],
},
support = !!document.querySelector && !!document.addEventListener,
settings;
// Helper Functions
var helperActions = {
/**
* Set the timer interval, will increment time on page if the user is
* active on the page, totalTime counter always increments
* @private
*/
timer: function(){
window.setInterval(function(){
if(document['visibilityState'] === 'visible'){
results.time.timeOnPage++;
}
results.time.totalTime++;
},1000);
},
/**
* Detect the X,Y coordinates of the mouse movement
* @private
*/
mouseMovement: function(){
document.addEventListener('mousemove', function(){
results.mouseMovements.push({
timestamp: Date.now(),
x: event.pageX,
y: event.pageY
});
});
},
/**
* Check if the user is navigating to a different page
* @private
*/
contextChange: function(){
document.addEventListener('visibilitychange', function(){
results.contextChange.push({
timestamp: Date.now(),
type: document['visibilityState']
});
});
},
/**
* Log the pasted information and keys pressed
* @private
*/
keyLogger: function(){
document.addEventListener('paste', function(){
var pastedText = undefined;
// Get Pasted Text
if (window.clipboardData && window.clipboardData.getData) {
pastedText = window.clipboardData.getData('Text');
} else if (event.clipboardData && event.clipboardData.getData) {
pastedText = event.clipboardData.getData('text/plain');
}
if(!!pastedText){
results.keyLogger.push({
timestamp: Date.now(),
data: pastedText,
type: 'paste'
});
}
});
document.addEventListener('keyup', function(){
var charCode = event.keyCode || event.which,
charString = String.fromCharCode(charCode);
results.keyLogger.push({
timestamp: Date.now(),
data: charString,
type: 'keypress'
});
});
}
}
/**
* Merge defaults with options
* @private
* @param {Object} default settings
* @param {Object} user options
* @returns {Object} merged object
*/
function getSettings(defaults, options){
var option;
for(option in options){
if(options.hasOwnProperty(option)){
defaults[option] = options[option];
}
}
return defaults;
}
/**
* Initialize the event listeners
* @public
* @param {Object} user options
*/
function init(options){
if(!support) return;
// Extend default options
if (options && typeof options === "object") {
settings = getSettings(defaults, options);
}
document.addEventListener('DOMContentLoaded', function() {
// Countdown Timer
window.setInterval(function(){
if(document['visibilityState'] === 'visible'){
results.time.timeOnPage++;
}
results.time.totalTime++;
// Check if we need to process results
if(settings.processTime > 0 && results.time.totalTime % settings.processTime === 0){
processResults();
}
},1000);
// Click registration, increment click counter and save click time+position
if(settings.clickCount || settings.clickDetails){
document.addEventListener('mouseup', function(){
if(settings.clickCount){
results.clicks.clickCount++;
}
if(settings.clickDetails){
results.clicks.clickDetails.push({
timestamp: Date.now(),
node: event.target.outerHTML,
x: event.pageX,
y: event.pageY
});
}
});
}
// Mouse movements
if(settings.mouseMovement){
helperActions.mouseMovement();
}
// Check context change
if(settings.context){
helperActions.contextChange();
}
// Key Logger
if(settings.keyLogger){
helperActions.keyLogger();
}
// Event Listener to porcess
if(settings.actionItem.processOnAction){
var node = document.querySelector(settings.actionItem.selector);
if(!!!node) throw new Error('Selector was not found.');
node.addEventListener(settings.actionItem.event, function(){
return processResults();
})
}
});
}
/**
* Calls provided function with results as parameter
* @public
*/
function processResults(){
if(settings.hasOwnProperty('processData')){
return settings.processData.call(undefined, results);
}
return false;
}
// Module pattern, only expose necessary methods
return {
init: init,
processResults: processResults,
};
})();