Vue Google 标签管理器
Vue.js 的 Google 标签管理器简单实施
此插件将帮助您完成通用的 GTM 任务 .
注意: 如果您想要跟踪所有 Vuex 突变, 您可以使用 Vuex GTM 插件
所需资源
- 维. 页:1
- Google Tag 管理器账户. 发送数据到
可选依赖关系
- Vue 路由器 QQ 4. x - 为了使用屏幕自动跟踪
配置
npm install @gtm-support/vue-gtm
以下是示例配置:
import { createApp } from 'vue';
import { createGtm } from '@gtm-support/vue-gtm';
import router from './router';
const app = createApp(App);
app.use(router);
app.use(
createGtm({
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',
},
source: 'https://customurl.com/gtm.js', // Add your own serverside GTM script
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/vue-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);