-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanalytics.js
28 lines (23 loc) · 928 Bytes
/
analytics.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
(function (firebase) {
var analytics = firebase.database().ref();
var activeVisitors = analytics.child('activeVisitors');
var visitor = {
path: window.location.pathname,
arrivedAt: firebase.database.ServerValue.TIMESTAMP,
userAgent: navigator.userAgent
};
var activeVisitorRef = activeVisitors.push();
var visitorId = activeVisitorRef.key;
activeVisitorRef.set(visitor);
activeVisitors.child(visitorId).once('value', function (snapshot) {
visitor.arrivedAt = snapshot.val().arrivedAt;
var pastVisitors = analytics.child('pastVisitors');
visitor.leftAt = firebase.database.ServerValue.TIMESTAMP;
pastVisitors.child(visitorId).onDisconnect().set(visitor);
activeVisitorRef.onDisconnect().remove();
});
var totalVisitors = analytics.child('totalVisitors');
totalVisitors.once('value', function (snapshot) {
totalVisitors.set(snapshot.val() + 1);
});
})(firebase);