Skip to content

fix(gtm): invoke onBeforeGtmStart callback when ID is in config - #584

Merged
harlan-zw merged 4 commits into
mainfrom
gtm-config-callback
Jan 18, 2026
Merged

fix(gtm): invoke onBeforeGtmStart callback when ID is in config#584
harlan-zw merged 4 commits into
mainfrom
gtm-config-callback

Conversation

@harlan-zw

@harlan-zwharlan-zw commented Jan 18, 2026

Copy link
Copy Markdown
Collaborator

🔗 Linked issue

Resolves#522

❓ Type of change

  • 📖 Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • 👌 Enhancement (improving an existing functionality)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

📚 Description

When GTM ID is configured in nuxt.config.scripts.registry, the module generates a plugin that pre-initializes the script at app startup—before any user components mount. This meant onBeforeGtmStart callbacks passed later in components were never invoked since clientInit had already run with no callback in its closure.

Now after useRegistryScript returns, we check if window.gtag already exists (indicating pre-initialization) and immediately invoke the callback.

// nuxt.config - ID in config
scripts: {registry: {googleTagManager: {id: "GTM-XXXXXX"}}}// component - callback now worksuseScriptGoogleTagManager({onBeforeGtmStart: (gtag)=>{gtag('consent','default',{ad_storage: 'denied'})}})

harlan-zwand others added 4 commits January 16, 2026 01:58
Combined commits:
- fix(plausible): use consistent window reference in clientInit stub
When GTM ID is configured in nuxt.config, the plugin pre-initializes
the script before user components mount. This caused onBeforeGtmStart
callbacks passed to useScriptGoogleTagManager() to never be invoked.
Now checks if script was already initialized (gtag exists) and invokes
the callback immediately for cached/pre-initialized scripts.
Closes#522
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@vercel

vercelBot commented Jan 18, 2026

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

ProjectDeploymentReviewUpdated (UTC)
scripts-docsReadyReadyPreview, CommentJan 18, 2026 2:04am
scripts-playgroundReadyReadyPreview, CommentJan 18, 2026 2:04am

@pkg-pr-new

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/nuxt/scripts/@nuxt/scripts@584

commit: 46f2e17

@harlan-zw
harlan-zw merged commit f8ce5a1 into mainJan 18, 2026
10 checks passed
@harlan-zw
harlan-zw deleted the gtm-config-callback branch January 18, 2026 02:05
@harlan-zwharlan-zw mentioned this pull request Jan 23, 2026
zizzfizzix pushed a commit to zizzfizzix/nuxt-scripts that referenced this pull request Mar 5, 2026
…#584)
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Sign up for freeto 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.

useScriptGoogleTagManager onBeforeGtmStart callback not triggered when GTM ID is set via nuxt.config

1 participant

@harlan-zw