1/**************************************************************** 2 * piwik/matomo util 3 * matomo 5.3.2 ç°å¢ã§ä½æ 4 * 5 * window._piwik ã®è¨å®ã«å¾ããMatomoã®ãã©ããã³ã°ãè¡ã 6 * ãã°ã¯ "_piwik:xxx" ã§åºåããã 7 * piwik.js, matomo.js ã®èªã¿è¾¼ã¿ã¯å¥ã§è¡ãã㨠8 * window._piwik.pageview(url, title, referrer) ã§åçã«ãã¼ã¸ãã¥ã¼ãéä¿¡ 9 * url, title, referrer ã¯çç¥å¯è½ 10 * window._piwik.event(category, action, name, value) ã§åçã«ã¤ãã³ããéä¿¡ 11 * 12 * windw.Matomo/window.Piwikã䏿¸ããããå ´å(ã客æ§ã«ã¹ã¿ã ï¼ã 13 * eventã¯ãããããpageview㯠<img> ã¿ã°æ¹å¼ã«ãã 14 * 15 * init: 16 * window._piwik = { 17 * 'BASE':'<piwik base url>', 18 * 'trackers':{ 19 * '<idsite1>':null, 20 * '<idsite2>':null, 21 * : 22 * }, 23 * 'pageview':null, 24 * 'event':null, 25 * } 26 * 27 * dynamic pageview: 28 * window._piwik.pageview(url, title, referrer) 29 * 30 * dynamioc event 31 * window._piwik.event(category, action, name, value) 32 * 33 */ 34 35(function(){ 36 37console.log('_piwik:initializing...', window._piwik); 38 39let _initialized = false, _Matomo = undefined, _Matomo_trusted = null; 40 41if (!window._piwik) { console.error('_piwik:matomoInit:window._piwik is not defined'); return; } 42if (window._piwik.BASE.endsWith('/')) { window._piwik.BASE + '/'; } 43 44const MATOMO_BASE = window._piwik.BASE.replace(/\/+$/, '') + '/'; 45const MATOMO_JS_SRC = MATOMO_BASE + 'matomo.js'; 46const MATOMO_PHP = MATOMO_BASE + 'matomo.php'; 47 48function checkMatomoTrusted() { 49 // ãã¹ã¦ã® script ã¿ã°èªã¿è¾¼ã¿ãèªåã§ãããã¨ããã§ã㯠50 if (_Matomo_trusted === false) { return false; } 51 // true or null 52 const matomo_scripts_src = Array.from(document.querySelectorAll('script[src$="matomo.js"], script[src$="piwik.js"]')) 53 .map(s => s.src); 54 const all_script_is_mine = matomo_scripts_src.every(src => src == MATOMO_JS_SRC); 55 if (!all_script_is_mine) { 56 // å¥ã® Matomo/Piwik ã¹ã¯ãªãããèªã¿è¾¼ã¾ãã¦ããå ´å 57 _Matomo_trusted = false; 58 console.warn('_piwik:matomoInit:Matomo/Piwik script overwritten by another script:', matomo_scripts_src); 59 return false; 60 } 61 _Matomo_trusted = true; 62 return true; 63} 64 65function trackPageViewImg(idsite, url, title, referrer) { 66 // img ã¿ã°æ¹å¼ã§ãã¼ã¸ãã¥ã¼ãéä¿¡ 67 console.log('_piwik:trackPageViewImg:', idsite, url, title, referrer); 68 const img = document.createElement('img'); 69 const params = new URLSearchParams({ 70 idsite: idsite, 71 rec: '1', 72 url: url || location.href, 73 action_name: title || document.title, 74 urlref: referrer || document.referrer, 75 rand: Math.random().toString().slice(2) 76 }); 77 img.className = 'img-piwik-tracker'; 78 img.src = MATOMO_PHP + '?' + params.toString(); 79 img.style.display = 'none'; 80 img.width = 1; 81 img.height = 1; 82 img.alt = ''; 83 img.onload = img.onerror = function() { 84 if (img.parentNode) img.parentNode.removeChild(img); 85 }; 86 (document.body || document.head || document.documentElement).appendChild(img); 87} 88 89function matomoInit(){ 90 91 if (_initialized) { 92 // äºéå¼ã³åºãã®é²æ¢ 93 console.warn('_piwik:matomoInit:already initialized:skip', window._piwik); 94 return; 95 } 96 97 _Matomo_trusted = checkMatomoTrusted(); 98 99 try { 100 101 // èªå tracker ã®åæåã¨ãã¼ã¸ãã¥ã¼ã®éä¿¡ 102 Object.keys(window._piwik.trackers).forEach(function(idsite){ 103 if (!_Matomo_trusted) { 104 // window.Matomo ãä¿¡ç¨ã§ããªãå ´åãimg ã¿ã°æ¹å¼ã§ã®ãã©ããã³ã°ãè¡ã 105 console.warn('_piwik:matomoInit:Matomo overwritten, using img tag for tracker idsite=' + idsite); 106 trackPageViewImg(idsite, location.href, document.title, document.referrer); 107 } else { 108 var tracker = window.Matomo.getTracker(window._piwik.BASE +'matomo.php', idsite); 109 this[idsite] = tracker; 110 tracker.trackPageView(); 111 tracker.enableLinkTracking(); // tracker.tarckLink() ãæå¹ã«ãã 112 } 113 }.bind(window._piwik.trackers)); 114 115 // pageview ã®å®ç¾© 116 window._piwik.pageview = function(url, title, referrer) { 117 url = url || location.href; 118 title = title || document.title; 119 referrer = referrer || document.referrer; 120 121 console.log([ 122 '_piwik.pageview:', 123 'url:' + url, 124 'title:' + title.replace('\n', '\n\t\t'), 125 'referrer:' + referrer 126 ].join('\n\t')); 127 128 try { 129 const trackers = window._piwik.trackers; 130 // const base = window._piwik.BASE.replace(/\/+$/, ''); 131 // const matomoUrl = base + '/matomo.php'; 132
133 Object.entries(trackers).forEach(([idsite, tracker]) => { 134 if (!_Matomo_trusted) { 135 // window.Matomo ãä¿¡ç¨ã§ããªãå ´åãimg ã¿ã°æ¹å¼ã§ã®ãã©ããã³ã°ãè¡ã 136 trackPageViewImg(idsite, url, title, referrer); 137 } else if (tracker) { 138 tracker.setCustomUrl(url); 139 tracker.setDocumentTitle(title); 140 tracker.setReferrerUrl(referrer); 141 tracker.trackPageView(); 142 } else { 143 console.warn('_piwik.pageview: tracker not found for idsite=' + idsite); 144 } 145 }); 146 } catch (ex) { 147 console.error('_piwik.pageview:error:', ex); 148 } 149 }; 150 151 // event ã®å®ç¾© 152 // Matomoã䏿¸ããããå ´åãã¤ãã³ãã¯ããããã 153 window._piwik.event = function(category, action, name, value) { 154 console.log([ 155 '_piwik.event:', 156 'category:' + category, 157 'action:' + action, 158 'name:' + name, 159 'value:' + value 160 ].join('\n\t')); 161 if (_Matomo_trusted) { 162 console.warn('_piwik.event: Matomo overwritten, event tracking skipped'); 163 return; 164 } 165 try { 166 const trackers = window._piwik.trackers; 167 Object.entries(trackers).forEach(([idsite, tracker]) => { 168 if (tracker) { 169 tracker.trackEvent(category, action, name, value); 170 } 171 }); 172 } catch (ex) { 173 console.error('_piwik.event:error:', ex); 174 } 175 }; 176 177 _initialized = true; 178 console.log('_piwik:initialized', window._piwik); 179 } catch (ex) { 180 console.error('_piwik:initialize failed:', ex); 181 Object.keys(window._piwik.trackers).forEach(function(idsite){ 182 trackPageViewImg(idsite, location.href, document.title, document.referrer); 183 }); 184 } 185}; 186 187// 注æ: matomoAsyncInit ã¯éå ¬å¼ã ããæ £ä¾çã«ä½¿ããã¦ãã 188if (!window.Matomo) { 189 // ã¾ã ãã¼ãããã¦ããªãå ´åãwindow.matomoAsyncInit ãå®ç¾©ãã¦ãã 190 // ä»è ã«ãã䏿¸ããã§ãã¯ããï¼warningè¡¨ç¤ºï¼ 191 let _internalAsyncInit = matomoInit; 192 Object.defineProperty(window, 'matomoAsyncInit', { 193 configurable: true, 194 enumerable: true, 195 get: function () { 196 return _internalAsyncInit; 197 }, 198 set: function (fn) { 199 console.warn('_piwik:window.matomoAsyncInit overwritten:', fn); 200 _internalAsyncInit = fn; 201 _Matomo_trusted = false; // matomoAsyncInit ã䏿¸ããããããMatomoã¯ä¿¡ç¨ã§ããªã 202 } 203 }); 204 // window.Matomoèªã¿è¾¼ã¿ã®æ¤ç¥ 205 Object.defineProperty(window, 'Matomo', { 206 configurable: true, 207 enumerable: true, 208 get: function () { 209 return _Matomo; 210 }, 211 set: function (val) { 212 console.log( 213 '_piwik:Matomo loaded', 214 document.querySelectorAll('script[src*="matomo.js"],script[src*="piwik.js"]') 215 ); 216 if (_Matomo === undefined) { 217 console.info('_piwik:window.Matomo initialized'); 218 } else { 219 console.warn('window.Matomo overwritten:', val); 220 _Matomo_trusted = false; 221 } 222 _Matomo = val; 223 } 224 }); 225 // èªåã§èªã¿è¾¼ã 226 let d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; 227 g.async=true; g.src=MATOMO_JS_SRC; s.parentNode.insertBefore(g,s); 228 229} else { 230 // Matomoãã¼ãæ¸ã¿ã¨ãããã¨ã¯ãmatomoAsyncInitããã£ãããã§ã«å®è¡ããã¦ããã¨èãããã 231 console.log('_piwik:Matomo already loaded', window.Matomo); 232 matomoInit(); 233} 234console.log('_piwik:initialize done.', window._piwik); 235 236})();
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.