xref: /plugin/statistics/script.js (revision 81ff4c3aae7d39e599ebbfa93da26e631b288d04)
1/**
2 * Statistics script
3 */
4var plugin_statistics = {
5    data: {},
6
7    /**
8     * initialize the script
9     *
10     * @param id string - urlencoded page id
11     */
12    init: function () {
13
14        // load visitor cookie
15        var now = new Date();
16        var uid = DokuCookie.getValue('plgstats');
17        if (!uid) {
18            uid = now.getTime() + '-' + Math.floor(Math.random() * 32000);
19            DokuCookie.setValue('plgstats', uid);
20        }
21        plugin_statistics.data = {
22            uid: uid,
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        var url = DOKU_BASE + 'lib/plugins/statistics/log.php?do=s&' + params;
77        jQuery.ajax(url, {async: false});
78    },
79
80    /**
81     * Pause the script execution for the given time
82     */
83    pause: function (ms) {
84        var now = new Date();
85        var exitTime = now.getTime() + ms;
86        while (true) {
87            now = new Date();
88            if (now.getTime() > exitTime) {
89                return;
90            }
91        }
92    }
93};
94
95
96jQuery(plugin_statistics.init);
97