11// GoatCounter: https://www.goatcounter.com
2- // This file (and *only* this file) is released under the ISC license:
3- // https://opensource.org/licenses/ISC
2+ // This file is released under the ISC license: https://opensource.org/licenses/ISC
43; ( function ( ) {
54 'use strict' ;
65
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 || { }
6+ window . goatcounter = window . goatcounter || { }
117
128 // Load settings from data-goatcounter-settings.
139 var s = document . querySelector ( 'script[data-goatcounter]' )
2218 var enc = encodeURIComponent
2319
2420 // Get all data we're going to send off to the counter endpoint.
25- var get_data = function ( vars ) {
21+ window . goatcounter . get_data = function ( vars ) {
22+ vars = vars || { }
2623 var data = {
2724 p : ( vars . path === undefined ? goatcounter . path : vars . path ) ,
2825 r : ( vars . referrer === undefined ? goatcounter . referrer : vars . referrer ) ,
2926 t : ( vars . title === undefined ? goatcounter . title : vars . title ) ,
3027 e : ! ! ( vars . event || goatcounter . event ) ,
31- s : [ window . screen . width , window . screen . height , ( window . devicePixelRatio || 1 ) ] ,
28+ s : window . screen . width ,
3229 b : is_bot ( ) ,
3330 q : location . search ,
3431 }
8582 // Get the endpoint to send requests to.
8683 var get_endpoint = function ( ) {
8784 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.
85+ return ( s && s . dataset . goatcounter ) ? s . dataset . goatcounter : goatcounter . endpoint
9186 }
9287
9388 // Get current path.
112107 }
113108
114109 // Filter some requests that we (probably) don't want to count.
115- goatcounter . filter = function ( ) {
110+ window . goatcounter . filter = function ( ) {
116111 if ( 'visibilityState' in document && document . visibilityState === 'prerender' )
117112 return 'visibilityState'
118113 if ( ! goatcounter . allow_frame && location !== parent . location )
128123
129124 // Get URL to send to GoatCounter.
130125 window . goatcounter . url = function ( vars ) {
131- var data = get_data ( vars || { } )
126+ var data = window . goatcounter . get_data ( vars || { } )
132127 if ( data . p === null ) // null from user callback.
133128 return
134129 data . rnd = Math . random ( ) . toString ( 36 ) . substr ( 2 , 5 ) // Browsers don't always listen to Cache-Control.
145140 var f = goatcounter . filter ( )
146141 if ( f )
147142 return warn ( 'not counting because of: ' + f )
148-
149143 var url = goatcounter . url ( vars )
150144 if ( ! url )
151145 return warn ( 'not counting because path callback returned null' )
152146
153- if ( navigator . sendBeacon )
154- navigator . sendBeacon ( url )
155- else { // Fallback for (very) old browsers .
147+ if ( ! navigator . sendBeacon ( url ) ) {
148+ // This mostly fails due to being blocked by CSP; try again with an
149+ // image-based fallback .
156150 var img = document . createElement ( 'img' )
157151 img . src = url
158152 img . style . position = 'absolute' // Affect layout less.
233227
234228 var p = document . querySelector ( opt . append )
235229 if ( ! p )
236- return warn ( 'visit_count: append not found: ' + opt . append )
230+ return warn ( 'visit_count: element to append to not found: ' + opt . append )
237231 p . appendChild ( d )
238232 } )
239233 }
270264 if ( ! goatcounter . no_events )
271265 goatcounter . bind_events ( )
272266 } )
273- } ) ( ) ;
267+ } ) ( ) ;
0 commit comments