Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 16 additions & 18 deletions _assets/js/vendor/count.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
;(function() {
'use strict';

if (window.goatcounter && window.goatcounter.vars) // Compatibility with very old version; do not use.
window.goatcounter = window.goatcounter.vars
else
window.goatcounter = window.goatcounter || {}
window.goatcounter = window.goatcounter || {}

// Load settings from data-goatcounter-settings.
var s = document.querySelector('script[data-goatcounter]')
Expand All @@ -21,13 +18,14 @@
var enc = encodeURIComponent

// Get all data we're going to send off to the counter endpoint.
var get_data = function(vars) {
window.goatcounter.get_data = function(vars) {
vars = vars || {}
var data = {
p: (vars.path === undefined ? goatcounter.path : vars.path),
r: (vars.referrer === undefined ? goatcounter.referrer : vars.referrer),
t: (vars.title === undefined ? goatcounter.title : vars.title),
e: !!(vars.event || goatcounter.event),
s: [window.screen.width, window.screen.height, (window.devicePixelRatio || 1)],
s: window.screen.width,
b: is_bot(),
q: location.search,
}
Expand All @@ -40,6 +38,7 @@
if (is_empty(data.r)) data.r = document.referrer
if (is_empty(data.t)) data.t = document.title
if (is_empty(data.p)) data.p = get_path()
if (vars.no_session) data.ns = (typeof(vars.no_session) === 'function' ? vars.no_session(false) : vars.no_session)

if (rcb) data.r = rcb(data.r)
if (tcb) data.t = tcb(data.t)
Expand Down Expand Up @@ -84,9 +83,7 @@
// Get the endpoint to send requests to.
var get_endpoint = function() {
var s = document.querySelector('script[data-goatcounter]')
if (s && s.dataset.goatcounter)
return s.dataset.goatcounter
return (goatcounter.endpoint || window.counter) // counter is for compat; don't use.
return (s && s.dataset.goatcounter) ? s.dataset.goatcounter : goatcounter.endpoint
}

// Get current path.
Expand All @@ -111,7 +108,7 @@
}

// Filter some requests that we (probably) don't want to count.
goatcounter.filter = function() {
window.goatcounter.filter = function() {
if ('visibilityState' in document && document.visibilityState === 'prerender')
return 'visibilityState'
if (!goatcounter.allow_frame && location !== parent.location)
Expand All @@ -127,7 +124,7 @@

// Get URL to send to GoatCounter.
window.goatcounter.url = function(vars) {
var data = get_data(vars || {})
var data = window.goatcounter.get_data(vars || {})
if (data.p === null) // null from user callback.
return
data.rnd = Math.random().toString(36).substr(2, 5) // Browsers don't always listen to Cache-Control.
Expand All @@ -148,7 +145,7 @@
if (!url)
return warn('not counting because path callback returned null')

if (!navigator.sendBeacon(url)) {
if (!navigator || !navigator.sendBeacon || !navigator.sendBeacon(url)) {
// This mostly fails due to being blocked by CSP; try again with an
// image-based fallback.
var img = document.createElement('img')
Expand Down Expand Up @@ -183,10 +180,11 @@
var send = function(elem) {
return function() {
goatcounter.count({
event: true,
path: (elem.dataset.goatcounterClick || elem.name || elem.id || ''),
title: (elem.dataset.goatcounterTitle || elem.title || (elem.innerHTML || '').substr(0, 200) || ''),
referrer: (elem.dataset.goatcounterReferrer || elem.dataset.goatcounterReferral || ''),
event: true,
path: (elem.dataset.goatcounterClick || elem.name || elem.id || ''),
title: (elem.dataset.goatcounterTitle || elem.title || (elem.innerHTML || '').substr(0, 200) || ''),
referrer: (elem.dataset.goatcounterReferrer || elem.dataset.goatcounterReferral || ''),
no_session: ['1', 't', 'true'].indexOf((elem.dataset.goatcounterNoSession || '').toLowerCase()) !== -1,
})
}
}
Expand Down Expand Up @@ -231,7 +229,7 @@

var p = document.querySelector(opt.append)
if (!p)
return warn('visit_count: append not found: ' + opt.append)
return warn('visit_count: element to append to not found: ' + opt.append)
p.appendChild(d)
})
}
Expand Down Expand Up @@ -268,4 +266,4 @@
if (!goatcounter.no_events)
goatcounter.bind_events()
})
})();
})();
Loading