xref: /plugin/statistics/script.js (revision 047fcb0f7b78224e30f1c86ced663810afacb301)
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        console.log('ook');
48    },
49
50    /**
51     * Log a view or session
52     *
53     * @param string act 'v' = view, 's' = session
54     */
55    log_view: function (act) {
56        var params = jQuery.param(plugin_statistics.data);
57        var img = new Image();
58        img.src = DOKU_BASE + 'lib/plugins/statistics/log.php?do=' + act + '&' + params;
59    },
60
61    /**
62     * Log clicks to external URLs
63     */
64    log_external: function () {
65        var params = jQuery.param(plugin_statistics.data);
66        var img = new Image();
67        img.src = DOKU_BASE + 'lib/plugins/statistics/log.php?do=o&ol=' + encodeURIComponent(this.href) + '&' + params;
68        plugin_statistics.pause(500);
69        return true;
70    },
71
72    /**
73     * Log any leaving action as session info
74     */
75    log_exit: function () {
76        var params = jQuery.param(plugin_statistics.data);
77        var url = DOKU_BASE + 'lib/plugins/statistics/log.php?do=s&' + params;
78        jQuery.ajax(url, {async: false});
79    },
80
81    /**
82     * Pause the script execution for the given time
83     */
84    pause: function (ms) {
85        var now = new Date();
86        var exitTime = now.getTime() + ms;
87        while (true) {
88            now = new Date();
89            if (now.getTime() > exitTime) {
90                return;
91            }
92        }
93    }
94};
95
96
97jQuery(plugin_statistics.init);
98