1/**
2 * Statistics script
3 */
4var plugin_statistics = {
5    data: {},
6
7    /**
8     * initialize the script
9     */
10    init: function () {
11
12        // load visitor cookie
13        var now = new Date();
14        var uid = DokuCookie.getValue('plgstats');
15        if (!uid) {
16            uid = now.getTime() + '-' + Math.floor(Math.random() * 32000);
17            DokuCookie.setValue('plgstats', uid);
18        }
19
20        plugin_statistics.data = {
21            uid: uid,
22            ses: plugin_statistics.get_session(),
23            p: JSINFO['id'],
24            r: document.referrer,
25            sx: screen.width,
26            sy: screen.height,
27            vx: window.innerWidth,
28            vy: window.innerHeight,
29            js: 1,
30            rnd: now.getTime()
31        };
32
33        // log access
34        if (JSINFO['act'] == 'show') {
35            plugin_statistics.log_view('v');
36        } else {
37            plugin_statistics.log_view('s');
38        }
39
40        // attach outgoing event
41        jQuery('a.urlextern').click(plugin_statistics.log_external);
42
43        // attach unload event
44        jQuery(window).bind('beforeunload', plugin_statistics.log_exit);
45
46        jQuery('.plg_stats_timeselect .datepicker').datepicker({dateFormat: 'yy-mm-dd'});
47    },
48
49    /**
50     * Log a view or session
51     *
52     * @param {string} act 'v' = view, 's' = session
53     */
54    log_view: function (act) {
55        var params = jQuery.param(plugin_statistics.data);
56        var img = new Image();
57        img.src = DOKU_BASE + 'lib/plugins/statistics/log.php?do=' + act + '&' + params;
58    },
59
60    /**
61     * Log clicks to external URLs
62     */
63    log_external: function () {
64        var params = jQuery.param(plugin_statistics.data);
65        var img = new Image();
66        img.src = DOKU_BASE + 'lib/plugins/statistics/log.php?do=o&ol=' + encodeURIComponent(this.href) + '&' + params;
67        plugin_statistics.pause(500);
68        return true;
69    },
70
71    /**
72     * Log any leaving action as session info
73     */
74    log_exit: function () {
75        var params = jQuery.param(plugin_statistics.data);
76
77        var ses = plugin_statistics.get_session();
78        if(ses != params.ses) return; // session expired a while ago, don't log this anymore
79
80        var url = DOKU_BASE + 'lib/plugins/statistics/log.php?do=s&' + params;
81        jQuery.ajax(url, {async: false});
82    },
83
84    /**
85     * get current session identifier
86     *
87     * Auto clears an expired session and creates a new one after 15 min idle time
88     *
89     * @returns {string}
90     */
91    get_session: function () {
92        var now = new Date();
93
94        // load session cookie
95        var ses = DokuCookie.getValue('plgstatsses');
96        if (ses) {
97            ses = ses.split('-');
98            var time = ses[0];
99            ses = ses[1];
100            if (now.getTime() - time > 15 * 60 * 1000) {
101                ses = ''; // session expired
102            }
103        }
104        // assign new session
105        if (!ses) {
106            //http://stackoverflow.com/a/16693578/172068
107            ses = (Math.random().toString(16) + "000000000").substr(2, 8) +
108                (Math.random().toString(16) + "000000000").substr(2, 8) +
109                (Math.random().toString(16) + "000000000").substr(2, 8) +
110                (Math.random().toString(16) + "000000000").substr(2, 8);
111        }
112        // update session info
113        DokuCookie.setValue('plgstatsses', now.getTime() + '-' + ses);
114
115        return ses;
116    },
117
118
119    /**
120     * Pause the script execution for the given time
121     *
122     * @param {int} ms
123     */
124    pause: function (ms) {
125        var now = new Date();
126        var exitTime = now.getTime() + ms;
127        while (true) {
128            now = new Date();
129            if (now.getTime() > exitTime) {
130                return;
131            }
132        }
133    }
134};
135
136
137jQuery(plugin_statistics.init);
138