I'm trying to use the gtag.js library with nuxt.js. To do this, I installed the plugin https://github.com/nuxt-community/google-gtag, but it does not work for me
//nuxt.config
if(APP_ENV.gtag !== '') {
m.modules.push([
'@nuxtjs/google-gtag',{
id: APP_ENV.gtag,
config: {
'send_page_view': false,
'anonymize_ip': true
},
debug: false,
disableAutoPageTrack: false
}
])
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<a href="#" @click.stop.prevent="onAddCartItem(false), $gtag('event','add_to_cart',{items: [{id: 'P12345'}, {name: 'P12345'}, {list_name: 'Search Results'}, {brand: 'Google'}, {category: 'Apparel/T-Shirts'}]})">В корзину</a>
Vue 3 and Vite SupportWith Nuxt 3 being written in Vue 3, you get access to features like the Composition API, better module imports, and overall improved app performance. Nuxt 3 also comes with Vite support, which is backwards compatible with Nuxt 2.
One of Nuxt's biggest selling points is that you can use search engine optimization (SEO) to assist your application and raise your rank on Google.
Nuxt. js is an open-source, high-level JavaScript frontend framework that builds applications on top of Vue. js. It aims to make powerful and server-side rendered web apps while using Vue.
You can use vue-gtag and create a custom plugin to activate it within nuxt as below:
npm add vue-gtag
in your plugins folder create an empty file vue-gtag.js
import Vue from "vue";
import VueGtag from "vue-gtag";
Vue.use(VueGtag, {
config: { id: "UA-174146361-1" },
})
configure your nuxt.config.js to use Vue-Gtag
plugins: [
{src: '~/plugins/vue-gtag',}
]
Your App should now start to use vue-gtag with the following defaults:
{
pageTrackerTemplate: () => {},
onBeforeTrack: () => {},
onAfterTrack: () => {},
onReady: () => {},
enabled: true,
disableScriptLoad: false,
bootstrap: true,
globalObjectName: "gtag",
globalDataLayerName: "dataLayer",
pageTrackerEnabled: true,
pageTrackerScreenviewEnabled: false,
pageTrackerSkipSamePath: true,
defaultGroupName: "default",
includes: null,
config: null
}
Find more dtails from Vue-Gtag documentation here: https://matteo-gabriele.gitbook.io/vue-gtag/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With