-
Notifications
You must be signed in to change notification settings - Fork 70
Description
π 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)