|
| 1 | +// GoatCounter: https://www.goatcounter.com |
| 2 | +// This file (and *only* this file) is released under the ISC license: |
| 3 | +// https://opensource.org/licenses/ISC |
| 4 | +;(function() { |
| 5 | + 'use strict'; |
| 6 | + |
| 7 | + if (window.goatcounter && window.goatcounter.vars) // Compatibility with very old version; do not use. |
| 8 | + window.goatcounter = window.goatcounter.vars |
| 9 | + else |
| 10 | + window.goatcounter = window.goatcounter || {} |
| 11 | + |
| 12 | + // Load settings from data-goatcounter-settings. |
| 13 | + var s = document.querySelector('script[data-goatcounter]') |
| 14 | + if (s && s.dataset.goatcounterSettings) { |
| 15 | + try { var set = JSON.parse(s.dataset.goatcounterSettings) } |
| 16 | + catch (err) { console.error('invalid JSON in data-goatcounter-settings: ' + err) } |
| 17 | + for (var k in set) |
| 18 | + if (['no_onload', 'no_events', 'allow_local', 'allow_frame', 'path', 'title', 'referrer', 'event'].indexOf(k) > -1) |
| 19 | + window.goatcounter[k] = set[k] |
| 20 | + } |
| 21 | + |
| 22 | + var enc = encodeURIComponent |
| 23 | + |
| 24 | + // Get all data we're going to send off to the counter endpoint. |
| 25 | + var get_data = function(vars) { |
| 26 | + var data = { |
| 27 | + p: (vars.path === undefined ? goatcounter.path : vars.path), |
| 28 | + r: (vars.referrer === undefined ? goatcounter.referrer : vars.referrer), |
| 29 | + t: (vars.title === undefined ? goatcounter.title : vars.title), |
| 30 | + e: !!(vars.event || goatcounter.event), |
| 31 | + s: [window.screen.width, window.screen.height, (window.devicePixelRatio || 1)], |
| 32 | + b: is_bot(), |
| 33 | + q: location.search, |
| 34 | + } |
| 35 | + |
| 36 | + var rcb, pcb, tcb // Save callbacks to apply later. |
| 37 | + if (typeof(data.r) === 'function') rcb = data.r |
| 38 | + if (typeof(data.t) === 'function') tcb = data.t |
| 39 | + if (typeof(data.p) === 'function') pcb = data.p |
| 40 | + |
| 41 | + if (is_empty(data.r)) data.r = document.referrer |
| 42 | + if (is_empty(data.t)) data.t = document.title |
| 43 | + if (is_empty(data.p)) data.p = get_path() |
| 44 | + |
| 45 | + if (rcb) data.r = rcb(data.r) |
| 46 | + if (tcb) data.t = tcb(data.t) |
| 47 | + if (pcb) data.p = pcb(data.p) |
| 48 | + return data |
| 49 | + } |
| 50 | + |
| 51 | + // Check if a value is "empty" for the purpose of get_data(). |
| 52 | + var is_empty = function(v) { return v === null || v === undefined || typeof(v) === 'function' } |
| 53 | + |
| 54 | + // See if this looks like a bot; there is some additional filtering on the |
| 55 | + // backend, but these properties can't be fetched from there. |
| 56 | + var is_bot = function() { |
| 57 | + // Headless browsers are probably a bot. |
| 58 | + var w = window, d = document |
| 59 | + if (w.callPhantom || w._phantom || w.phantom) |
| 60 | + return 150 |
| 61 | + if (w.__nightmare) |
| 62 | + return 151 |
| 63 | + if (d.__selenium_unwrapped || d.__webdriver_evaluate || d.__driver_evaluate) |
| 64 | + return 152 |
| 65 | + if (navigator.webdriver) |
| 66 | + return 153 |
| 67 | + return 0 |
| 68 | + } |
| 69 | + |
| 70 | + // Object to urlencoded string, starting with a ?. |
| 71 | + var urlencode = function(obj) { |
| 72 | + var p = [] |
| 73 | + for (var k in obj) |
| 74 | + if (obj[k] !== '' && obj[k] !== null && obj[k] !== undefined && obj[k] !== false) |
| 75 | + p.push(enc(k) + '=' + enc(obj[k])) |
| 76 | + return '?' + p.join('&') |
| 77 | + } |
| 78 | + |
| 79 | + // Show a warning in the console. |
| 80 | + var warn = function(msg) { |
| 81 | + if (console && 'warn' in console) |
| 82 | + console.warn('goatcounter: ' + msg) |
| 83 | + } |
| 84 | + |
| 85 | + // Get the endpoint to send requests to. |
| 86 | + var get_endpoint = function() { |
| 87 | + var s = document.querySelector('script[data-goatcounter]') |
| 88 | + if (s && s.dataset.goatcounter) |
| 89 | + return s.dataset.goatcounter |
| 90 | + return (goatcounter.endpoint || window.counter) // counter is for compat; don't use. |
| 91 | + } |
| 92 | + |
| 93 | + // Get current path. |
| 94 | + var get_path = function() { |
| 95 | + var loc = location, |
| 96 | + c = document.querySelector('link[rel="canonical"][href]') |
| 97 | + if (c) { // May be relative or point to different domain. |
| 98 | + var a = document.createElement('a') |
| 99 | + a.href = c.href |
| 100 | + if (a.hostname.replace(/^www\./, '') === location.hostname.replace(/^www\./, '')) |
| 101 | + loc = a |
| 102 | + } |
| 103 | + return (loc.pathname + loc.search) || '/' |
| 104 | + } |
| 105 | + |
| 106 | + // Run function after DOM is loaded. |
| 107 | + var on_load = function(f) { |
| 108 | + if (document.body === null) |
| 109 | + document.addEventListener('DOMContentLoaded', function() { f() }, false) |
| 110 | + else |
| 111 | + f() |
| 112 | + } |
| 113 | + |
| 114 | + // Filter some requests that we (probably) don't want to count. |
| 115 | + goatcounter.filter = function() { |
| 116 | + if ('visibilityState' in document && document.visibilityState === 'prerender') |
| 117 | + return 'visibilityState' |
| 118 | + if (!goatcounter.allow_frame && location !== parent.location) |
| 119 | + return 'frame' |
| 120 | + if (!goatcounter.allow_local && location.hostname.match(/(localhost$|^127\.|^10\.|^172\.(1[6-9]|2[0-9]|3[0-1])\.|^192\.168\.|^0\.0\.0\.0$)/)) |
| 121 | + return 'localhost' |
| 122 | + if (!goatcounter.allow_local && location.protocol === 'file:') |
| 123 | + return 'localfile' |
| 124 | + if (localStorage && localStorage.getItem('skipgc') === 't') |
| 125 | + return 'disabled with #toggle-goatcounter' |
| 126 | + return false |
| 127 | + } |
| 128 | + |
| 129 | + // Get URL to send to GoatCounter. |
| 130 | + window.goatcounter.url = function(vars) { |
| 131 | + var data = get_data(vars || {}) |
| 132 | + if (data.p === null) // null from user callback. |
| 133 | + return |
| 134 | + data.rnd = Math.random().toString(36).substr(2, 5) // Browsers don't always listen to Cache-Control. |
| 135 | + |
| 136 | + var endpoint = get_endpoint() |
| 137 | + if (!endpoint) |
| 138 | + return warn('no endpoint found') |
| 139 | + |
| 140 | + return endpoint + urlencode(data) |
| 141 | + } |
| 142 | + |
| 143 | + // Count a hit. |
| 144 | + window.goatcounter.count = function(vars) { |
| 145 | + var f = goatcounter.filter() |
| 146 | + if (f) |
| 147 | + return warn('not counting because of: ' + f) |
| 148 | + |
| 149 | + var url = goatcounter.url(vars) |
| 150 | + if (!url) |
| 151 | + return warn('not counting because path callback returned null') |
| 152 | + |
| 153 | + if (navigator.sendBeacon) |
| 154 | + navigator.sendBeacon(url) |
| 155 | + else { // Fallback for (very) old browsers. |
| 156 | + var img = document.createElement('img') |
| 157 | + img.src = url |
| 158 | + img.style.position = 'absolute' // Affect layout less. |
| 159 | + img.style.bottom = '0px' |
| 160 | + img.style.width = '1px' |
| 161 | + img.style.height = '1px' |
| 162 | + img.loading = 'eager' |
| 163 | + img.setAttribute('alt', '') |
| 164 | + img.setAttribute('aria-hidden', 'true') |
| 165 | + |
| 166 | + var rm = function() { if (img && img.parentNode) img.parentNode.removeChild(img) } |
| 167 | + img.addEventListener('load', rm, false) |
| 168 | + document.body.appendChild(img) |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + // Get a query parameter. |
| 173 | + window.goatcounter.get_query = function(name) { |
| 174 | + var s = location.search.substr(1).split('&') |
| 175 | + for (var i = 0; i < s.length; i++) |
| 176 | + if (s[i].toLowerCase().indexOf(name.toLowerCase() + '=') === 0) |
| 177 | + return s[i].substr(name.length + 1) |
| 178 | + } |
| 179 | + |
| 180 | + // Track click events. |
| 181 | + window.goatcounter.bind_events = function() { |
| 182 | + if (!document.querySelectorAll) // Just in case someone uses an ancient browser. |
| 183 | + return |
| 184 | + |
| 185 | + var send = function(elem) { |
| 186 | + return function() { |
| 187 | + goatcounter.count({ |
| 188 | + event: true, |
| 189 | + path: (elem.dataset.goatcounterClick || elem.name || elem.id || ''), |
| 190 | + title: (elem.dataset.goatcounterTitle || elem.title || (elem.innerHTML || '').substr(0, 200) || ''), |
| 191 | + referrer: (elem.dataset.goatcounterReferrer || elem.dataset.goatcounterReferral || ''), |
| 192 | + }) |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + Array.prototype.slice.call(document.querySelectorAll("*[data-goatcounter-click]")).forEach(function(elem) { |
| 197 | + if (elem.dataset.goatcounterBound) |
| 198 | + return |
| 199 | + var f = send(elem) |
| 200 | + elem.addEventListener('click', f, false) |
| 201 | + elem.addEventListener('auxclick', f, false) // Middle click. |
| 202 | + elem.dataset.goatcounterBound = 'true' |
| 203 | + }) |
| 204 | + } |
| 205 | + |
| 206 | + // Add a "visitor counter" frame or image. |
| 207 | + window.goatcounter.visit_count = function(opt) { |
| 208 | + on_load(function() { |
| 209 | + opt = opt || {} |
| 210 | + opt.type = opt.type || 'html' |
| 211 | + opt.append = opt.append || 'body' |
| 212 | + opt.path = opt.path || get_path() |
| 213 | + opt.attr = opt.attr || {width: '200', height: (opt.no_branding ? '60' : '80')} |
| 214 | + |
| 215 | + opt.attr['src'] = get_endpoint() + 'er/' + enc(opt.path) + '.' + enc(opt.type) + '?' |
| 216 | + if (opt.no_branding) opt.attr['src'] += '&no_branding=1' |
| 217 | + if (opt.style) opt.attr['src'] += '&style=' + enc(opt.style) |
| 218 | + if (opt.start) opt.attr['src'] += '&start=' + enc(opt.start) |
| 219 | + if (opt.end) opt.attr['src'] += '&end=' + enc(opt.end) |
| 220 | + |
| 221 | + var tag = {png: 'img', svg: 'img', html: 'iframe'}[opt.type] |
| 222 | + if (!tag) |
| 223 | + return warn('visit_count: unknown type: ' + opt.type) |
| 224 | + |
| 225 | + if (opt.type === 'html') { |
| 226 | + opt.attr['frameborder'] = '0' |
| 227 | + opt.attr['scrolling'] = 'no' |
| 228 | + } |
| 229 | + |
| 230 | + var d = document.createElement(tag) |
| 231 | + for (var k in opt.attr) |
| 232 | + d.setAttribute(k, opt.attr[k]) |
| 233 | + |
| 234 | + var p = document.querySelector(opt.append) |
| 235 | + if (!p) |
| 236 | + return warn('visit_count: append not found: ' + opt.append) |
| 237 | + p.appendChild(d) |
| 238 | + }) |
| 239 | + } |
| 240 | + |
| 241 | + // Make it easy to skip your own views. |
| 242 | + if (location.hash === '#toggle-goatcounter') { |
| 243 | + if (localStorage.getItem('skipgc') === 't') { |
| 244 | + localStorage.removeItem('skipgc', 't') |
| 245 | + alert('GoatCounter tracking is now ENABLED in this browser.') |
| 246 | + } |
| 247 | + else { |
| 248 | + localStorage.setItem('skipgc', 't') |
| 249 | + alert('GoatCounter tracking is now DISABLED in this browser until ' + location + ' is loaded again.') |
| 250 | + } |
| 251 | + } |
| 252 | + |
| 253 | + if (!goatcounter.no_onload) |
| 254 | + on_load(function() { |
| 255 | + // 1. Page is visible, count request. |
| 256 | + // 2. Page is not yet visible; wait until it switches to 'visible' and count. |
| 257 | + // See #487 |
| 258 | + if (!('visibilityState' in document) || document.visibilityState === 'visible') |
| 259 | + goatcounter.count() |
| 260 | + else { |
| 261 | + var f = function(e) { |
| 262 | + if (document.visibilityState !== 'visible') |
| 263 | + return |
| 264 | + document.removeEventListener('visibilitychange', f) |
| 265 | + goatcounter.count() |
| 266 | + } |
| 267 | + document.addEventListener('visibilitychange', f) |
| 268 | + } |
| 269 | + |
| 270 | + if (!goatcounter.no_events) |
| 271 | + goatcounter.bind_events() |
| 272 | + }) |
| 273 | +})(); |
0 commit comments