Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@huang-julien
Copy link
Member

πŸ”— Linked issue

resolve #243

❓ 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

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

@vercel
Copy link

vercel bot commented Feb 16, 2025

The latest updates on your projects. Learn more about Vercel for Git β†—οΈŽ

Name Status Preview Comments Updated (UTC)
scripts-docs ❌ Failed (Inspect) Feb 17, 2025 9:51pm
scripts-playground βœ… Ready (Inspect) Visit Preview πŸ’¬ Add feedback Feb 17, 2025 9:51pm

@harlan-zw
Copy link
Collaborator

harlan-zw commented Feb 17, 2025

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 huang-julien marked this pull request as draft February 17, 2025 21:11
@huang-julien
Copy link
Member Author

Seems like we DO need to use a gtag() fn in order to configure the consent mode.... This makes 0 sense.

@huang-julien
Copy link
Member Author

I think if all EU users need the consent mode we should have a dedicated docs section for that πŸ€”

Are you talking about a whole page ?

@harlan-zw
Copy link
Collaborator

Nah just like a section in the GTM page, let's go with this for now. Thanks!

@harlan-zw harlan-zw merged commit 5e7d9b4 into main Feb 18, 2025
3 of 4 checks passed
@harlan-zw harlan-zw deleted the feat/gtm_onbeforestart branch February 18, 2025 20:30
@endorfin
Copy link
Contributor

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">
    <div v-if="!consent" class="fixed inset-0 bg-black/90 flex flex-col items-center justify-center space-y-4 z-50">
      <div class="absolute bottom-6 inset-x-6 bg-white dark:bg-gray-800 rounded-lg">
        <div class="w-full py-12 px-6 lg:px-6 xl:px-20 max-w-full mx-auto xl:max-w-content">
          <div class="headline-lg mb-6">
            {{ $t('common.cookieBanner.title') }}
          </div>

          <p class="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>
const consent = useCookie('consent')

function gtag() {
  window.dataLayer = window.dataLayer || [];
  dataLayer.push(arguments);
}

function handleConsent(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
Copy link
Collaborator

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">
    <div v-if="!consent" class="fixed inset-0 bg-black/90 flex flex-col items-center justify-center space-y-4 z-50">
      <div class="absolute bottom-6 inset-x-6 bg-white dark:bg-gray-800 rounded-lg">
        <div class="w-full py-12 px-6 lg:px-6 xl:px-20 max-w-full mx-auto xl:max-w-content">
          <div class="headline-lg mb-6">
            {{ $t('common.cookieBanner.title') }}
          </div>

          <p class="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>
const consent = useCookie('consent')

function gtag() {
  window.dataLayer = window.dataLayer || [];
  dataLayer.push(arguments);
}

function handleConsent(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>

Would you mind making an issue to track? πŸ™‚

@huang-julien
Copy link
Member Author

Yup doing that tomorrow πŸ‘πŸ‘

@endorfin
Copy link
Contributor

endorfin commented Sep 9, 2025

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
Copy link
Contributor

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 onBeforeGtmStart hook to configure consent mode.

Sign up for free to 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.

Add consent mode for Google Analytics registry script

4 participants