1/* 2* This file is part of the ABDEO package. 3* 4* Copyright (c) ALFA BRAVO <https://alfabravo.pl> 5* 6* For the full copyright and license information, please view the LICENSE 7* file that was distributed with this source code. 8*/ 9 10// dane statystyk 11var hitsData = { 12 apiUrl: window.apphits.apiurl + "hits", 13 key: "", 14 page_view: getPageViewRate(), 15 times: [5, 5, 10, 40, 60], 16 uid: 0 17}; 18 19/** 20 * Po zaÅadowaniu strony 21 * -> tworzÄ nowy rekord; 22 * -> aktualizujÄ wartoÅÄ wyÅwietlenia strony 23 * -> uruchamiam cyklicznÄ aktualizacjÄ danych 24 * {czasu oraz stopnia wyÅwietlania strony} 25 */ 26function hitsLoaded(saveUser = 0, uid = 0, timesStr = '') { 27 if (saveUser) { 28 hitsData.uid = uid; 29 } 30 31 if (timesStr.length > 0) { 32 hitsData.times = timesStr.split(','); 33 } 34 updatePageViewRate(); 35 getUid(); 36 createNewHitStatistics(); 37 runUpdate(); 38} // hitsLoaded 39 40/** 41 * Funkcja rekurencyjna, uruchamiajÄ ca aktualizacjÄ statystyk 42 */ 43function runUpdate() { 44 if (hitsData.times.length > 0) { 45 setTimeout(() => { 46 updateHitStatistics(); 47 runUpdate() 48 }, (hitsData.times.shift() * 1000)); 49 } 50} // runUpdate 51 52/** 53 * Aktualizacja rekordu po zamkniÄciu okna/przejÅciu na innÄ stronÄ 54 */ 55window.addEventListener('pagehide', updateHitStatistics); 56 57/** 58 * Dodaje nowy wpis statystyk odwiedzin 59 */ 60function createNewHitStatistics() { 61 var formData = new FormData(); 62 formData.append('url', window.location.href); 63 formData.append('uid', hitsData.uid); 64 formData.append('page_view', hitsData.page_view); 65 66 // Perform the POST request using fetch 67 fetch(hitsData.apiUrl, { 68 method: 'POST', // Specify the request method 69 headers: { 70 'Accept': 'application/json' 71 }, 72 body: formData, 73 keepalive: true 74 }) 75 .then(response => response.json()) // Parse the JSON response 76 .then(data => { 77 if (data.status === 1 && typeof data.key !== "undefined") { 78 hitsData.key = data.key; // Update the key in hitsData if the condition is met 79 } 80 }) 81 .catch(error => { 82 console.error('Error:', error); // Handle any errors 83 }); 84} // createNewHitStatistics 85 86/** 87 * Aktualizuje wpis statystyk odwiedzin 88 */ 89function updateHitStatistics() { 90 var formData = new FormData(); 91 formData.append('key', hitsData.key); 92 formData.append('page_view', hitsData.page_view); 93 94 if (hitsData.key != 0) { 95 fetch(hitsData.apiUrl, { 96 method: 'PUT', 97 headers: { 98 'Accept': 'application/json' 99 }, 100 body: formData, 101 keepalive: true 102 }) 103 .catch(error => { 104 console.error('Error:', error); 105 }); 106 } 107} // updateHitStatistics 108 109// -------------------- WARTOÅÄ WYÅWIETLENIA STRONY -------------------- 110/** 111 * AktualizujÄ wartoÅÄ wyÅwietlenia strony 112 * w oparciu o pozycje scrolla 113 */ 114window.addEventListener('scroll', function () { 115 updatePageViewRate(); 116}); // .addEventListener 117 118/** 119 * Aktualizuje procentowy stopieÅ wyÅwietlenia strony 120 */ 121var updateDelay = null; 122function updatePageViewRate() { 123 clearTimeout(updateDelay); 124 updateDelay = setTimeout(function() { 125 var viewed = getPageViewRate(); 126 if (viewed > hitsData.page_view) { 127 hitsData.page_view = viewed; 128 } 129 }, 1000); 130} // updatePageViewRate 131 132/** 133 * Zwracam procentowy stopieÅ wyÅwietlenia strony 134 */ 135function getPageViewRate() { 136 const scrollTop = window.scrollY; 137 const innerHeight = window.innerHeight; 138 const documentHeight = document.documentElement.scrollHeight; 139 140 return Math.round((scrollTop + innerHeight) / documentHeight * 100); 141} // getPageViewRate 142 143// -------------------- OBSÅUGA COKKIES -------------------- 144/** 145 * Zwracam klucz odwiedzajÄ cego stronÄ 146 */ 147function getUid() { 148 var htguid = readCookie('auid'); 149 150 if (htguid == null || htguid == "") { 151 htguid = Date.now() + Math.random(); // TODO: nie może byÄ Date.now() 152 } 153 154 createCookie('auid', htguid, 180); 155 156 return htguid; 157} // getUid 158 159// TODO: to co poniżej do application.js? 160 161/** 162 * Odczyt ciasteczka 163 */ 164function readCookie(name) { 165 var nameEQ = name + "="; 166 var ca = document.cookie.split(';'); 167 168 for (var i = 0; i < ca.length; i++) { 169 var c = ca[i];
170 171 while (c.charAt(0) == ' ') { 172 c = c.substring(1, c.length); 173 } 174 175 if (c.indexOf(nameEQ) == 0) { 176 return c.substring(nameEQ.length, c.length); 177 } 178 } 179 180 return null; 181} // readCookie 182 183/** 184 * Zapis ciasteczka 185 */ 186function createCookie(name, value, days) { 187 if (days) { 188 var date = new Date(); 189 date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); 190 var expires = "; expires=" + date.toGMTString(); 191 } 192 else { 193 var expires = ""; 194 } 195 196 document.cookie = name + "=" + value + expires + "; path=/"; 197} // createCookie 198 199// -------------------- TESTY A/B -------------------- 200 201//----------------------------------------------------------------------------- 202//! Funkcja skrótu FNV-1a 32-bit - zwraca liczbÄ caÅkowitÄ z dowolnego ciÄ gu znaków 203function fnv1a32(str) { 204 let h = 0x811c9dc5 205 206 for (let i = 0; i < str.length; i++) { 207 h ^= str.charCodeAt(i) 208 // h *= 16777619 (z 32-bit overflow) 209 h = Math.imul(h, 0x01000193) 210 } 211 212 return h >>> 0 213} // fnv1a32 214 215//----------------------------------------------------------------------------- 216//! Zwraca kubeÅek testu A/B dla podanej wartoÅci i umożliwia rozkÅad na wiÄcej niż 2 opcje 217function abBucket(cookieValue, optionsCount = 2) { 218 const s = String(cookieValue ?? '') 219 const n = fnv1a32(s) 220 221 return n % optionsCount 222} // abBucket 223 224//----------------------------------------------------------------------------- 225//! Zwraca wariant testu A/B dla podanej wartoÅci i umożliwia rozkÅad na wiÄcej niż 2 opcje 226function abVariant(cookieValue, optionsCount = 2) { 227 const b = abBucket(cookieValue, optionsCount) 228 const alphabet = 'abcdefghijklmnopqrstuvwxyz' 229 230 return alphabet[b] ?? String(b) 231} // abVariant 232 233//----------------------------------------------------------------------------- 234//! Zwraca wariant testu A/B dla bieÅ¼Ä cego użytkownika 235function abGetTestVariant() { 236 return abVariant(getUid(), 2) 237} // abGetTestVariant 238 239// eof: hits.js
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.