Skip to content

Commit 8a1a8b1

Browse files
committed
Improve neutered Google Analytics replacement scriptlet
Related issue: - uBlockOrigin/uAssets#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.
1 parent e98a4b1 commit 8a1a8b1

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

src/web_accessible_resources/google-analytics_analytics.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,24 @@
3737
//
3838
const w = window;
3939
const gaName = w.GoogleAnalyticsObject || 'ga';
40+
const gaQueue = w[gaName];
4041
const ga = function() {
41-
var len = arguments.length;
42-
if ( len === 0 ) {
43-
return;
44-
}
45-
var f = arguments[len-1];
46-
if ( typeof f !== 'object' || f === null || typeof f.hitCallback !== 'function' ) {
47-
return;
42+
const len = arguments.length;
43+
if ( len === 0 ) { return; }
44+
const args = Array.from(arguments);
45+
let fn;
46+
let a = args[len-1];
47+
if ( a instanceof Object && a.hitCallback instanceof Function ) {
48+
fn = a.hitCallback;
49+
} else {
50+
const pos = args.indexOf('hitCallback');
51+
if ( pos !== -1 && args[pos+1] instanceof Function ) {
52+
fn = args[pos+1];
53+
}
4854
}
55+
if ( fn instanceof Function === false ) { return; }
4956
try {
50-
f.hitCallback();
57+
fn();
5158
} catch (ex) {
5259
}
5360
};
@@ -67,4 +74,10 @@
6774
if ( dl instanceof Object && dl.hide instanceof Object && typeof dl.hide.end === 'function' ) {
6875
dl.hide.end();
6976
}
77+
// empty ga queue
78+
if ( gaQueue instanceof Function && Array.isArray(gaQueue.q) ) {
79+
for ( const entry of gaQueue.q ) {
80+
ga(...entry);
81+
}
82+
}
7083
})();

0 commit comments

Comments
 (0)