Search code examples
nuxt.jsgoogle-tag-managergtag.js

How to use Google tags in Nuxt.js


I'm on Nuxt.js 2.13 and I want to use Google tags in my project.

But there are several things that are not clear to me!

First: there are two packages by pi0 in Nuxt.js community, @nuxtjs/gtm and @nuxtjs/google-gtag. Which one should I use?

Second: How can I use dataLayer.push({'varName':'value'}) with these packages? As in their documentation they only instructed about push('event').


Solution

  • About the first point:

    As explained in the release notes:

    This is a major rewrite of @nuxtjs/google-tag-manager after 3 years of contributions and feedback from users.


    About the second point, you have to use the $gtm.push method to make your push event or data.

    For example, in your pages/index.vue file:

    <script>
    export default {
      middleware ({ $gtm }) {
        $gtm.push({ 'varName': 'value' })
      }
    }
    </script>
    

    See another full example with configuration at https://github.com/nuxt-community/gtm-module/tree/master/example.