Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
262 views
in Technique[技术] by (71.8m points)

vue.js - Gtag in Nuxt.js

I'm really facing trouble configuring gtag in my nuxt js app.

I follow this guide:

https://www.carlcassar.com/articles/add-google-analytics-to-a-nuxt-js-app/

This is my plugin :

    import Vue from 'vue';
    import VueGtag from 'vue-gtag';
    
    Vue.use(VueGtag, {
        config: { id: 'G-*********' },
        appName: 'app-name',
    });

And this is how i Load it in nuxt.confing.js

      plugins: [
        "@/plugins/aos.client",
        "@/plugins/progress-path",
        "@/plugins/vue-input-ui",
        '@plugins/vue-js-modal.js',
        "@/plugins/paypal",
        "@/plugins/autocomplete",
        "@/plugins/lazy-load",
        {
          src: './plugins/gtag.js',
          mode: 'client'
        },
      ],

But I'm really facing trouble not getting anything to analytics console.

This is the gtag from google

    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=G-*********"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());
    
      gtag('config', 'G-*********');
    </script>

Where am I wrong?

Thanks everyone


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

From the tutorial that you are following: Track page views.

You need to wrap the Vue.use function in the export default in your plugin and pass the router:

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

export default ({ app }) => {
  Vue.use(VueGtag, {
    config: { id: 'G-*********' },
    appName: 'app-name',
  }, app.router);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...