Improve neutered Google Analytics replacement scriptlet

Related issue:
- https://github.com/uBlockOrigin/uAssets/issues/5132

The change in this commit make it so that it's no
longer required to have an exception filter for
`google-analytics.com/analytics.js` for the page to
render properly.
This commit is contained in:
Raymond Hill 2019-12-01 10:40:05 -05:00
parent e98a4b1ace
commit 8a1a8b103f
No known key found for this signature in database
GPG key ID: 25E1490B761470C2

View file

@ -37,17 +37,24 @@
// //
const w = window; const w = window;
const gaName = w.GoogleAnalyticsObject || 'ga'; const gaName = w.GoogleAnalyticsObject || 'ga';
const gaQueue = w[gaName];
const ga = function() { const ga = function() {
var len = arguments.length; const len = arguments.length;
if ( len === 0 ) { if ( len === 0 ) { return; }
return; const args = Array.from(arguments);
} let fn;
var f = arguments[len-1]; let a = args[len-1];
if ( typeof f !== 'object' || f === null || typeof f.hitCallback !== 'function' ) { if ( a instanceof Object && a.hitCallback instanceof Function ) {
return; fn = a.hitCallback;
} else {
const pos = args.indexOf('hitCallback');
if ( pos !== -1 && args[pos+1] instanceof Function ) {
fn = args[pos+1];
}
} }
if ( fn instanceof Function === false ) { return; }
try { try {
f.hitCallback(); fn();
} catch (ex) { } catch (ex) {
} }
}; };
@ -67,4 +74,10 @@
if ( dl instanceof Object && dl.hide instanceof Object && typeof dl.hide.end === 'function' ) { if ( dl instanceof Object && dl.hide instanceof Object && typeof dl.hide.end === 'function' ) {
dl.hide.end(); dl.hide.end();
} }
// empty ga queue
if ( gaQueue instanceof Function && Array.isArray(gaQueue.q) ) {
for ( const entry of gaQueue.q ) {
ga(...entry);
}
}
})(); })();