Update goatcounter script

This commit is contained in:
Gabriel Augendre 2021-04-17 17:36:07 +02:00
parent a72d88771f
commit 373512e52d

View file

@ -1,14 +1,24 @@
// GoatCounter: https://www.goatcounter.com // GoatCounter: https://www.goatcounter.com
// This file (and *only* this file) is released under the ISC license: // This file (and *only* this file) is released under the ISC license:
// https://opensource.org/licenses/ISC // https://opensource.org/licenses/ISC
(function() { ;(function() {
'use strict'; 'use strict';
if (window.goatcounter && window.goatcounter.vars) // Compatibility if (window.goatcounter && window.goatcounter.vars) // Compatibility with very old version; do not use.
window.goatcounter = window.goatcounter.vars window.goatcounter = window.goatcounter.vars
else else
window.goatcounter = window.goatcounter || {} window.goatcounter = window.goatcounter || {}
// Load settings from data-goatcounter-settings.
var s = document.querySelector('script[data-goatcounter]')
if (s && s.dataset.goatcounterSettings) {
try { var set = JSON.parse(s.dataset.goatcounterSettings) }
catch (err) { console.error('invalid JSON in data-goatcounter-settings: ' + err) }
for (var k in set)
if (['no_onload', 'no_events', 'allow_local', 'allow_frame', 'path', 'title', 'referrer', 'event'].indexOf(k) > -1)
window.goatcounter[k] = set[k]
}
// Get all data we're going to send off to the counter endpoint. // Get all data we're going to send off to the counter endpoint.
var get_data = function(vars) { var get_data = function(vars) {
var data = { var data = {
@ -72,7 +82,7 @@
// Get the endpoint to send requests to. // Get the endpoint to send requests to.
var get_endpoint = function() { var get_endpoint = function() {
var s = document.querySelector('script[data-goatcounter]'); var s = document.querySelector('script[data-goatcounter]')
if (s && s.dataset.goatcounter) if (s && s.dataset.goatcounter)
return s.dataset.goatcounter return s.dataset.goatcounter
return (goatcounter.endpoint || window.counter) // counter is for compat; don't use. return (goatcounter.endpoint || window.counter) // counter is for compat; don't use.
@ -91,6 +101,14 @@
return (loc.pathname + loc.search) || '/' return (loc.pathname + loc.search) || '/'
} }
// Run function after DOM is loaded.
var on_load = function(f) {
if (document.body === null)
document.addEventListener('DOMContentLoaded', function() { f() }, false)
else
f()
}
// Filter some requests that we (probably) don't want to count. // Filter some requests that we (probably) don't want to count.
goatcounter.filter = function() { goatcounter.filter = function() {
if ('visibilityState' in document && (document.visibilityState === 'prerender' || document.visibilityState === 'hidden')) if ('visibilityState' in document && (document.visibilityState === 'prerender' || document.visibilityState === 'hidden'))
@ -137,7 +155,7 @@
img.setAttribute('aria-hidden', 'true') img.setAttribute('aria-hidden', 'true')
var rm = function() { if (img && img.parentNode) img.parentNode.removeChild(img) } var rm = function() { if (img && img.parentNode) img.parentNode.removeChild(img) }
setTimeout(rm, 3000) // In case the onload isn't triggered. setTimeout(rm, 10000) // In case the onload isn't triggered.
img.addEventListener('load', rm, false) img.addEventListener('load', rm, false)
document.body.appendChild(img) document.body.appendChild(img)
} }
@ -178,37 +196,39 @@
// Add a "visitor counter" frame or image. // Add a "visitor counter" frame or image.
window.goatcounter.visit_count = function(opt) { window.goatcounter.visit_count = function(opt) {
opt = opt || {} on_load(function() {
opt.type = opt.type || 'html' opt = opt || {}
opt.append = opt.append || 'body' opt.type = opt.type || 'html'
opt.path = opt.path || get_path() opt.append = opt.append || 'body'
opt.attr = opt.attr || {width: '200', height: (opt.no_branding ? '60' : '80')} opt.path = opt.path || get_path()
opt.attr = opt.attr || {width: '200', height: (opt.no_branding ? '60' : '80')}
opt.attr['src'] = get_endpoint() + 'er/' + encodeURIComponent(opt.path) + '.' + opt.type + '?' opt.attr['src'] = get_endpoint() + 'er/' + encodeURIComponent(opt.path) + '.' + opt.type + '?'
if (opt.no_branding) opt.attr['src'] += '&no_branding=1' if (opt.no_branding) opt.attr['src'] += '&no_branding=1'
if (opt.style) opt.attr['src'] += '&style=' + encodeURIComponent(opt.style) if (opt.style) opt.attr['src'] += '&style=' + encodeURIComponent(opt.style)
var tag = {png: 'img', svg: 'img', html: 'iframe'}[opt.type] var tag = {png: 'img', svg: 'img', html: 'iframe'}[opt.type]
if (!tag) if (!tag)
return warn('visit_count: unknown type: ' + opt.type) return warn('visit_count: unknown type: ' + opt.type)
if (opt.type === 'html') { if (opt.type === 'html') {
opt.attr['frameborder'] = '0' opt.attr['frameborder'] = '0'
opt.attr['scrolling'] = 'no' opt.attr['scrolling'] = 'no'
} }
var d = document.createElement(tag) var d = document.createElement(tag)
for (var k in opt.attr) for (var k in opt.attr)
d.setAttribute(k, opt.attr[k]) d.setAttribute(k, opt.attr[k])
var p = document.querySelector(opt.append) var p = document.querySelector(opt.append)
if (!p) if (!p)
return warn('visit_count: append not found: ' + opt.append) return warn('visit_count: append not found: ' + opt.append)
p.appendChild(d) p.appendChild(d)
})
} }
// Make it easy to skip your own views. // Make it easy to skip your own views.
if (location.hash === '#toggle-goatcounter') if (location.hash === '#toggle-goatcounter') {
if (localStorage.getItem('skipgc') === 't') { if (localStorage.getItem('skipgc') === 't') {
localStorage.removeItem('skipgc', 't') localStorage.removeItem('skipgc', 't')
alert('GoatCounter tracking is now ENABLED in this browser.') alert('GoatCounter tracking is now ENABLED in this browser.')
@ -217,17 +237,12 @@
localStorage.setItem('skipgc', 't') localStorage.setItem('skipgc', 't')
alert('GoatCounter tracking is now DISABLED in this browser until ' + location + ' is loaded again.') alert('GoatCounter tracking is now DISABLED in this browser until ' + location + ' is loaded again.')
} }
}
if (!goatcounter.no_onload) { if (!goatcounter.no_onload)
var go = function() { on_load(function() {
goatcounter.count() goatcounter.count()
if (!goatcounter.no_events) if (!goatcounter.no_events)
goatcounter.bind_events() goatcounter.bind_events()
} })
if (document.body === null)
document.addEventListener('DOMContentLoaded', function() { go() }, false)
else
go()
}
})(); })();