Example of Google Analytics #120
Replies: 5 comments 5 replies
-
Thank you for opening the discussion! I've generalized the example so that it does not appear as if it would show an actual recipe for Google Analytics. Also, the cookies should be removed automatically for optional cookies by cookie id when the consent is removed. |
Beta Was this translation helpful? Give feedback.
-
I struggle a lot to setup consent mode v2 from google. I use nuxt-gtag and want to fire the consent via cookie-control and can seem to find a way. Is there any help? |
Beta Was this translation helpful? Give feedback.
-
Is consent mode supported in this solution, or should we look elsewhere? |
Beta Was this translation helpful? Give feedback.
-
Okay. But the Consent Mode v2 is about getting data. And this works via a consent banner. My question is how to implement this in nuxt cookie control. in the gtag module is a way to integrate this: <script setup lang="ts">
const { gtag, initialize } = useGtag()
</script>
<template>
<button @click="initialize()">
Grant Consent
</button>
</template> export default defineNuxtConfig({
modules: ['nuxt-gtag'],
gtag: {
id: 'G-XXXXXXXXXX',
initCommands: [
// Setup up consent mode
['consent', 'default', {
ad_user_data: 'denied',
ad_personalization: 'denied',
ad_storage: 'denied',
analytics_storage: 'denied',
wait_for_update: 500,
}]
]
}
}) function allConsentGranted() {
const { gtag } = useGtag()
gtag('consent', 'update', {
ad_user_data: 'granted',
ad_personalization: 'granted',
ad_storage: 'granted',
analytics_storage: 'granted'
})
}
function consentGrantedAdStorage() {
const { gtag } = useGtag()
gtag('consent', 'update', {
ad_storage: 'granted'
})
}
// Invoke the consent function when a user interacts with your banner
consentGrantedAdStorage() // Or `allConsentGranted() Is there a way to get that consent working with cookie control? |
Beta Was this translation helpful? Give feedback.
-
maybe like this: nuxt.config.ts export default defineNuxtConfig({
modules: [
"nuxt-gtag",
"@dargmuesli/nuxt-cookie-control"
],
gtag: {
enabled: false,
id: 'XY-12345'
},
cookieControl: {
cookies: {
necessary: [],
optional: [
{
id: 'google-analytics',
name: 'google-analytics',
targetCookieIds: ['_ga', '_ga_', '_gcl_au'],
},
],
},
}, app.vue <template>
<NuxtPage />
<CookieControl />
</template>
<script setup lang="ts">
const {
cookiesEnabled,
cookiesEnabledIds,
isConsentGiven,
isModalActive,
moduleOptions,
} = useCookieControl()
const { gtag, initialize } = useGtag()
// example: react to a cookie being accepted
watch(
() => cookiesEnabledIds.value,
(current, previous) => {
if (
!previous?.includes('google-analytics') &&
current?.includes('google-analytics')
) {
// use nuxt-gtag
initialize()
}
},
{ deep: true },
)
</script> plugins/analytics.client.ts export default defineNuxtPlugin(nuxtApp => {
// example: initialization based on enabled cookies
const cookieControl = useCookieControl()
const { gtag, initialize } = useGtag()
if (cookieControl?.cookiesEnabledIds?.value?.includes('google-analytics')) {
initialize()
}
}) |
Beta Was this translation helpful? Give feedback.
-
Would be nice to have a full example of Google Analytics.
At the moment in the "Getting Started Guide" there is the section Cookies which contains:
{ description: { en: 'Used for cookie control.' }, id: 'ga', name: { en: 'Google Analytics' // you always have to specify a cookie name (in English) }, src: 'https://www.googletagmanager.com/gtag/js?id=<API-KEY>', targetCookieIds: ['cookie_control_consent', 'cookie_control_enabled_cookies'] }
This is a bit confusing because the targetCookieIds are not fitting the GA cookies Id's.
It also would be great to have an example on how the cookies for GA are deleted when the consent is removed.
Thanks a lot
Beta Was this translation helpful? Give feedback.
All reactions