🕷️ Patch tracking element load failure#2417
Conversation
There was a problem hiding this comment.
Pull request overview
This PR makes Docusaurus’ Google gtag page-view tracking resilient to cases where window.gtag is blocked or not yet loaded (e.g., ad blockers / interrupted GTM), preventing runtime errors during navigation.
Changes:
- Adds a runtime guard to return early when
window.gtagis not a function before sendingpage_view.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
The bug is real — the gtag plugin calls I don't want to take it as a package patch, though. Patches freeze us to a plugin version and rot silently (the pnpm migration in #2493 already left this PR's patch-package file inert — it currently applies nothing, which is why CI is green). And this particular fix doesn't need one: we can put the guard in our own code instead. Proposal: rework this as a Docusaurus client module in if (typeof window !== 'undefined' && typeof window.gtag !== 'function') {
window.dataLayer = window.dataLayer || [];
window.gtag = function () {
window.dataLayer.push(arguments);
};
}Client modules load at app bootstrap, before any route update fires, so the plugin's unguarded call always finds a function. Same fix, zero patches, survives every plugin upgrade untouched. Separately, this is worth an upstream issue at facebook/docusaurus so the plugin guards its own call — you found it, you should file it, and I'm glad to co-sign. Want to rework this PR along those lines? |
Co-authored-by: Codex <noreply@openai.com>
Docusaurus does not have a good time right now where there are interruptions between it and the Google Tag Manager. This PR stops the following recurring error (scales with page reloads when it happens):
Changes
Adds a runtime guard around Docusaurus’ Google gtag page-view tracking callback. It is often blocked by an ad blocker, delayed, or not loaded yet, that call can crash.
Previously, the plugin assumed
window.gtagwas always available before sending thepage_viewevent. This patch returns early whenwindow.gtagis not a function, preventing runtime errors in environments where the analytics script is unavailable.The intent is to keep analytics optional and non-blocking, so page navigation continues safely even when Google tracking cannot run. Hence the page-view guard.