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

Resolve Google Analytics not loading if gtag already exists on the window object. #370

Merged
merged 2 commits into from
Mar 7, 2024
Merged

Resolve Google Analytics not loading if gtag already exists on the window object. #370

merged 2 commits into from
Mar 7, 2024

Conversation

workeffortwaste
Copy link
Contributor

Changes proposed in this Pull Request:

Closes #367.

Resolves the tracker not loading if gtag already exists on the window object.

It's valid for gtag to already exist on the object, as a cookie banner may have created it to send a consent update, or it may exist from another tag such as Google Ads.

Here's an example from Google where it's valid for it to be created multiple times:

https://developers.google.com/tag-platform/security/guides/consent?consentmode=advanced#gtag.js_3

Checks:

  • Does your code follow the WordPress coding standards?
  • Have you successfully run tests with your changes locally?
  • Have you checked to ensure there aren't other open Pull Requests for the same update/change?

Detailed test instructions:

  1. Initialise a gtag object on the window before the plugin loads its JS.

Barebones example for testing purposes:

<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
</script>
  1. Confirm that the plugin has successfully loaded the Google Analytics tag.

@tomalec tomalec self-requested a review March 7, 2024 10:58
Initialize directly in the constructor.
Copy link
Member

@tomalec tomalec left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the fix!

I reviewed the code and tested it locally LGTM.

I've also removed the init function, moving its contents to the constructor. I see no reason to keep it publicly available.
With its contents in the constructor, we're sure we will not re-initialize the gtag ourselves

@tomalec tomalec merged commit 7594c1b into woocommerce:trunk Mar 7, 2024
1 check passed
@jorgemd24 jorgemd24 mentioned this pull request Mar 7, 2024
19 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Plugin does not load tracker if gtag already exists on the window object
2 participants