google-tagmanager

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Google 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

容器设置

  1. Create a GTM account at tagmanager.google.com
  2. Create a container (Web, iOS, Android, or Server)
  3. Install the container snippet on your website
  4. Configure tags, triggers, and variables
  5. Test in Preview mode
  6. Publish
See setup.md for detailed installation instructions.
  1. 前往tagmanager.google.com创建GTM账号
  2. 创建容器(Web、iOS、Android或Server端)
  3. 在你的网站上安装容器代码片段
  4. 配置代码标签、触发器和变量
  5. 在预览模式下测试
  6. 发布配置
详见setup.md获取详细安装说明。

Basic Tag Configuration

基础代码标签配置

javascript
// Example: GA4 Configuration Tag
Tag Type: Google Analytics: GA4 Configuration
Measurement ID: G-XXXXXXXXXX
Trigger: All Pages
See 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页面浏览追踪

  1. Create GA4 Configuration tag with Measurement ID
  2. Set trigger to "All Pages"
  3. Test in Preview mode
  4. Verify in GA4 DebugView
  5. Publish
  1. 创建带有Measurement ID的GA4配置标签
  2. 设置触发器为“所有页面”
  3. 在预览模式下测试
  4. 在GA4 DebugView中验证
  5. 发布配置

Track Form Submissions

追踪表单提交

  1. Create Form Submission trigger
  2. Create GA4 Event tag with event name
    form_submit
  3. Add form ID/name as event parameter
  4. Test submission in Preview mode
  5. Publish
  1. 创建表单提交触发器
  2. 创建事件名称为
    form_submit
    的GA4事件标签
  3. 将表单ID/名称添加为事件参数
  4. 在预览模式下测试提交操作
  5. 发布配置

Implement E-commerce Tracking

实施电商追踪

  1. Implement data layer with e-commerce events
  2. Create data layer variables for product data
  3. Create GA4 Event tags for each e-commerce event
  4. Map data layer variables to event parameters
  5. Test complete purchase flow
  6. Publish
  1. 实施包含电商事件的数据层
  2. 为产品数据创建数据层变量
  3. 为每个电商事件创建GA4事件标签
  4. 将数据层变量映射到事件参数
  5. 测试完整的购买流程
  6. 发布配置

Debug Tag Not Firing

排查标签未触发问题

  1. Enable Preview mode
  2. Perform the action that should fire the tag
  3. Check "Tags Not Fired" section
  4. Review trigger conditions in Variables tab
  5. Check data layer for required values
  6. Fix conditions and retest
See debugging.md for detailed debugging workflows.
  1. 启用预览模式
  2. 执行应触发标签的操作
  3. 查看“未触发的标签”区域
  4. 在变量标签页中检查触发器条件
  5. 检查数据层是否包含所需值
  6. 修改条件并重新测试
详见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
undefined
GTM使用RE2正则表达式语法,与标准JavaScript正则表达式有所不同:
支持特性:字符类、量词、锚点、分组、分支 不支持特性:正向预查、反向预查、反向引用、占有量词
regex
undefined

Match product pages

Match product pages

^/products/[^/]+$
^/products/[^/]+$

Case-insensitive matching

Case-insensitive matching

(?i)^/checkout
undefined
(?i)^/checkout
undefined

Naming Conventions

命名规范

Tags

代码标签

Format:
[Platform] - [Type] - [Description]
Examples:
  • GA4 - Event - Form Submit
  • Google Ads - Conversion - Purchase
  • FB - Pixel - Page View
格式:
[平台] - [类型] - [描述]
示例:
  • GA4 - Event - Form Submit
  • Google Ads - Conversion - Purchase
  • FB - Pixel - Page View

Triggers

触发器

Format:
[Event Type] - [Description]
Examples:
  • Click - CTA Button
  • Page View - Homepage
  • Form Submit - Contact Form
格式:
[事件类型] - [描述]
示例:
  • Click - CTA Button
  • Page View - Homepage
  • Form Submit - Contact Form

Variables

变量

Format:
[Type] - [Description]
Examples:
  • DL - User ID
    (Data Layer)
  • CJS - Format Price
    (Custom JavaScript)
  • Constant - GA4 Measurement ID
格式:
[类型] - [描述]
示例:
  • DL - User ID
    (数据层)
  • CJS - Format Price
    (自定义JavaScript)
  • Constant - GA4 Measurement ID

Performance Tips

性能优化技巧

  1. Use native tag templates instead of Custom HTML when possible
  2. Minimise Custom JavaScript execution time
  3. Remove unused tags, triggers, and variables regularly
  4. Use tag sequencing wisely to avoid unnecessary delays
  5. Defer non-critical tags to improve page load
  1. 尽可能使用原生标签模板,而非自定义HTML
  2. 减少自定义JavaScript的执行时间
  3. 定期移除未使用的代码标签、触发器和变量
  4. 合理使用标签执行顺序,避免不必要的延迟
  5. 延迟加载非关键标签,提升页面加载速度

Security Best Practices

安全最佳实践

  1. Vet all Custom HTML tags for malicious code
  2. Never push PII to the data layer - hash sensitive identifiers
  3. Implement consent mode for privacy compliance
  4. Limit container admin access to trusted users
  5. Review third-party templates before using
  1. 审核所有自定义HTML标签,检查是否包含恶意代码
  2. 切勿将个人身份信息(PII)推送至数据层——对敏感标识符进行哈希处理
  3. 实施同意模式以符合隐私合规要求
  4. 限制容器管理员权限,仅授权可信用户
  5. 使用第三方模板前先进行审核

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

参考文件

TopicReference File
Container setupsetup.md
Tag configurationtags.md
Trigger configurationtriggers.md
Variable configurationvariables.md
Data layerdatalayer.md
Debuggingdebugging.md
Best practicesbest-practices.md
Custom templatescustom-templates.md
API automationapi.md
主题参考文件
容器设置setup.md
代码标签配置tags.md
触发器配置triggers.md
变量配置variables.md
数据层datalayer.md
调试debugging.md
最佳实践best-practices.md
自定义模板custom-templates.md
API自动化api.md

External Resources

外部资源