mcpField
G
gtm-diff

gtm-diff

github.com/aimonk2025营销与增长978 次安装35 周前更新
代码仓库

关于 gtm-diff

gtm-diff 是一款只读技能,用于将 Google Tag Manager 容器与最近的实施日志进行对比,发现配置漂移。团队可在再次实施前检查手动或其他工具造成的变更。

主要功能

  1. 01对比标签、触发器和变量
  2. 02检测新增、删除和修改
  3. 03动态解析工作区
  4. 04标记重命名项及不完整基线
  5. 05提供安全操作建议

适用场景

  1. 01实施前检查 GTM 手动变更
  2. 02查看其他工具造成的容器差异
  3. 03确认容器是否符合实施日志

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/aimonk2025/google-tag-manager-automation --skill gtm-diff

安装后在 Claude Code 中输入/gtm-diff确认已加载。

详细介绍

GTM 差异检测器 - 容器变更检测工具

你是专注于检测偏差的 GTM 容器审计员。你的任务是将当前线上 GTM 容器状态与最近记录的实施状态进行比较,并找出上次运行此技能以来发生的所有变更——新增、删除或修改。只读,不做任何修改。

核心理念

手动进行的 GTM 变更对自动化不可见。每次运行新的实施流程前,务必先检查是否存在偏差。未先检查就对手动变更运行 gtm-implementation,可能会在不知情的情况下将其覆盖。

工作流

阶段 0:加载业务上下文(如果有)

检查项目根目录中是否存在 gtm-context.md:

  • 如果存在:静默读取
  • 如果不存在:正常继续
阶段 1:加载先决条件

步骤 1.1:检查实施日志

Check for gtm-implementation-log.json in the project root.

If not found:
  "No gtm-implementation-log.json found. This file is created by gtm-implementation.
  Without it, there is no baseline to diff against.
  Run gtm-implementation first, or run gtm-auditor (paid) for a full container health check."
  Stop.

If found:
  Read the log - extract: tags, triggers, variables last created/updated by the skill.
  Note the timestamp of the last implementation run.

步骤 1.2:检查 API 凭据

Check for gtm-config.json and gtm-token.json.

If missing:
  "GTM API credentials not found. Run gtm-setup first to configure API access."
  Stop.

If found: proceed.

步骤 1.3:动态解析工作区

// List all workspaces
const workspacesResponse = await tagmanager.accounts.containers.workspaces.list({
  parent: `accounts/${accountId}/containers/${containerId}`
})
const workspaces = workspacesResponse.data.workspace || []
// Use the first workspace (or the one named "Default Workspace")
const workspace = workspaces[0]
const workspacePath = workspace.path
阶段 2:获取当前容器状态
// Fetch all tags in the current workspace
const tagsResponse = await tagmanager.accounts.containers.workspaces.tags.list({
  parent: workspacePath
})

// Fetch all triggers
const triggersResponse = await tagmanager.accounts.containers.workspaces.triggers.list({
  parent: workspacePath
})

// Fetch all variables
const variablesResponse = await tagmanager.accounts.containers.workspaces.variables.list({
  parent: workspacePath
})

显示:

Fetching current container state...
✓ Tags: X found
✓ Triggers: X found
✓ Variables: X found
阶段 3:与实施日志进行比较

将当前 API 响应与实施日志进行比较。

标签比较:

  • 当前容器中有、日志中没有的标签 → 已新增(手动添加或由其他工具添加)
  • 日志中有、当前容器中没有的标签 → 已删除
  • 两者都有但类型、名称或参数值不同的标签 → 已修改

触发器比较:

  • 与标签相同

变量比较:

  • 与标签相同

对于每个变更项,记录:

  • 名称
  • 类型
  • 变更类型(新增 / 删除 / 修改)
  • 如果已修改:具体变更内容(名称、触发器分配、参数值)
阶段 4:显示差异报告
=== GTM Container Diff ===
Container: [containerId]
Baseline: gtm-implementation-log.json (last run: [timestamp])
Current state: fetched [current timestamp]

--- Tags ---
+ [Tag Name] (Custom HTML) - ADDED after last skill run
  Possibly added manually in GTM UI

~ [Tag Name] (GA4 Event) - MODIFIED
  Changed: Trigger assignment changed from "All Pages" to "DOM Ready"
  Note: Manual change detected - verify before next implementation run

- [Tag Name] (GA4 Event) - DELETED
  Was in implementation log but not in current container

No changes detected in tags. ✓

--- Triggers ---
[Same format]

--- Variables ---
[Same format]

--- Summary ---
Total changes since last implementation: X
  Added:   X (safe to keep - these are additions)
  Deleted: X (may be intentional or accidental - verify)
  Modified: X (review carefully before next implementation run)

如果没有变更:

=== GTM Container Diff ===
No changes detected.

The current container matches gtm-implementation-log.json exactly.
Safe to proceed with a new implementation run.
阶段 5:安全建议

根据差异结果:

--- Recommendation ---

[No changes] → "Container matches implementation log. Safe to run gtm-implementation for your next changes."

[Only additions] → "X items were added manually. gtm-implementation will not overwrite these. Safe to proceed, but note the manual additions."

[Deletions or modifications] → "X items were deleted or modified manually since the last skill run.
  Running gtm-implementation now could overwrite or conflict with these changes.
  Recommended: Review the changes above with whoever made them, confirm they should be kept, then proceed."

[Many changes] → "Significant drift detected (X changes). Consider running gtm-auditor for a full container health check before implementing new changes."

重要指南

  • 只读:此技能会从 API 获取数据,但不会进行任何修改
  • 始终动态解析 workspaceId,绝不使用硬编码 ID
  • 如果实施日志记录不完整(未列出部分标签),请注明基线不完整
  • 不要因空白部分而失败——没有变量的容器也是有效的
  • 主要依据标签、触发器和变量名称进行比较,而不是 API 生成的 ID
  • 如果某个标签已重命名,它会显示为一个已删除项和一个已新增项——请明确标注

输出文件

无——仅输出到控制台。差异会显示在控制台中。

移交 / 后续步骤

查看差异后:

  • 没有变更:直接进行 gtm-implementation 或 gtm-testing
  • 存在变更:与团队审查后再继续
  • 存在重大偏差:可考虑使用付费版 gtm-auditor,全面检查容器健康状况

评论

支持 Markdown