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
1 changed files with 21 additions and 8 deletions

View File

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