-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
125 lines (99 loc) · 3.67 KB
/
index.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
var firebase = require('firebase/app');
require('firebase/auth');
require('firebase/database');
const FLUSH_INTERVAL = 2000;
/**
* Called every `FLUSH_INTERVAL`ms,
* this sends scroll data back to
* the database.
*/
const getViewport = () => {
let viewPortWidth;
let viewPortHeight;
// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
if (typeof window.innerWidth != 'undefined') {
viewPortWidth = window.innerWidth,
viewPortHeight = window.innerHeight
}
// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
else if (typeof document.documentElement != 'undefined'
&& typeof document.documentElement.clientWidth !=
'undefined' && document.documentElement.clientWidth != 0) {
viewPortWidth = document.documentElement.clientWidth,
viewPortHeight = document.documentElement.clientHeight
}
// older versions of IE
else {
viewPortWidth = document.getElementsByTagName('body')[0].clientWidth,
viewPortHeight = document.getElementsByTagName('body')[0].clientHeight
}
return [viewPortWidth, viewPortHeight];
}
class Analytics {
flushData() {
this.visitRef.child('timeOnPage').set(new Date() - this.startTime);
if (this.scrolls.length) {
this.visitRef.child(`scroll-positions/${this.scrollCount++}`).set(JSON.stringify(this.scrolls));
this.scrolls = [];
}
// if (this.mousePositions.length) {
// this.visitRef.child(`mouse-positions/${this.mouseCount++}`).set(JSON.stringify(this.mousePositions));
// this.mousePositions = [];
// }
}
updateState(newState) {
this.visitRef.child(`state/${this.stateCount++}`).set(JSON.stringify(Object.assign({}, newState, {
timestamp: new Date() - this.startTime
})));
}
onLoad(cb) {
this._onLoad = cb;
}
constructor(projectName, config) {
this.startTime = new Date();
this.scrolls = [];
// this.mousePositions = [];
this.scrollCount = 0;
this.mouseCount = 0;
this.stateCount = 0;
var doc = document.documentElement;
window.addEventListener("scroll", () => {
var t = new Date();
var diff = t - this.startTime;
// var left = (window.pageXOffset || doc.scrollLeft) - (doc.clientLeft || 0);
var top = (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0);
this.scrolls.push([ diff, top ]);
});
var _navigator = {};
for (var i in navigator) _navigator[i] = navigator[i];
delete _navigator.plugins;
delete _navigator.mimeTypes;
delete _navigator.credentials;
delete _navigator.clipboard;
const userDetails = {
location: window.location,
viewport: getViewport(),
navigator: _navigator,
top: (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0)
};
firebase.initializeApp(config);
firebase.auth().signInAnonymously().then(({user}) => {
const userRef = firebase.database().ref(`${projectName}/users/${user.uid}`);
userRef.child('visitCount').once('value').then((value) => {
let v = value && value.toJSON ? value.toJSON() : 0;
let visitCount = v + 1;
userRef.child('visitCount').set(visitCount);
userRef.child('details').set(JSON.stringify(userDetails))
this.visitRef = userRef.child(`visits/${visitCount}`);
// userRef.child(`visits`).set(''+startTime);
this.visitRef.child('details').set(JSON.stringify(userDetails));
this.visitRef.child('startTime').set(+this.startTime);
setInterval(() => {
this.flushData();
}, FLUSH_INTERVAL);
this._onLoad && this._onLoad();
});
});
}
}
module.exports = Analytics;