Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use gtag.js with nuxt.js?

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>
like image 766
Vladimir Fedyanin Avatar asked May 27 '19 09:05

Vladimir Fedyanin


People also ask

Can I use vue3 with Nuxt?

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.

Is Nuxt good for SEO?

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.

Is NUXT JS frontend or backend?

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.


1 Answers

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/

like image 115
Isaac T Avatar answered Oct 04 '22 00:10

Isaac T