Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Will this module use vue-gtag instead of vue-analytics? #111

Open
eggsy opened this issue May 5, 2021 · 2 comments
Open

Will this module use vue-gtag instead of vue-analytics? #111

eggsy opened this issue May 5, 2021 · 2 comments

Comments

@eggsy
Copy link

eggsy commented May 5, 2021

As the current module isn't maintained and throwing warnings to console, are you going to switch to vue-gtag?

image

@reinoldus
Copy link

reinoldus commented Jul 15, 2021

More and more I am getting the feeling that modules that just wrap another vue-module without any magic around it are not so useful, since configuring vue-gtag is pretty easy. It's a bit different for something like nuxt-sentry that seems more involved.

For really simple modules like this one it would probably be better to have a tutorial somewhere in the nuxt docs.

Here is an example on how to use vue-gtag wit nuxt + gdpr consent:

// init_gtag.js
import Vue from 'vue'
import VueGtag from 'vue-gtag'

export default (context, inject) => {
  Vue.use(
    VueGtag,
    {
      config: { id: 'xxx' },
      bootstrap: false, // load on consent
    },
    context.app.router
  )

  inject('gtag', Vue.$gtag) // this is the most important line to get gtag in nuxt-content
}
// consent.js
import CookiesEuBanner from 'cookies-eu-banner'
import { bootstrap } from 'vue-gtag'

export default (context, inject) => {
  CookiesEuBanner.prototype.setCookie = function (name, value) {
    const date = new Date()
    date.setTime(date.getTime() + this.cookieTimeout)

    document.cookie =
      name +
      '=' +
      value +
      ';expires=' +
      date.toGMTString() +
      ';path=/' +
      ';secure;SameSite=Lax'
  }

  function waitForElement(selector) {
    return new Promise(function (resolve, reject) {
      const element = document.querySelector(selector)

      if (element) {
        resolve(element)
        return
      }

      const observer = new MutationObserver(function (mutations) {
        mutations.forEach(function (mutation) {
          const nodes = Array.from(mutation.addedNodes)
          for (const node of nodes) {
            if (node.matches && node.matches(selector)) {
              observer.disconnect()
              resolve(node)
              return
            }
          }
        })
      })

      observer.observe(document.documentElement, {
        childList: true,
        subtree: true,
      })
    })
  }
  waitForElement('#cookies-eu-banner').then(function (element) {
    CookiesEuBanner(function () {
      // Your code to launch when user accept cookies

      const checkExist = setInterval(function () {
        try {
          bootstrap().then((gtag) => {
            console.log('gtag enabled')
            context.$gtag.set('user_properties', {
              logged_in: context.app.$auth.loggedIn,
            })
          })
          clearInterval(checkExist)
        } catch (e) {
          console.log(e)
        }
      }, 100)
    }, true)
  })
}

@marcelofern
Copy link

Definitely keen for the upgrade.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants