-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
166 lines (141 loc) · 5.22 KB
/
index.ts
File metadata and controls
166 lines (141 loc) · 5.22 KB
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
import Module from './modules/module';
import TrackTimeBySections from './modules/track-time-by-sections/index';
import TrackTime from './modules/track-time/index';
class Interact {
readonly MIN_DATA_INTERVAL: number = 1000 // milliseconds
readonly INACTIVITY_INTERVAL: number = 2000
ws: WebSocket
lastSentAt: number = performance.now() // In milliseconds
lastInteractedAt: number = performance.now(); // In milliseconds
interactId: string
requestId: string
data: Object = {} // Data to send to websocket
isInActive: boolean = false
sendingForFirstTime: boolean = true
// Event listeners
mouseMoveEventListener: EventListener
modules: Module[] = [
new TrackTime,
new TrackTimeBySections,
]
constructor() {
this.mouseMoveEventListener = () => this.maybeSendData();
}
start(): void {
let interactId = this.getCookie('interactid');
if (!interactId) {
interactId = this.generateUuid();
this.setCookie('interactid', interactId, 90);
}
this.requestId = this.generateUuid();
this.interactId = interactId;
// For now host is the same site script is running on
let host = '';
if (process.env.NODE_ENV === 'production') {
host = `${location.protocol === 'https:' ? 'wss' : 'ws'}://${window.location.host}/app/${process.env.PUSHER_APP_KEY}/v1/client/ws`;
} else {
host = `${location.protocol === 'https:' ? 'wss' : 'ws'}://${window.location.host}:6001/app/${process.env.PUSHER_APP_KEY}/v1/client/ws`;
}
this.ws = new WebSocket(host);
this.ws.onopen = (evt) => this.onOpen(evt);
this.ws.onmessage = (evt) => this.onMessage(evt);
this.loadModules();
document.addEventListener('mousemove', this.mouseMoveEventListener);
document.addEventListener('scroll', this.mouseMoveEventListener);
setInterval(() => this.checkInactivity(), 100);
}
checkInactivity(): void {
if (this.isInActive) return;
const currentTime = performance.now();
if ((currentTime - this.lastInteractedAt) > this.INACTIVITY_INTERVAL) {
this.isInActive = true;
// Set lastSentAt to current time
this.lastInteractedAt = currentTime;
// All modules know that user is inactive
this.modules.forEach(module => {
module.onUserInactive(this.data);
})
}
}
maybeSendData(): void {
if (this.isInActive) {
this.isInActive = false;
this.modules.forEach(module => {
module.onUserReactive(this.data);
})
} else {
this.lastInteractedAt = performance.now();
const currentTime = performance.now();
if ((currentTime - this.lastSentAt) < this.MIN_DATA_INTERVAL) {
return;
}
this.prepareAndSendData();
}
}
generateUuid(): string {
var dt = new Date().getTime();
var uuid = 'xxxxxxxx-xxxx-xxxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = (dt + Math.random()*16)%16 | 0;
dt = Math.floor(dt/16);
return (c=='x' ? r :(r&0x3|0x8)).toString(16);
});
return uuid;
}
loadModules(): void {
this.modules.forEach(module => {
module.start();
})
}
prepareAndSendData(): void {
// Prepare data from all modules
this.modules.forEach(module => {
module.onSendData(this.data);
});
const prependData = {
uuid: this.interactId,
url: window.location.href,
request_uuid: this.requestId,
}
if (this.sendingForFirstTime) {
this.sendingForFirstTime = false;
const { detect } = require('detect-browser');
const browser = detect();
if (browser) {
prependData['browser'] = browser.name;
prependData['os'] = browser.os;
prependData['browser_type'] = browser.type;
prependData['browser_version'] = browser.version;
prependData['referrer'] = document.referrer;
}
}
const payload = {...this.data, ...prependData};
// Finally send data
this.ws.send(JSON.stringify(payload));
this.lastSentAt = performance.now();
}
setCookie(name: string, value: string, days: number) {
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days*24*60*60*1000));
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=/";
}
getCookie(name: string) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
onOpen(evt) {
this.prepareAndSendData();
}
onMessage(evt) {
}
}
new Interact().start();