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