google-analytics-module icon indicating copy to clipboard operation
google-analytics-module copied to clipboard

$ga is missing from router middleware`s context

Open DevineDecrypter opened this issue 4 years ago • 3 comments

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.

DevineDecrypter avatar Dec 02 '21 23:12 DevineDecrypter

i have the same issue

BenGoetemann avatar Apr 20 '22 14:04 BenGoetemann

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

liuther9 avatar Feb 10 '23 11:02 liuther9

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!

BenGoetemann avatar Feb 10 '23 16:02 BenGoetemann