zadigetvoltaire / nuxt-gtm

Nuxt 3 module for Google Tag Manager
https://www.npmjs.com/package/@zadigetvoltaire/nuxt-gtm
MIT License
59 stars 3 forks source link

adding lazy loading #10

Open s00d opened 1 year ago

s00d commented 1 year ago

adding lazy loading, after add you can use custom gtm plugin, example to change the tag after loading the module

// pugins/gtm.ts
import {defineNuxtPlugin, useRouter} from 'nuxt/app'
import { createGtm, type VueGtmUseOptions } from '@gtm-support/vue-gtm'

export default defineNuxtPlugin((nuxt) => {
  if (process.client) {
    const options = nuxt.$config.public.gtm

    const router = useRouter()

    const pluginOptions: VueGtmUseOptions = {
      ...options,
      vueRouter: options.enableRouterSync && router ? router as VueGtmUseOptions['vueRouter'] : undefined,
      id: 'GTM-YYYYYY'
    }

    nuxt.vueApp.use(createGtm(pluginOptions))
  }
})