gtm-module icon indicating copy to clipboard operation
gtm-module copied to clipboard

A huge setTimeout delay of 250ms

Open proArtex opened this issue 5 years ago • 2 comments

Triggering tags has a delay, that may impact data gathering (especially first load call). I have found this peace of code:

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

And I do understand why setTimeout(250) is here, because of DOM re-rendering delay. As soon as I set it to 0, gtm pushes a title of a previous page instead of a current one. Is there more reliable way of catching it?

proArtex avatar May 29 '20 07:05 proArtex

for a reference:

  1. https://github.com/nuxt-community/analytics-module/issues/8#issuecomment-573637447
  2. https://github.com/MatteoGabriele/vue-analytics/commit/e0a92e301aa992a64936075d5da141c2c0821b38

everyx avatar Sep 02 '20 06:09 everyx

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

nuxt.config.js

export default {
  ......
  features: {
    transitions: false
  },
  vueMeta: {
    refreshOnceOnNavigation: true
  },
  ......
}

app/router.scrollBehavior.js

export default function () {
  return { x: 0, y: 0 };
}

related https://github.com/nuxt-community/analytics-module/issues/8#issuecomment-573637447 https://nuxtjs.org/api/configuration-router#scrollbehavior

wusongliang avatar Sep 05 '20 02:09 wusongliang