1({ 2 init: function() { 3 4 /* send the page view request: */ 5 this._onPageView(this._src.replace( this._scriptName, '/pview.php')); 6 7 /* send the first heartbeat signal after x seconds: */ 8 setTimeout(this._onHeartbeat.bind(this, this._src.replace( this._scriptName, '/tick.php')),this._heartbeat * 1000); 9 }, 10 11 /* keep a reference to the script URL: */ 12 _src: document.currentScript.src, 13 14 /* heartbeat signal every x seconds: */ 15 _heartbeat: 30, 16 17 /* name of this script (with slash): */ 18 _scriptName: '/client.js', 19 20 /* function to init page data on server: */ 21 _onPageView: async function(url) { 22 try { 23 /* collect the data to send: */ 24 const visit = { 25 'pg': JSINFO.id, 26 'u': document._botmon.user || null, 27 'lg': navigator.language.substring(0,2), 28 'lt': ( document._botmon ? Date.now() - document._botmon.t0 : null), 29 'id': (document._botmon.session || 'null').replaceAll('\"', ''), 30 'r': document.referrer, 31 'tz': new Date().getTimezoneOffset() /*, 32 'url': window.location.href, 33 'scr': screen.width+':'+screen.height, 34 'l': navigator.languages */ 35 } 36 37 /* compile to a FormData object: */ 38 const data = new FormData(); 39 data.append( "pageview", JSON.stringify( visit ) ); 40 41 /* send the request */ 42 const response = await fetch(url + '?t=' + Date.now(), { 43 method: 'POST', 44 body: data 45 }); 46 if (!response.ok) { 47 throw new Error(response.status + ' ' + response.statusText + ' - ' + url); 48 } 49 } catch (err) { 50 console.error('Error: ', err); 51 } 52 }, 53 54 /* function to call regularly to show the user is still on the page: */ 55 _onHeartbeat: async function(url) { 56 //console.info('botmon_client._onHeartbeat', url); 57 58 let uid = document._botmon.user || null; 59 let sessionId = (document._botmon.session || 'null').replaceAll('\"', '') 60 61 try { 62 const req = '?p=' + encodeURIComponent(JSINFO.id) 63 + '&t=' + encodeURIComponent(Date.now()) 64 + ( sessionId ? '&id=' + encodeURIComponent(sessionId) : '') 65 + ( uid ? '&u=' + encodeURIComponent(uid) : ''); 66 const response = await fetch(url + req, { 67 method: 'HEAD' 68 }); 69 if (!response.ok) { 70 throw new Error(response.status + ' ' + response.statusText + ' - ' + url); 71 } 72 } catch (err) { 73 console.error(err); 74 } finally { 75 /* send the next heartbeat signal after x seconds: */ 76 // setTimeout(this._onHeartbeat.bind(this, this._src.replace( this._scriptName, '/tick.php')),this._heartbeat * 1000); 77 } 78 } 79}).init();