Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve google-analytics — add 'ga.q' property #355

Closed
AdamWr opened this issue Sep 4, 2023 · 0 comments
Closed

Improve google-analytics — add 'ga.q' property #355

AdamWr opened this issue Sep 4, 2023 · 0 comments

Comments

@AdamWr
Copy link
Member

AdamWr commented Sep 4, 2023

Related to - AdguardTeam/AdguardFilters#160751
Steps to reproduce:

  1. Add this rule:
||google-analytics.com/analytics.js$script,redirect=google-analytics,important
  1. Go to - https://example.org/
  2. In browser console run:
(function (i, s, o, g, r, a, m) {
  i['GoogleAnalyticsObject'] = r;
  i[r] = i[r] || function () {
    (i[r].q = i[r].q || []).push(arguments);
  }, i[r].l = 1 * new Date();
  a = s.createElement(o), m = document.body;
  a.async = 1;
  a.src = g;
  m.parentNode.insertBefore(a, m);
})(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');
ga('create', 'UA-32789052-1', 'auto');
ga('send', 'pageview', {
  'page': '/',
  'hitCallback': function () {
    location.href = 'https://adguard.com/';
  }
});

There should be redirection to https://adguard.com/, but it doesn't work.

We could add something like:

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

before ga function:

And:

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

before:

const { dataLayer, google_optimize } = window; // eslint-disable-line camelcase

It should fixes this issue.


Something similar is done in https://searchfox.org/mozilla-central/source/browser/extensions/webcompat/shims/google-analytics-and-tag-manager.js

@slavaleleka slavaleleka added the enhancement Improvement of existent feature label Nov 2, 2023
@adguard-bot adguard-bot assigned slavaleleka and unassigned maximtop Nov 2, 2023
@adguard-bot adguard-bot changed the title Improve 'google-analytics' — ga.q Improve google-analytics — ga.q Nov 2, 2023
adguard pushed a commit that referenced this issue Dec 5, 2023
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
@adguard-bot adguard-bot changed the title Improve google-analytics — ga.q Improve google-analytics — add 'ga.q' property Feb 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants