Skip to content

Commit

Permalink
AG-25544 Improve google-analytics — ga.q. #355
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit 6a6bdfd
Author: Adam Wróblewski <adam@adguard.com>
Date:   Mon Dec 4 18:59:07 2023 +0100

    Simplify forEach

commit bfcdcc5
Author: Adam Wróblewski <adam@adguard.com>
Date:   Mon Dec 4 18:32:44 2023 +0100

    Add ga.q to google-analytics
  • Loading branch information
AdamWr committed Dec 5, 2023
1 parent 223b8a3 commit 8c56c52
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
<!-- TODO: add @added tag to the files with specific version -->
<!-- during new scriptlets or redirects releasing -->

## [Unreleased]

### Added

- `ga.q` (queued commands) to `google-analytics` redirect
[#355](https://github.com/AdguardTeam/Scriptlets/issues/355)

## [v1.9.101] - 2023-11-30

### Added
Expand Down Expand Up @@ -308,6 +315,7 @@ prevent inline `onerror` and match `link` tag [#276](https://github.com/AdguardT
- `metrika-yandex-tag` [#254](https://github.com/AdguardTeam/Scriptlets/issues/254)
- `googlesyndication-adsbygoogle` [#252](https://github.com/AdguardTeam/Scriptlets/issues/252)

[Unreleased]: https://github.com/AdguardTeam/Scriptlets/compare/v1.9.101...HEAD
[v1.9.101]: https://github.com/AdguardTeam/Scriptlets/compare/v1.9.96...v1.9.101
[v1.9.96]: https://github.com/AdguardTeam/Scriptlets/compare/v1.9.91...v1.9.96
[v1.9.91]: https://github.com/AdguardTeam/Scriptlets/compare/v1.9.83...v1.9.91
Expand Down
10 changes: 10 additions & 0 deletions src/redirects/google-analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export function GoogleAnalytics(source) {
proto.send = noopFunc;

const googleAnalyticsName = window.GoogleAnalyticsObject || 'ga';
const queue = window[googleAnalyticsName]?.q;

// a -- fake arg for 'ga.length < 1' antiadblock checking
// eslint-disable-next-line no-unused-vars
function ga(a) {
Expand Down Expand Up @@ -70,6 +72,14 @@ export function GoogleAnalytics(source) {
ga.loaded = true;
window[googleAnalyticsName] = ga;

if (Array.isArray(queue)) {
const push = (arg) => {
ga(...arg);
};
queue.push = push;
queue.forEach(push);
}

const { dataLayer, google_optimize } = window; // eslint-disable-line camelcase
if (dataLayer instanceof Object === false) {
return;
Expand Down
40 changes: 39 additions & 1 deletion tests/redirects/google-analytics.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ test('Test eventCallback mocking', (assert) => {
test('Test .push(data) mocking', (assert) => {
// emulate API
window.dataLayer = [];
window.dataLayer.push = () => {};
window.dataLayer.push = () => { };
const gtag = (data) => {
window.dataLayer.push(data);
};
Expand Down Expand Up @@ -234,3 +234,41 @@ test('Test callback mocking', (assert) => {

assert.strictEqual(window.hit, 'FIRED', 'hit function was executed');
});

test('Test ga queued commands', (assert) => {
let firstScriptExecuted = false;
let secondScriptExecuted = false;

function gaFunction(...args) {
window.ga.q = window.ga.q || [];
window.ga.q.push(args);
}

window.ga = window.ga || gaFunction;

window.ga('create', 'UA-32789052-1', 'auto');

window.ga('send', 'pageview', {
page: '/',
hitCallback: () => {
firstScriptExecuted = true;
},
});

window.ga('event', 'pageview', {
page: '/test',
hitCallback: () => {
secondScriptExecuted = true;
},
});

runRedirect(name);

const done = assert.async();

setTimeout(() => {
assert.strictEqual(firstScriptExecuted, true, 'first hitCallback executed');
assert.strictEqual(secondScriptExecuted, true, 'second hitCallback executed');
done();
}, 20);
});

0 comments on commit 8c56c52

Please sign in to comment.