Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can anyone help implementing Nuxt.js Google Tag Manager?

Hey i've built a Nuxt app and am having trouble with the package @nuxtjs/google-tag-manager package. Found below. The documentation is pretty light and I haven't found many example implementations out there. In my nuxt.config.js I have the following set.

['@nuxtjs/google-tag-manager', {
  id: process.env.GTM_ID,
  layer: 'dataLayer',
  pageTracking: true
}],

..but unfortunately am not getting any Page Views in Google Tag Manager

Does anyone have any ideas or experience in how to best implement GTM or what has worked for them?

Thanks in advance

like image 269
RuNpiXelruN Avatar asked Oct 19 '18 02:10

RuNpiXelruN


People also ask

Do you need to know programming to leverage Google Tag Manager?

You'll need to have a basic understanding of HTML, JavaScript, and CSS classes to fully use the power of Google Tag Manager. Within Google Tag Manager, you can set up something called a “trigger“.

Is Google Tag Manager difficult to learn?

Google Tag Manager is not “easy” to use without some technical knowledge or training (courses or self-taught). You have to have some technical knowledge to understand how to set up tags, triggers and variables. If you're dropping in Facebook pixels, you'll need some understanding of how Facebook tracking pixels work.

Is Google Tag Manager going away?

But it looks like Google Tag Manager isn't going away anytime soon: its popularity is growing, more and more people are using it as their work tool, the number of free and paid GTM resources is also constantly increasing.


1 Answers

I had a look at the package, inside https://github.com/nuxt-community/gtm-module/blob/master/lib/defaults.js there is this piece of code:

function startPageTracking(ctx) {
  ctx.app.router.afterEach((to) => {
    setTimeout(() => {
      ctx.$gtm.push(to.gtm || {
        routeName: to.name,
        pageType: 'PageView',
        pageUrl: '<%= options.routerBase %>' + to.fullPath,
        pageTitle: (typeof document !== 'undefined' && document.title) || '',
        event: '<%= options.pageViewEventName %>'
      })
    }, 250)
  })
}

From that, it looks like the datalayer looks like this:

{
routeName: to.name,
pageType: 'PageView',
pageUrl: '<%= options.routerBase %>' + to.fullPath,
pageTitle: (typeof document !== 'undefined' && document.title) || '',
event: '<%= options.pageViewEventName %>' //default is 'nuxtRoute'
}

The default name for the event is 'nuxtRoute'. Thus in GTM, you'll get to define a custom event trigger to trigger on the "nuxtRoute' event. Like so: enter image description here

Then you want to create two DataLayer variables in GTM that will capture pageUrl(Please note the camel case) and possibly routeName, I say routeName is optional depends on if you're changing/updating the of the document or not. enter image description here

Then create your Google Analytics tag in GTM. Make sure it is the "pageview" type, then check the "enable overriding settings in this tag" checkbox, under "more settings > fields to set" type in "page" for fieldname and for "value" reference that variable we created. If you want to set the page title using the to.name variable just use the "title" field. Make sure you add the nuxt route trigger as well under "triggering". enter image description here

Save and publish everything or run it in preview mode and you should see the pageviews some through.

like image 123
XTOTHEL Avatar answered Sep 24 '22 15:09

XTOTHEL