Skip to content

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

@DanLDU

Description

@DanLDU

πŸ› The bug

When using the useScriptGoogleTagManager composable in a Nuxt 4 project with the GTM ID defined in nuxt.config, the onBeforeGtmStart callback is not triggered.

Example (Doesn't work):

// nuxt.config:
scripts: {
    registry: {
      googleTagManager: {
        id: "GTM-XXXXXX",
      },
    },
  }

//component: 
const { proxy } = useScriptGoogleTagManager({
  onBeforeGtmStart: (gtag) => {
    gtag('consent', 'default', {
      ad_user_data: 'denied',
      ad_personalization: 'denied',
      ad_storage: 'granted',
      analytics_storage: 'denied',
      wait_for_update: 500,
    });
  },
});

However, if the GTM ID is passed directly as an argument to useScriptGoogleTagManager, the callback works as expected.
Example (It works):

const { proxy } = useScriptGoogleTagManager({
  id: 'GTM-XXXXXXXX',
  onBeforeGtmStart: (gtag) => {
    gtag('consent', 'default', {
      ad_user_data: 'denied',
      ad_personalization: 'denied',
      ad_storage: 'granted',
      analytics_storage: 'denied',
      wait_for_update: 500,
    });
  },
});

πŸ› οΈ To reproduce

https://stackblitz.com/edit/nuxt-starter-zwdg7pfn?file=package.json,nuxt.config.ts,pages%2Findex.vue

🌈 Expected behavior

When the GTM ID is set via the Nuxt config, the onBeforeGtmStart hook passed to useScriptGoogleTagManager should be called before the GTM script starts, just as it does when passing the ID directly.

https://scripts.nuxt.com/scripts/tracking/google-tag-manager#configuring-gtm-before-it-starts

ℹ️ Additional context

Versions:

  • Nuxt 4.1.2
  • Nuxt Scripts 0.12.1

Using the nuxt scripts module's useScriptGoogleTagManager

The difference seems to be related to the GTM ID source (config vs argument)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions