forked from splitbrain/dokuwiki-plugin-statistics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
137 lines (118 loc) · 3.77 KB
/
script.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
/**
* Statistics script
*/
var plugin_statistics = {
data: {},
/**
* initialize the script
*/
init: function () {
// load visitor cookie
var now = new Date();
var uid = DokuCookie.getValue('plgstats');
if (!uid) {
uid = now.getTime() + '-' + Math.floor(Math.random() * 32000);
DokuCookie.setValue('plgstats', uid);
}
plugin_statistics.data = {
uid: uid,
ses: plugin_statistics.get_session(),
p: JSINFO['id'],
r: document.referrer,
sx: screen.width,
sy: screen.height,
vx: window.innerWidth,
vy: window.innerHeight,
js: 1,
rnd: now.getTime()
};
// log access
if (JSINFO['act'] == 'show') {
plugin_statistics.log_view('v');
} else {
plugin_statistics.log_view('s');
}
// attach outgoing event
jQuery('a.urlextern').click(plugin_statistics.log_external);
// attach unload event
jQuery(window).bind('beforeunload', plugin_statistics.log_exit);
jQuery('.plg_stats_timeselect .datepicker').datepicker({dateFormat: 'yy-mm-dd'});
},
/**
* Log a view or session
*
* @param {string} act 'v' = view, 's' = session
*/
log_view: function (act) {
var params = jQuery.param(plugin_statistics.data);
var img = new Image();
img.src = DOKU_BASE + 'lib/plugins/statistics/log.php?do=' + act + '&' + params;
},
/**
* Log clicks to external URLs
*/
log_external: function () {
var params = jQuery.param(plugin_statistics.data);
var img = new Image();
img.src = DOKU_BASE + 'lib/plugins/statistics/log.php?do=o&ol=' + encodeURIComponent(this.href) + '&' + params;
plugin_statistics.pause(500);
return true;
},
/**
* Log any leaving action as session info
*/
log_exit: function () {
var params = jQuery.param(plugin_statistics.data);
var ses = plugin_statistics.get_session();
if(ses != params.ses) return; // session expired a while ago, don't log this anymore
var url = DOKU_BASE + 'lib/plugins/statistics/log.php?do=s&' + params;
jQuery.ajax(url, {async: false});
},
/**
* get current session identifier
*
* Auto clears an expired session and creates a new one after 15 min idle time
*
* @returns {string}
*/
get_session: function () {
var now = new Date();
// load session cookie
var ses = DokuCookie.getValue('plgstatsses');
if (ses) {
ses = ses.split('-');
var time = ses[0];
ses = ses[1];
if (now.getTime() - time > 15 * 60 * 1000) {
ses = ''; // session expired
}
}
// assign new session
if (!ses) {
//http://stackoverflow.com/a/16693578/172068
ses = (Math.random().toString(16) + "000000000").substr(2, 8) +
(Math.random().toString(16) + "000000000").substr(2, 8) +
(Math.random().toString(16) + "000000000").substr(2, 8) +
(Math.random().toString(16) + "000000000").substr(2, 8);
}
// update session info
DokuCookie.setValue('plgstatsses', now.getTime() + '-' + ses);
return ses;
},
/**
* Pause the script execution for the given time
*
* @param {int} ms
*/
pause: function (ms) {
var now = new Date();
var exitTime = now.getTime() + ms;
while (true) {
now = new Date();
if (now.getTime() > exitTime) {
return;
}
}
}
};
jQuery(plugin_statistics.init);