google-tagmanager
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGoogle Tag Manager
Google Tag Manager
Overview
概述
This skill provides comprehensive expertise for Google Tag Manager (GTM), covering container setup, tag configuration, triggers, variables, data layer implementation, debugging, custom templates, and API automation. Whether you are implementing a new GTM setup, optimising an existing configuration, or troubleshooting issues, this skill has you covered.
本技能提供关于Google Tag Manager(GTM)的全面专业指导,涵盖容器设置、代码标签配置、触发器、变量、数据层实施、调试、自定义模板以及API自动化。无论你是要搭建新的GTM环境、优化现有配置,还是排查问题,本技能都能为你提供支持。
When to Use This Skill
适用场景
Invoke this skill when:
- Setting up a new GTM container
- Configuring tags (GA4, Google Ads, Facebook Pixel, etc.)
- Creating triggers (page views, clicks, form submissions, etc.)
- Defining variables (data layer, custom JavaScript, constants, etc.)
- Implementing the data layer for e-commerce or custom events
- Debugging GTM issues using Preview mode or Tag Assistant
- Building custom templates with sandboxed JavaScript
- Automating GTM operations via the REST API
- Optimising container performance and organisation
- Ensuring privacy compliance and consent management
在以下场景中调用本技能:
- 搭建新的GTM容器
- 配置代码标签(GA4、Google Ads、Facebook Pixel等)
- 创建触发器(页面浏览、点击、表单提交等)
- 定义变量(数据层、自定义JavaScript、常量等)
- 为电商或自定义事件实施数据层
- 使用预览模式或Tag Assistant调试GTM问题
- 利用沙箱JavaScript构建自定义模板
- 通过REST API自动化GTM操作
- 优化容器性能与组织结构
- 确保隐私合规与同意管理
Quick Start
快速入门
Container Setup
容器设置
- Create a GTM account at tagmanager.google.com
- Create a container (Web, iOS, Android, or Server)
- Install the container snippet on your website
- Configure tags, triggers, and variables
- Test in Preview mode
- Publish
See setup.md for detailed installation instructions.
- 前往tagmanager.google.com创建GTM账号
- 创建容器(Web、iOS、Android或Server端)
- 在你的网站上安装容器代码片段
- 配置代码标签、触发器和变量
- 在预览模式下测试
- 发布配置
详见setup.md获取详细安装说明。
Basic Tag Configuration
基础代码标签配置
javascript
// Example: GA4 Configuration Tag
Tag Type: Google Analytics: GA4 Configuration
Measurement ID: G-XXXXXXXXXX
Trigger: All PagesSee tags.md for comprehensive tag documentation.
javascript
// Example: GA4 Configuration Tag
Tag Type: Google Analytics: GA4 Configuration
Measurement ID: G-XXXXXXXXXX
Trigger: All Pages详见tags.md获取全面的代码标签文档。
Data Layer Push
数据层推送
javascript
// Push custom event to data layer
window.dataLayer = window.dataLayer || [];
dataLayer.push({
'event': 'custom_event',
'category': 'engagement',
'action': 'button_click',
'label': 'CTA Button'
});See datalayer.md for data layer patterns.
javascript
// Push custom event to data layer
window.dataLayer = window.dataLayer || [];
dataLayer.push({
'event': 'custom_event',
'category': 'engagement',
'action': 'button_click',
'label': 'CTA Button'
});详见datalayer.md获取数据层模式说明。
Core Concepts
核心概念
Tags, Triggers, and Variables
代码标签、触发器与变量
Tags are snippets of code that execute on your site (e.g., GA4, Google Ads, Facebook Pixel).
Triggers define when tags fire (e.g., page views, clicks, form submissions).
Variables capture dynamic values for use in tags and triggers (e.g., page URL, click text, data layer values).
代码标签是在你的网站上执行的代码片段(如GA4、Google Ads、Facebook Pixel)。
触发器定义代码标签的触发时机(如页面浏览、点击、表单提交)。
变量用于捕获供代码标签和触发器使用的动态值(如页面URL、点击文本、数据层值)。
How They Work Together
协同工作机制
User Action --> Trigger Fires --> Tag Executes --> Data Sent
^ |
| v
+--- Variables provide values -------+用户操作 --> 触发器触发 --> 代码标签执行 --> 数据发送
^ |
| v
+--- 变量提供所需值 ----------------+Topic Reference Guide
主题参考指南
Container Setup and Installation
容器设置与安装
For container creation, snippet installation, and initial configuration:
- Reference: setup.md
- Topics: Container types, snippet placement, noscript fallback, verification
关于容器创建、代码片段安装和初始配置:
- 参考文档:setup.md
- 涵盖主题:容器类型、代码片段放置、noscript备选方案、验证
Tag Configuration
代码标签配置
For configuring analytics, advertising, and custom tags:
- Reference: tags.md
- Topics: GA4 tags, Google Ads conversions, Facebook Pixel, custom HTML, tag sequencing
关于分析、广告和自定义代码标签的配置:
- 参考文档:tags.md
- 涵盖主题:GA4标签、Google Ads转化追踪、Facebook Pixel、自定义HTML、标签执行顺序
Trigger Types
触发器类型
For setting up firing conditions:
- Reference: triggers.md
- Topics: Page views, clicks, form submissions, custom events, trigger groups, blocking triggers
关于触发条件的设置:
- 参考文档:triggers.md
- 涵盖主题:页面浏览、点击、表单提交、自定义事件、触发器组、阻止触发器
Variable Configuration
变量配置
For capturing and formatting data:
- Reference: variables.md
- Topics: Built-in variables, data layer variables, custom JavaScript, URL variables, lookup tables
关于数据的捕获与格式化:
- 参考文档:variables.md
- 涵盖主题:内置变量、数据层变量、自定义JavaScript、URL变量、查找表
Data Layer Implementation
数据层实施
For implementing the data layer:
- Reference: datalayer.md
- Topics: Data layer structure, e-commerce tracking, SPA tracking, best practices
关于数据层的实施:
- 参考文档:datalayer.md
- 涵盖主题:数据层结构、电商追踪、单页应用(SPA)追踪、最佳实践
Debugging and Testing
调试与测试
For troubleshooting GTM implementations:
- Reference: debugging.md
- Topics: Preview mode, Tag Assistant, debug console, common issues, testing workflows
关于GTM实施的故障排查:
- 参考文档:debugging.md
- 涵盖主题:预览模式、Tag Assistant、调试控制台、常见问题、测试流程
Best Practices
最佳实践
For naming conventions, performance, and security:
- Reference: best-practices.md
- Topics: Naming conventions, container organisation, performance optimisation, security, deployment strategies
关于命名规范、性能与安全:
- 参考文档:best-practices.md
- 涵盖主题:命名规范、容器组织、性能优化、安全、部署策略
Custom Templates
自定义模板
For building custom tag and variable templates:
- Reference: custom-templates.md
- Topics: Sandboxed JavaScript, template APIs, permissions, testing, publishing to Gallery
关于构建自定义代码标签和变量模板:
- 参考文档:custom-templates.md
- 涵盖主题:沙箱JavaScript、模板API、权限、测试、发布至模板库
API Automation
API自动化
For programmatic GTM management:
- Reference: api.md
- Topics: Authentication, REST API operations, bulk operations, backup/restore, CI/CD integration
关于GTM的程序化管理:
- 参考文档:api.md
- 涵盖主题:身份验证、REST API操作、批量操作、备份/恢复、CI/CD集成
Common Workflows
常见工作流程
Implement GA4 Page View Tracking
实施GA4页面浏览追踪
- Create GA4 Configuration tag with Measurement ID
- Set trigger to "All Pages"
- Test in Preview mode
- Verify in GA4 DebugView
- Publish
- 创建带有Measurement ID的GA4配置标签
- 设置触发器为“所有页面”
- 在预览模式下测试
- 在GA4 DebugView中验证
- 发布配置
Track Form Submissions
追踪表单提交
- Create Form Submission trigger
- Create GA4 Event tag with event name
form_submit - Add form ID/name as event parameter
- Test submission in Preview mode
- Publish
- 创建表单提交触发器
- 创建事件名称为的GA4事件标签
form_submit - 将表单ID/名称添加为事件参数
- 在预览模式下测试提交操作
- 发布配置
Implement E-commerce Tracking
实施电商追踪
- Implement data layer with e-commerce events
- Create data layer variables for product data
- Create GA4 Event tags for each e-commerce event
- Map data layer variables to event parameters
- Test complete purchase flow
- Publish
- 实施包含电商事件的数据层
- 为产品数据创建数据层变量
- 为每个电商事件创建GA4事件标签
- 将数据层变量映射到事件参数
- 测试完整的购买流程
- 发布配置
Debug Tag Not Firing
排查标签未触发问题
- Enable Preview mode
- Perform the action that should fire the tag
- Check "Tags Not Fired" section
- Review trigger conditions in Variables tab
- Check data layer for required values
- Fix conditions and retest
See debugging.md for detailed debugging workflows.
- 启用预览模式
- 执行应触发标签的操作
- 查看“未触发的标签”区域
- 在变量标签页中检查触发器条件
- 检查数据层是否包含所需值
- 修改条件并重新测试
详见debugging.md获取详细的调试流程。
Technical Constraints
技术限制
JavaScript (ES5 Requirement)
JavaScript(ES5要求)
GTM Custom JavaScript Variables and Custom HTML Tags require ES5 syntax:
javascript
// Use var instead of const/let
var myVar = 'value';
// Use function instead of arrow functions
var myFunc = function(x) { return x * 2; };
// Use string concatenation instead of template literals
var message = 'Hello, ' + name;Exception: Custom Templates support some ES6 features in their sandboxed environment.
GTM自定义JavaScript变量和自定义HTML标签要求使用ES5语法:
javascript
// Use var instead of const/let
var myVar = 'value';
// Use function instead of arrow functions
var myFunc = function(x) { return x * 2; };
// Use string concatenation instead of template literals
var message = 'Hello, ' + name;例外情况:自定义模板在其沙箱环境中支持部分ES6特性。
Regular Expressions (RE2 Format)
正则表达式(RE2格式)
GTM uses RE2 regex syntax, which differs from standard JavaScript regex:
Supported: Character classes, quantifiers, anchors, groups, alternation
Not Supported: Lookahead, lookbehind, backreferences, possessive quantifiers
regex
undefinedGTM使用RE2正则表达式语法,与标准JavaScript正则表达式有所不同:
支持特性:字符类、量词、锚点、分组、分支
不支持特性:正向预查、反向预查、反向引用、占有量词
regex
undefinedMatch product pages
Match product pages
^/products/[^/]+$
^/products/[^/]+$
Case-insensitive matching
Case-insensitive matching
(?i)^/checkout
undefined(?i)^/checkout
undefinedNaming Conventions
命名规范
Tags
代码标签
Format:
[Platform] - [Type] - [Description]Examples:
GA4 - Event - Form SubmitGoogle Ads - Conversion - PurchaseFB - Pixel - Page View
格式:
[平台] - [类型] - [描述]示例:
GA4 - Event - Form SubmitGoogle Ads - Conversion - PurchaseFB - Pixel - Page View
Triggers
触发器
Format:
[Event Type] - [Description]Examples:
Click - CTA ButtonPage View - HomepageForm Submit - Contact Form
格式:
[事件类型] - [描述]示例:
Click - CTA ButtonPage View - HomepageForm Submit - Contact Form
Variables
变量
Format:
[Type] - [Description]Examples:
- (Data Layer)
DL - User ID - (Custom JavaScript)
CJS - Format Price Constant - GA4 Measurement ID
格式:
[类型] - [描述]示例:
- (数据层)
DL - User ID - (自定义JavaScript)
CJS - Format Price Constant - GA4 Measurement ID
Performance Tips
性能优化技巧
- Use native tag templates instead of Custom HTML when possible
- Minimise Custom JavaScript execution time
- Remove unused tags, triggers, and variables regularly
- Use tag sequencing wisely to avoid unnecessary delays
- Defer non-critical tags to improve page load
- 尽可能使用原生标签模板,而非自定义HTML
- 减少自定义JavaScript的执行时间
- 定期移除未使用的代码标签、触发器和变量
- 合理使用标签执行顺序,避免不必要的延迟
- 延迟加载非关键标签,提升页面加载速度
Security Best Practices
安全最佳实践
- Vet all Custom HTML tags for malicious code
- Never push PII to the data layer - hash sensitive identifiers
- Implement consent mode for privacy compliance
- Limit container admin access to trusted users
- Review third-party templates before using
- 审核所有自定义HTML标签,检查是否包含恶意代码
- 切勿将个人身份信息(PII)推送至数据层——对敏感标识符进行哈希处理
- 实施同意模式以符合隐私合规要求
- 限制容器管理员权限,仅授权可信用户
- 使用第三方模板前先进行审核
Quick Reference
快速参考
Built-in Variables to Enable
建议启用的内置变量
- Page URL, Page Path, Page Hostname
- Click Element, Click Classes, Click ID, Click URL, Click Text
- Form Element, Form ID, Form Classes
- Scroll Depth Threshold, Scroll Direction
- 页面URL、页面路径、页面主机名
- 点击元素、点击类、点击ID、点击URL、点击文本
- 表单元素、表单ID、表单类
- 滚动深度阈值、滚动方向
Common Trigger Types
常见触发器类型
- Page View (All Pages, Some Pages)
- Click (All Elements, Just Links)
- Form Submission
- Custom Event
- History Change (for SPAs)
- Timer
- Scroll Depth
- 页面浏览(所有页面、部分页面)
- 点击(所有元素、仅链接)
- 表单提交
- 自定义事件
- 历史记录变更(适用于SPA)
- 计时器
- 滚动深度
Essential Data Layer Events
核心数据层事件
javascript
// Page view
window.dataLayer.push({ 'event': 'page_view' });
// User login
window.dataLayer.push({ 'event': 'login', 'method': 'Google' });
// Purchase
window.dataLayer.push({
'event': 'purchase',
'ecommerce': {
'transaction_id': 'T12345',
'value': 99.99,
'currency': 'AUD',
'items': [...]
}
});javascript
// Page view
window.dataLayer.push({ 'event': 'page_view' });
// User login
window.dataLayer.push({ 'event': 'login', 'method': 'Google' });
// Purchase
window.dataLayer.push({
'event': 'purchase',
'ecommerce': {
'transaction_id': 'T12345',
'value': 99.99,
'currency': 'AUD',
'items': [...]
}
});Reference Files
参考文件
| Topic | Reference File |
|---|---|
| Container setup | setup.md |
| Tag configuration | tags.md |
| Trigger configuration | triggers.md |
| Variable configuration | variables.md |
| Data layer | datalayer.md |
| Debugging | debugging.md |
| Best practices | best-practices.md |
| Custom templates | custom-templates.md |
| API automation | api.md |
| 主题 | 参考文件 |
|---|---|
| 容器设置 | setup.md |
| 代码标签配置 | tags.md |
| 触发器配置 | triggers.md |
| 变量配置 | variables.md |
| 数据层 | datalayer.md |
| 调试 | debugging.md |
| 最佳实践 | best-practices.md |
| 自定义模板 | custom-templates.md |
| API自动化 | api.md |