-
Notifications
You must be signed in to change notification settings - Fork 101
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
Offloading Google Analytics (gtag) to a Web Worker #1455
Comments
This integration wouldn't make sense for core merge, so it should be put into a separate file perhaps in an |
@westonruter Nice! There's also the GTM4WP - A Google Tag Manager for WordPress plugin, which has 700,000 installs. |
I just realized that the inline scripts will be problematic for the current implementation of Web Worker Offloading, since it automatically blocks offloading to a worker when there are any inline after scripts (or, importantly, any blocking dependent scripts), and it does this indirectly by setting a Given that we do actually need to opt-in scripts with after scripts to be offloaded to the worker, I wonder if this was the right way to go? Should the code be replaced with logic instead to focus on setting the We faced this same struggle when implementing the script strategies, without ever implementing support for delayed inline after scripts. See Core-58632. We opted to err on the side of caution to not cause breakages. We considered adding a |
@gutobenn Thanks for that. I see it is not included in my search because it doesn't include the |
Ahhh, nice find! I did some tests and it seems that www.googletagmanager.com/gtm.js returns a 404 error only if you do not provide valid values for the required parameters id and gtm_auth (e.g., https://www.googletagmanager.com/gtm.js?id=GTM-XXXXXXX>m_auth=XXXXXXXXXXXX). |
Has this actually been tested? We tried running it some year back but eventually gave up due to:
looking at their issue tracker i can see there are plenty of these kinds of bugs that for many sites are simply not allowed to happen no matter what QwikDev/partytown#583 We chose that reliable tracking was more important than the performance gain. Super excited if this does end up working out though, I just wanted to check a POC has been tested. |
@oxyc Thanks a lot for this valuable feedback. @thelovekesh has been doing some testing, but this hasn't been completed yet. Ultimately, of course, the third-party script providers should be doing this web worker offloading themselves so as to not require Partytown's shims in the first place. But I have been hopeful the Partytown would provide a way to jumpstart this. But your findings may prove Partytown to be currently infeasible. |
@oxyc oh, actually, the scope of this issue is not for GTM generally, but rather for "gtag" for Google Analytics which (confusingly) is loaded from |
@westonruter Is there a particular way I can help with this? Any questions you have? For reference:
Relevant classes used include: |
@felixarntz Thanks! I guess the main thing would be how best to set up a local development environment to get the gtag script to be output. With WooCommerce it was simply a matter of activating the plugin and adding a random Google Analytics ID. With Site Kit I'd want to avoid connecting it to an actual Google account to test. I'm sure there are some established methods for local development, so I'd love to be pointed in that direction. |
As far as I remember, the local development workflows still rely on being connected to an actual Google account. There are several security requirements in place and working around them would be so tedious that back then it was not deemed worthwhile to spend hours of development on it - also because a lot of developing with Site Kit is about the Google service integrations, so developing without being connected to the services would have us more likely miss potential issues. What I personally do when developing for it is set up a local site that has the same site URL as my live site where I used Site Kit with various services. That's a bit of a strange development setup, but the easiest to set up and most reliable to work with. Alternatively, you can use the official helper plugin, which allows you to run this on any local site URL, but you'll still need to add the URL of an actual site using Site Kit with your Google account into that helper plugin's UI, which will make Site Kit behave more or less as if that was the site you're using. It will also require setting up a custom OAuth app, which means you won't be using Site Kit Service - which should be an okay limitation for what we're trying to do here, since it doesn't tie into the service at all. See https://sitekit.withgoogle.com/documentation/using-site-kit/staging/ for relevant instructions if you want to go with that approach. |
I've got two PRs open now for both Rank Math and Site Kit:
The Site Kit one is blocked by a necessary upstream change related to Consent Mode. |
Feature Description
The Web Worker Offloading plugin is now merged into trunk (#1247). However, for it to be utilized a plugin author has to manually make the
web-worker-offloading
script a dependency of the script intended to be offloaded. As mentioned in #1247 (comment), we should look for opportunities to offload existing scripts automatically as much as possible. From Partytown's Common Services page, they list out 3rd party services known to be compatible. Included on that list is Google Tag Manager, which I've found to be responsible for INP issues on highly-trafficked sites. Partytown has documentation for how to integrate with Google Tag Manager.The challenge for automatically implementing the Partytown integration with Google Tag Manager is that the plugins (and themes) which add gtag do so in inconsistent ways. The scripts could be registered properly via
wp_enqueue_script()
but the handles won't be consistent. Or a plugin may manually print the script atwp_head
.In the case where a plugin properly enqueues the script, we can look at WPdirectory for the most common script handles used. Otherwise, for manually-printed scripts the alternative would be for Web Worker Offloading to integrate with Optimization Detective to register a tag visitor that looks for a
SCRIPT
tag with asrc
pointing tohttps://www.googletagmanager.com/gtag/js
, and when present, inject the Partytown script (if not already present) and update theSCRIPT
tag attributes as necessary.In looking at WPdirectory for plugins with at least 100K installs, the script handles used are:
google-tag-manager
google_gtagjs
google_gtagjs
These are not using GTM:
These add up to 11.2 million installs.
The following plugins print the script without using
WP_Scripts
:Excluding the Jetpack legacy module, these total up to 4.4 million installs.
So by starting out just targeting scripts registered via
WP_Scripts
, we can handle ~72% of the installs.So the only script handles used are
google-tag-manager
andgoogle_gtagjs
. However, we could discover other script handles by looping overwp_scripts()->registered
to find any other dependencies with asrc
beginning withhttps://www.googletagmanager.com/gtag/js
at runtime. We can then addweb-worker-offloading
as a dependency for this registered script.However, adding the script dependency is half of what we need to do. We also need to make sure that the inline script also gets the
text/partytown
type:WooCommerce adds this as an inline after script. As does Site Kit. It seems that "Google Analytics for WooCommerce" adds another script with its own inline script, so it may not be as straightforward to offload to a worker.
WC_Google_Gtag_JS::enquque_tracker()
[sic] method inwoocommerce-google-analytics-integration/includes/class-wc-google-gtag-js.php
But in the case of WooCommerce and Site Kit doing things in a more straightforward case: in order to add the
type="text/partytown"
to the inline script, we can do so via filteringwp_inline_script_attributes
. For example:The text was updated successfully, but these errors were encountered: