Uh oh!
There was an error while loading. Please reload this page.
feat(gtm): add onBeforeGtmStart - #392
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
This is a great escape hatch. Would it be possible to document with an example of when you'd use this 🙏 I think if all EU users need the consent mode we should have a dedicated docs section for that 🤔 |
huang-julien
commented
Feb 17, 2025
Seems like we DO need to use a |
huang-julien
commented
Feb 17, 2025
Are you talking about a whole page ? |
harlan-zw
commented
Feb 18, 2025
Nah just like a section in the GTM page, let's go with this for now. Thanks! |
endorfin
commented
Sep 8, 2025
The hook “onBeforeGtmStart” does not work correctly, the event “gtm.init_consent” is not being set. I solved the problem as follows: <template>
<Teleport to="body">
<divv-if="!consent"class="fixed inset-0 bg-black/90 flex flex-col items-center justify-center space-y-4 z-50">
<divclass="absolute bottom-6 inset-x-6 bg-white dark:bg-gray-800 rounded-lg">
<divclass="w-full py-12 px-6 lg:px-6 xl:px-20 max-w-full mx-auto xl:max-w-content">
<divclass="headline-lg mb-6">
{{ $t('common.cookieBanner.title') }}
</div>
<pclass="mb-8 max-w-xl">
{{ $t('common.cookieBanner.description') }}
</p>
<BaseButtonGroup>
<BaseButton
:text="$t('common.cookieBanner.btnAcceptAll')"
@click="handleConsent(true)"
/>
<BaseButton
variant="outline"
:text="$t('common.cookieBanner.btnConfirmSelection')"
@click="handleConsent(false)" />
</BaseButtonGroup>
</div>
</div>
</div>
</Teleport>
</template>
<script setup>constconsent=useCookie('consent')functiongtag() {window.dataLayer=window.dataLayer|| [];dataLayer.push(arguments);}functionhandleConsent(consentGiven) {consent.value= consentGiven ?'granted':'denied'gtag('consent', 'update', { ad_user_data:consent.value, ad_personalization:consent.value, ad_storage:consent.value, analytics_storage:consent.value })}onMounted(() => {gtag('consent', 'default', {'ad_user_data':'denied','ad_personalization':'denied','ad_storage':'denied','analytics_storage':'denied','wait_for_update':500, })const { proxy } =useScriptGoogleTagManager()useScriptEventPage(({ title, path }) => {proxy.dataLayer.push({event:'pageview', title, path }) })})</script> |
harlan-zw
commented
Sep 9, 2025
Would you mind making an issue to track? 🙂 |
huang-julien
commented
Sep 9, 2025
Yup doing that tomorrow 👍👍 |
I just discovered another important point. When I use my own gtag function, everything also works through the onBeforeGtmStart hook. But if you use the gtag function provided by the onBeforeGtmStart hook, then it doesn’t work. The issue is caused by the gtag function provided by the onBeforeGtmStart hook. This works function gtag() {
window.dataLayer = window.dataLayer || [];
dataLayer.push(arguments);
}
onMounted(() => {
const { proxy } = useScriptGoogleTagManager({
onBeforeGtmStart: () => {
gtag('consent', 'default', {
'ad_user_data': 'denied',
'ad_personalization': 'denied',
'ad_storage': 'denied',
'analytics_storage': 'denied',
'wait_for_update': 500,
})
}
})
})This doesn’t work onMounted(() => {
const { proxy } = useScriptGoogleTagManager({
onBeforeGtmStart: (gtag) => {
gtag('consent', 'default', {
'ad_user_data': 'denied',
'ad_personalization': 'denied',
'ad_storage': 'denied',
'analytics_storage': 'denied',
'wait_for_update': 500,
})
}
})
}) |
endorfin
commented
Sep 9, 2025
just created a pull request #494 to solve this issue. Also added an example in the documentation showing how to use the new |
🔗 Linked issue
resolve#243
❓ Type of change
📚 Description
This PR improves GTM by adding a new callback to allow users to push anything into the datalayer before we push gtm.start . This way, users will be able to correctly set the consent mode