🔸 Google Tag integration for Nuxt: Google Analytics, Google Ads and more
initCommands
option - by @johannschopplich in https://github.com/johannschopplich/nuxt-gtag/issues/50 (e83d2)
No significant changes
The latest version of Nuxt Gtag improves the API and adds support for Google Tag Consent Mode v2. This migration guide will help you to upgrade your project from the previous version to the latest one.
initialConsent
This module option has been renamed to enabled
. If you have set initialConsent
to false
in your configuration, you should rename it to enabled
.
export default defineNuxtConfig({
modules: ['nuxt-gtag'],
gtag: {
id: 'G-XXXXXXXXXX',
- initialConsent: false
+ enabled: false
}
})
grantConsent
This function has been renamed to initialize
. If you have used grantConsent
in your project, you should rename it to initialize
.
const {
gtag,
- grantConsent
+ initialize
} = useGtag()
- grantConsent()
+ initialize()
revokeConsent
Before v2, grantConsent
and revokeConsent
were used to enable and disable analytics. But the abstraction was not clear, because they didn't work for other Google products like Google Ads.
Now, you can use enableAnalytics
and disableAnalytics
to enable and disable analytics.
[!TIP] Follows the Google Consent Mode v2 specification.
Set a default value for each consent type you are using. By default, no consent mode values are set.
The following example sets multiple consent mode parameters to denied by default:
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,
}]
]
}
})
After a user indicates their consent choices, update relevant parameters to granted
:
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()`
initialConsent
to enabled
- by @johannschopplich (b342e)
grantConsent
to initialize
- by @johannschopplich (3bd75)
disableAnalytics
and enableAnalytics
- by @johannschopplich (998fe)
initCommands
- by @johannschopplich (d9706)
config
to empty object - by @johannschopplich in https://github.com/johannschopplich/nuxt-gtag/issues/47 (e9947)
useTrackEvent
param types fixes #28 - by @johannschopplich in https://github.com/johannschopplich/nuxt-gtag/issues/28 (d57cb)