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'
],
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
Use the composable useGtm of @gtm-support/vue-gtm
Example:
<template>
<button @click="triggerEvent">
Trigger event!
</button>
<button @click="triggerView">
Trigger event!
</button>
</template>
<script lang="ts" setup>
import { useGtm } from '@gtm-support/vue-gtm'
const gtm = useGtm()
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>
Modules options
The modules inherits the options of the plugin @gtm-support/vue-gtm, expect vueRouter
entry replaced by enableRouterSync
.
type ModuleOptions = {
enableRouterSync?: boolean
devtools: 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