@gtm-support/vue2-gtm

中文文档
2.0.0发布于 2022/10/07MITTypeScriptESM + CJS近 30 天下载 6.1万

Vue 的 Google 标签管理器的简单执行

中文使用文档
文档目录9 节
  1. 与您的路由器同步 gtm
  2. 以页面视图事件传递自定义属性
  3. 以页面查看事件传递动态属性
  4. 使用成分 API
  5. 方法
  6. 启用插件
  7. 调试插件
  8. IE 11支助
  9. 贷项

Vue Google 标签管理器

*** 欢迎捐款者 ***

Google Tag Manager Vue.js

Vue.js 的 Google 标签管理器简单实施


license: Apache-2.0 NPM package downloads code style: Prettier Build Status

此插件将帮助您完成通用的 GTM 任务 .

注意: 如果您想要跟踪所有 Vuex 突变, 您可以使用 Vuex GTM 插件

如果您想要 Vue 3 兼容性, 请使用软件包 @gtm-support/vue-gtm (英语)。 。 。 .

所需资源

  • 维. >= 2.0.0 < 3.0.0
  • Google Tag 管理器账户. 发送数据到

可选依赖关系

  • Vue 路由器 >= 3.x < 4.x - 为了使用屏幕自动跟踪

配置

npm install @gtm-support/vue2-gtm 或者说 yarn add @gtm-support/vue2-gtm 如果使用的话 Yarn 软件包管理器

以下是示例配置:

import VueGtm from '@gtm-support/vue2-gtm';
import VueRouter from 'vue-router';
const router = new VueRouter({ routes, mode, linkActiveClass });

Vue.use(VueGtm, {
  id: 'GTM-xxxxxx', // Your GTM single container ID, array of container ids ['GTM-xxxxxx', 'GTM-yyyyyy'] or array of objects [{id: 'GTM-xxxxxx', queryParams: { gtm_auth: 'abc123', gtm_preview: 'env-4', gtm_cookies_win: 'x'}}, {id: 'GTM-yyyyyy', queryParams: {gtm_auth: 'abc234', gtm_preview: 'env-5', gtm_cookies_win: 'x'}}], // Your GTM single container ID or array of container ids ['GTM-xxxxxx', 'GTM-yyyyyy']
  queryParams: {
    // Add URL query string when loading gtm.js with GTM ID (required when using custom environments)
    gtm_auth: 'AB7cDEf3GHIjkl-MnOP8qr',
    gtm_preview: 'env-4',
    gtm_cookies_win: 'x',
  },
  defer: false, // Script can be set to `defer` to speed up page load at the cost of less accurate results (in case visitor leaves before script is loaded, which is unlikely but possible). Defaults to false, so the script is loaded `async` by default
  compatibility: false, // Will add `async` and `defer` to the script tag to not block requests for old browsers that do not support `async`
  nonce: '2726c7f26c', // Will add `nonce` to the script tag
  enabled: true, // defaults to true. Plugin can be disabled by setting this to false for Ex: enabled: !!GDPR_Cookie (optional)
  debug: true, // Whether or not display console logs debugs (optional)
  loadScript: true, // Whether or not to load the GTM Script (Helpful if you are including GTM manually, but need the dataLayer functionality in your components) (optional)
  vueRouter: router, // Pass the router instance to automatically sync with router (optional)
  ignoredViews: ['homepage'], // Don't trigger events for specified router names (optional)
  trackOnNextTick: false, // Whether or not call trackView in Vue.nextTick
});

这将在页面中注入标签管理器脚本, 除非 enabled 设置为 false。 。 。 . 在这种情况下,在呼叫时会注射 this.$gtm.enable(true) 这是第一次.

记住要启用历史更改触发器, 路由器更改要通过 GTM 发送 .

文档

配置完成后,您可以在您的组件中访问 vue gtm 实例 :

export default {
  name: 'MyComponent',
  data() {
    return {
      someData: false,
    };
  },
  methods: {
    onClick() {
      this.$gtm.trackEvent({
        event: null, // Event type [default = 'interaction'] (Optional)
        category: 'Calculator',
        action: 'click',
        label: 'Home page SIP calculator',
        value: 5000,
        noninteraction: false, // Optional
      });
    },
  },
  mounted() {
    this.$gtm.trackView('MyScreenName', 'currentPath');
  },
};

通过的变量以 GTM 数据层映射如下

dataLayer.push({
  event: event || 'interaction',
  target: category,
  action: action,
  'target-properties': label,
  value: value,
  'interaction-type': noninteraction,
  ...rest,
});

当您输入时, 您也可以访问此实例 Vue 通过使用 Vue.gtm。当您处于存储模块或某个部件范围以外的地方时,它特别有用.

也可以将完全自定义的数据发送到GTM,只用手动推到 dataLayer数字 :

if (this.$gtm.enabled()) {
  window.dataLayer?.push({
    event: 'myEvent',
    // further parameters
  });
}

与您的路由器同步 gtm

感谢vue-router卫士,你可以自动发送关于路由器更改的新屏幕视图! 要使用此特性, 您只需要在插件初始化时注入路由器实例 .

此特性将根据优先级规则生成视图名 :

  • 如果您定义了您命名的路径的元字段 gtm 这将为视图名称取出此字段的价值.
  • 否则,如果插件没有值 meta.gtm 它会回到内部路线名称.

多数时候第二件案子已经足够了,但有时你想对寄出的东西有更大的控制力,这就是第一个规则闪耀的地方.

示例

const myRoute = {
  path: 'myRoute',
  name: 'MyRouteName',
  component: SomeComponent,
  meta: { gtm: 'MyCustomValue' },
};

这将使用 MyCustomValue 作为取景名.

以页面视图事件传递自定义属性

如果您的 GTM 设置预计自定义数据将作为页面视图的一部分发送, 您可以通过 meta.gtmAdditionalEventData 属性.

示例

const myRoute = {
  path: 'myRoute',
  name: 'myRouteName',
  component: SomeComponent,
  meta: { gtmAdditionalEventData: { routeCategory: 'INFO' } },
};

这个送来的财产 routeCategory 以“INFO”作为该路由的页面查看事件的一部分.

注意这些属性 event, (中文(简体) ), content-name 财务报告和审定财务报表 content-view-name 总是被压倒.

以页面查看事件传递动态属性

如果您需要通过动态属性作为页面视图的一部分,您可以设置一个在导航后获取自定义数据的回调 .

示例

createGtm({
  // ...other options
  vueRouter: router,
  vueRouterAdditionalEventData: () => ({
    someComputedProperty: computeProperty(),
  }),
});

此计算并发送财产 someComputedProperty ,作为您在每次导航后页面查看事件的一部分.

请注意, 路由级别上同一名称的地产将覆盖此功能 .

使用成分 API

要使用此插件, 包含组件 API( 在您的内) setup 方法),您可以直接调用自定义可调用 useGtm。 。 。 .

示例

<template>
  <button @click="triggerEvent">Trigger event!</button>
</template>

<script>
import { useGtm } from '@gtm-support/vue2-gtm';

export default {
  name: 'MyCustomComponent',

  setup() {
    const gtm = useGtm();

    function triggerEvent() {
      gtm.trackEvent({
        event: 'event name',
        category: 'category',
        action: 'click',
        label: 'My custom component trigger',
        value: 5000,
        noninteraction: false,
      });
    }

    return {
      triggerEvent,
    };
  },
};
</script>

方法

启用插件

检查插件是否启用

this.$gtm.enabled();

启用插件

this.$gtm.enable(true);

禁用插件

this.$gtm.enable(false);

调试插件

检查插件是否处于调试模式

this.$gtm.debugEnabled();

启用调试模式

this.$gtm.debug(true);

禁用调试模式

this.$gtm.debug(false);

IE 11支助

如果您真的需要支持 IE 11 这样的浏览器, 您需要配置 transpileDependencies: ['@gtm-support/core'] 在你身边 vue.config.js。 。 。 .
见 gtm-support/core #20(评论)

贷项

版本与发布记录

@gtm-support/vue2-gtm 共发布 9 个版本,首次发布于 2021/04/25;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.0.0latest2022/10/074 年前
1.3.02022/05/164 年前
1.2.12021/11/184 年前
1.2.02021/09/205 年前
1.1.02021/07/045 年前
1.0.02021/04/305 年前

其他发布通道:alpha → 1.0.0-alpha.3。

关于 @gtm-support/vue2-gtm 的常见问题

@gtm-support/vue2-gtm 是什么?

Vue 的 Google 标签管理器的简单执行

如何安装 @gtm-support/vue2-gtm?

在项目目录运行 npm install @gtm-support/vue2-gtm,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:vue(^2.7.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@gtm-support/vue2-gtm 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@gtm-support/vue2-gtm 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@gtm-support/vue2-gtm 可以商用吗?

@gtm-support/vue2-gtm 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@gtm-support/vue2-gtm 的下载量有多少?

根据 npm 官方下载统计,@gtm-support/vue2-gtm 在 2026/10/17 – 2026/11/15 这 30 天内被下载 60,533 次,日均约 2,018 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@gtm-support/vue2-gtm 还在维护吗?

最新版本 2.0.0 发布于 2022/10/07(4 年前);至今共发布 9 个版本,其中过去 12 个月发布了 0 个;首次发布于 2021/04/25。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。