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