-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdashboard.js
55 lines (47 loc) · 1.72 KB
/
dashboard.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
(function (firebase, $) {
var analytics = firebase.database().ref();
$(document).ready(function () {
var $totalVisitors = $('#total-visitors');
var $activeVisitors = $('#active-visitors');
var $pastVisitors = $('#past-visitors');
analytics.child('totalVisitors').on('value', function (snapshot) {
$totalVisitors.text(snapshot.val());
});
var activeVisitors = analytics.child('activeVisitors');
activeVisitors.on('child_added', function (snapshot) {
var n = snapshot.key;
var v = snapshot.val();
$activeVisitors.prepend(
'<li id="active-visitor' + n + '">' + n + ':' +
'<ul>' +
'<li>Arrived: ' + new Date(v.arrivedAt) + '</li>' +
'<li>Path: ' + v.path + '</li>' +
'<li>User Agent: ' + v.userAgent + '</li>' +
'</ul>' +
'</li>'
);
});
var pastVisitors = analytics.child('pastVisitors').limitToLast(3);
pastVisitors.on('child_added', function (snapshot) {
var n = snapshot.key;
var v = snapshot.val();
$pastVisitors.prepend(
'<li id="past-visitor' + n + '">' + n + ':' +
'<ul>' +
'<li>Arrived: ' + new Date(v.arrivedAt) + '</li>' +
'<li>Left: ' + new Date(v.leftAt) + '</li>' +
'<li>Spent: ' + ((v.leftAt - v.arrivedAt) / 1000) + ' Seconds </li>' +
'<li>Path: ' + v.path + '</li>' +
'<li>User Agent: ' + v.userAgent + '</li>' +
'</ul>' +
'</li>'
);
});
activeVisitors.on('child_removed', function (snapshot) {
$('#active-visitor' + snapshot.key).remove();
});
pastVisitors.on('child_removed', function (snapshot) {
$('#past-visitor' + snapshot.key).remove();
});
});
})(firebase, $);