如何覆盖Nuxt的GTM模块中的GTM事件对象以进行页面跟踪?

时间:2019-11-04 07:39:41

标签: vue.js google-tag-manager nuxt.js

我正在查看模块中的这一行:

window[this.options.layer].push(to.gtm || { event: this.options.pageViewEventName, pageType: 'PageView', pageUrl: to.fullPath, routeName: to.name })

来源:https://github.com/nuxt-community/modules/blob/24427eb4dce4e516a68e6523297d3375a024df0b/packages/google-tag-manager/plugin.js#L24

任何想法都应如何提供在上一行中传递的to.gtm值?我尝试挂钩到组件中的beforeRouteEnter,但是即使更改to.gtm属性,传递给to.params的值似乎也无法通过。

beforeRouteEnter(to, from, next) {
    if (process.client) {
      console.log('BRE', to, window.dataLayer);
      if (to.params.test123) {
        next();
      } else {
        const newTo = Object.assign({}, to);
        newTo.gtm = { event: 'nuxtRoute3' };
        newTo.params.test123 = 'test';
        console.log(newTo);
        next(newTo);
      }
    } else {
      next();
    }
  }

0 个答案:

没有答案