nuxt-community / google-analytics-module

Google Analytics Module for Nuxt 2
https://google-analytics.nuxtjs.org/
MIT License
634 stars 53 forks source link

$ga is missing from router middleware`s context #123

Open DevineDecrypter opened 2 years ago

DevineDecrypter commented 2 years ago

I'm trying to use $ga in a middleware, but it doesn't seem to contain it. I tried using context.$ga and context.app.$ga but they are undefined. I also tried putting '@nuxtjs/google-analytics' in both buildModules and modules in nuxt.config.js but didn't work either.

export default function (context) {
  console.log(context.$ga)
  console.log(context.app.$ga)
}

both logs are undefined.

BenGoetemann commented 2 years ago

i have the same issue

liuther9 commented 1 year ago

@DevineDecrypter @BenGoetemann did you guys solve it? This.$ga is undefined for me

BenGoetemann commented 1 year ago

No I used vue gtag instead. If you're using Nuxt 2 you have to use vue gtag v1. I didnt use it for Nuxt 3.

If your using Nuxt 2 you have to register it as plugin in your nuxt.config. This is the plugin code:

import Vue from 'vue';
import VueGtag from 'vue-gtag';

export default ({
  app, env
}) => {

  Vue.use(VueGtag, {
    bootstrap: false,
    config: {
      id: env.ANALYTICS_KEY,
      params: {
        anonymize_ip: true
      }
    },
    appName: 'your app name',
  }, app.router);
}

... and then you can use it via this.$gtag inside your components.

Hope this helps!