MatteoGabriele / vue-gtag

Global Site Tag plugin for Vue (gtag.js)
https://matteo-gabriele.gitbook.io/vue-gtag/
MIT License
864 stars 64 forks source link

In Nuxt this.$gtag is undefined in actions of vuex store #273

Closed julianfox closed 3 years ago

julianfox commented 3 years ago

Description

In Nuxt this.$gtag is undefined in actions of vuex store

Expected behavior

Be able to access gtag in actions of vuex store

Actual behavior

$gtag is undefined, work well in components

Environment

Run this command in the project folder and fill in their results:

npm ls vue-gtag: vue-gtag@1.11.0

Then, specify:

  1. Operating system: Mac Os 10.14
  2. Browser and version: Google chrome: Version 88.0.4324.182 (Official Build) (x86_64)

Reproducible Demo

async signupWithLocal ({ dispatch, rootState }, payload) {
    const { $auth, $axios, $gtag } = this

    ...

    await $auth.setUserToken(data.token)

    try {
      this.$gtag.set({
        'userId': $auth.user._id
      })
    } catch (e) {
      console.log(e)
    }
}
pierresaid commented 3 years ago

Use vm in nuxt

this._vm.$gtag
MatteoGabriele commented 3 years ago

vue-gtag is not available in the vuex context, you should import it like this

import { set } from 'vue-gtag'

set({ userId: 'foo' })

check all methods here https://matteo-gabriele.gitbook.io/vue-gtag/api

Coinhexa commented 1 month ago

Use vm in nuxt

this._vm.$gtag

undefined inside a nuxt 2 plugin