Nuxt GTM
Nuxt Google Tag Manager module integrated with the Nuxt Devtools for Nuxt 3.
This library is an Nuxt 3 module wrapper of the @gtm-support/vue-gtm plugin
Quick Setup
- Add
@zadigetvoltaire/nuxt-gtm
dependency to your project
pnpm add -D @zadigetvoltaire/nuxt-gtm
yarn add --dev @zadigetvoltaire/nuxt-gtm
npm install --save-dev @zadigetvoltaire/nuxt-gtm
- Add
@zadigetvoltaire/nuxt-gtm
to the modules
section of nuxt.config.ts
export default defineNuxtConfig({
modules: [
'@zadigetvoltaire/nuxt-gtm'
],
})
- Add configuration in
nuxtConfig.gtm
or in nuxtConfig.runtimeConfig.public.gtm
This module supports 2 ways of configuration:
- Directly in key
gtm
of the Nuxt config - In public runtimeConfig: useful to override the config with environment variables and handle multiple environments
export default defineNuxtConfig({
...
gtm: {
id: 'GTM-xxxxxx',
queryParams: {
gtm_auth: 'AB7cDEf3GHIjkl-MnOP8qr',
gtm_preview: 'env-4',
gtm_cookies_win: 'x',
},
defer: false,
compatibility: false,
nonce: '2726c7f26c',
enabled: true,
debug: true,
loadScript: true,
enableRouterSync: true,
ignoredViews: ['homepage'],
trackOnNextTick: false,
devtools: true,
}
...
runtimeConfig: {
public: {
gtm: {
id: 'GTM-xxxxxx',
queryParams: {
gtm_auth: 'AB7cDEf3GHIjkl-MnOP8qr',
gtm_preview: 'env-4',
gtm_cookies_win: 'x',
},
defer: false,
compatibility: false,
nonce: '2726c7f26c',
enabled: true,
debug: true,
loadScript: true,
enableRouterSync: true,
ignoredViews: ['homepage'],
trackOnNextTick: false,
devtools: true,
}
}
}
})
Documentation
Please refer to the @gtm-support/vue-gtm documentation
Composition API - useGtm composable
Example:
<template>
<button @click="triggerEvent">
Trigger event!
</button>
<button @click="triggerView">
Trigger event!
</button>
</template>
<script lang="ts" setup>
const gtm = useGtm() // auto-imported by the module
function triggerEvent() {
gtm.trackEvent({
event: 'event name',
category: 'category',
action: 'click',
label: 'My custom component trigger',
value: 5000,
noninteraction: false,
})
}
function triggerView() {
gtm.trackView('Home', '/')
}
</script>
Options API
export default {
methods: {
triggerEvent() {
this.$gtm.trackEvent({
event: 'event name',
category: 'category',
action: 'click',
label: 'My custom component trigger',
value: 5000,
noninteraction: false,
})
}
}
}
Modules options
The modules inherit the options of the plugin @gtm-support/vue-gtm, except vueRouter
entry replaced by enableRouterSync
.
type ModuleOptions = {
devtools: boolean
enableRouterSync?: boolean
vueRouterAdditionalEventData?: (to: RouteLocationNormalized, from: RouteLocationNormalized) => Record<string, any> | Promise<Record<string, any>>;
ignoredViews?: string[] | ((to: RouteLocationNormalized, from: RouteLocationNormalized) => boolean);
trackOnNextTick?: boolean;
id: string | string[] | GtmIdContainer[];
queryParams?: GtmQueryParams;
defer?: boolean;
compatibility?: boolean;
nonce?: string;
source?: string;
enabled?: boolean;
debug?: boolean;
loadScript?: boolean;
trackViewEventProperty?: string;
}
That's it! You can now use Nuxt GTM in your Nuxt app ✨
Contributing
make start
pnpm dev
pnpm play:prod
pnpm lint
pnpm test
pnpm test:watch
Release new version
- Execute release command
⚠ This command should be executed only on the main branch
This command will:
- Generate the CHANGELOG.md and push it with a release commit
- Bump the package version
- Create and push the new tag
- Create a github release to trigger the library publish pipeline
pnpm release
© Zadig&Voltaire is a registered trademark of ZV FRANCE