Home > Blockchain >  How to integrate Google Analytics into your Nuxt.js App
How to integrate Google Analytics into your Nuxt.js App

Time:10-01

I have made nuxt.js app and I want to integrate Google Analytics. I have found @nuxtjs/google-analytics package, installed it and did everything like it was in documentation, but it still doesn't work.

Here is nuxt.config.js

module.exports = {
...
modules: [
    // Doc: https://axios.nuxtjs.org/usage
    '@nuxtjs/axios',
    '@nuxtjs/pwa',
    // 'nuxt-basic-auth-module',
    '@nuxtjs/google-analytics',
    '@nuxtjs/recaptcha'
  ],
  googleAnalytics: {
    id: process.env.GA_ID || ''
  },
}

When page is loading in browser networking I can see analytics.js, but on the analytics.google.com page I can see nothing, even there is no online users.

Also, I tried to add event on button click, and here is function:

async showLogin() {
  this.$ga.event({
    eventCategory: 'login',
    eventAction: 'login',
    eventLabel: 'login',
    eventValue: 1
  });
  await this.$router.push(`/${this.$store.state.locale}/login`)
},

This is what happens, when user clicks login button, and, as I said, there is no info on analytics.google.com page and there is no info in browser networking.

So, what do I do wrong? If someone have already worked with Nuxt and Google Analytics, how did you integrate it into your app? I didn't find a lot of information on the Internet on this issue, but I tried everything I found, and it doesn't work.

CodePudding user response:

nuxt.config.js

const testingAnalytics = false

module.exports = {
  buildModules: [
    '@nuxtjs/google-analytics'
  ],
  googleAnalytics: {
    id: '', 
    /* 
      If testingAnalytics  === true than send test hits

      If testingAnalytics  === false than enable analytics
      only when process.env.NODE_ENV === 'production'
    */
    dev: testingAnalytics ? true : process.env.NODE_ENV !== 'production',
    debug: testingAnalytics
      ? {
        enabled: true,
        sendHitTask: true
      }
      : {}
  }
}
  • Related