1/* globals JSINFO, DOKU_BASE, jQuery */ 2 3/** 4 * Statistics script 5 */ 6const plugin_statistics = { 7 data: {}, 8 9 /** 10 * initialize the script 11 */ 12 init: function () { 13 14 // load visitor cookie 15 const now = new Date(); 16 let uid = DokuCookie.getValue('plgstats'); 17 if (!uid) { 18 uid = now.getTime() + '-' + Math.floor(Math.random() * 32000); 19 DokuCookie.setValue('plgstats', uid); 20 } 21 22 plugin_statistics.data = { 23 uid: uid, 24 ses: plugin_statistics.get_session(), 25 p: JSINFO['id'], 26 r: document.referrer, 27 sx: screen.width, 28 sy: screen.height, 29 vx: window.innerWidth, 30 vy: window.innerHeight, 31 js: 1, 32 rnd: now.getTime() 33 }; 34 35 // log access 36 if (JSINFO['act'] === 'show') { 37 plugin_statistics.log_view('v'); 38 } else { 39 plugin_statistics.log_view('s'); 40 } 41 42 // attach outgoing event 43 jQuery('a.urlextern').click(plugin_statistics.log_external); 44 45 // attach unload event 46 jQuery(window).bind('beforeunload', plugin_statistics.log_exit); 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 const params = jQuery.param(plugin_statistics.data); 56 const 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 const params = jQuery.param(plugin_statistics.data); 65 const url = DOKU_BASE + 'lib/plugins/statistics/log.php?do=o&ol=' + encodeURIComponent(this.href) + '&' + params; 66 navigator.sendBeacon(url); 67 return true; 68 }, 69 70 /** 71 * Log any leaving action as session info 72 */ 73 log_exit: function () { 74 const params = jQuery.param(plugin_statistics.data); 75 76 const ses = plugin_statistics.get_session(); 77 if (ses !== params.ses) return; // session expired a while ago, don't log this anymore 78 79 const url = DOKU_BASE + 'lib/plugins/statistics/log.php?do=s&' + params; 80 navigator.sendBeacon(url); 81 }, 82 83 /** 84 * get current session identifier 85 * 86 * Auto clears an expired session and creates a new one after 15 min idle time 87 * 88 * @returns {string} 89 */ 90 get_session: function () { 91 const now = new Date(); 92 93 // load session cookie 94 let ses = DokuCookie.getValue('plgstatsses'); 95 if (ses) { 96 ses = ses.split('-'); 97 const time = ses[0]; 98 ses = ses[1]; 99 if (now.getTime() - time > 15 * 60 * 1000) { 100 ses = ''; // session expired 101 } 102 } 103 // assign new session 104 if (!ses) { 105 //http://stackoverflow.com/a/16693578/172068 106 ses = (Math.random().toString(16) + "000000000").substr(2, 8) + 107 (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 } 111 // update session info 112 DokuCookie.setValue('plgstatsses', now.getTime() + '-' + ses); 113 114 return ses; 115 }, 116}; 117 118 119jQuery(plugin_statistics.init); 120