Skip to content

Global gtag function not exposed when using GTM via useScriptGoogleTagManagerΒ #523

@DanLDU

Description

@DanLDU

πŸ†’ Your use case

When using the useScriptGoogleTagManager composable from the nuxt-scripts module in a Nuxt 4 project, the gtag function is not exposed globally (window.gtag remains undefined), even after the GTM script has loaded.

This prevents any manual use of gtag(), which is essential for features like dynamic consent updates, event tracking, and Google Consent Mode compliance.

πŸ†• The solution you'd like

Once the GTM script has loaded, window.gtag should be available globally. This allows developers to:

  • Update consent dynamically (gtag('consent', 'update', {...}))
  • Track custom events manually
  • Integrate with third-party libraries that rely on gtag() being present

This is standard behavior when the GTM script is loaded manually or via traditional integrations like @nuxtjs/gtm.

πŸ” Alternatives you've considered

// google-tag-manager.ts

Inside clientInit
Assign gtag function to window for global access:
(window as any).gtag = gtag; or

ℹ️ Additional info

Versions:

  • Nuxt 4.1.2
  • Nuxt Scripts 0.12.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions