> For the complete documentation index, see [llms.txt](https://helps.ptengine.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://helps.ptengine.com/cn/start-guide/othertag/tag-gtm.md).

# Google Tag Manager 中的标签设置

不方便直接改网站 HTML 时，可以用 GTM 把 Ptengine 基础代码发到页面上。两种做法：**让 Ptengine 自动生成并发布**，或者**自己在 GTM 里建**。

{% hint style="warning" %}
**能直接写进 `<head>` 就别绕 GTM。** 经 GTM 多一层加载，数据完整性会略降，页面编辑类体验也更容易[闪动](/cn/faq/experience-edit/anti-flicker.md)。详见[其他安装方法](/cn/start-guide/othertag.md)。
{% endhint %}

{% hint style="info" %}
**先对一下 GTM 的中文叫法**，照着界面找才不会迷路：

| 通常说的               | GTM 中文界面里叫     |
| ------------------ | -------------- |
| Google Tag Manager | 跟踪代码管理器        |
| tag（标签）            | **代码**         |
| trigger            | **触发器** / 触发条件 |
| publish（发布）        | **提交**         |
| {% endhint %}      |                |

***

## 开始之前 <a href="#before-you-start" id="before-you-start"></a>

* 网站上**已经装好并提交过 GTM 容器**。还没有的话先看 [Google 的说明](https://support.google.com/tagmanager/answer/6103696?hl=zh-CN)
* 快速安装需要用**能管理该 GTM 容器的 Google 账号**授权
* 不管用哪种方法，**最后都要验证一次**（见[装完之后](#verify)）

***

## 方法一：快速安装（Ptengine 替你建） <a href="#quick-installation-steps" id="quick-installation-steps"></a>

{% stepper %}
{% step %}

### 在安装页点“使用谷歌标签管理器（GTM）快速安装”

入口在 Experience 的 ⚙ →**数据设置**→右上角**监测代码帮助**，第一步“添加代码”里。

<figure><img src="https://2044586328-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnucbKsG9WGIoxCpzyoHL%2Fuploads%2Fgit-blob-724237700f2d89c0d81d5e36812de9c2257a770b%2Fstart-guide-othertag-gtm-01.png?alt=media" alt="添加代码步骤里的 GTM 快速安装入口"><figcaption></figcaption></figure>
{% endstep %}

{% step %}

### 选 GTM账号和 GTM容器

两项都选完，**生成并发布标签**才可点。

<figure><img src="https://2044586328-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnucbKsG9WGIoxCpzyoHL%2Fuploads%2Fgit-blob-130dc0185bd045083571c2b526205a02c87ed4f9%2Fstart-guide-othertag-gtm-02.png?alt=media" alt="安装 Google Tag Manager：选择账号与容器"><figcaption></figcaption></figure>

{% hint style="danger" %}
**这一步会直接改动线上。** 点下去之后 Ptengine 会往你选的容器里建代码**并提交**，不经过草稿确认。先确认选对了容器。
{% endhint %}
{% endstep %}

{% step %}

### 回 Ptengine 验证

见[装完之后](#verify)。
{% endstep %}
{% endstepper %}

***

## 方法二：自己在 GTM 里建 <a href="#manual-installation-steps" id="manual-installation-steps"></a>

好处是**能精确控制代码放在哪、什么时候触发**，也不用把容器权限交出去。

{% stepper %}
{% step %}

### 先从 Ptengine 复制基础代码

齿轮图标 →“基础设置”→“档案”→“监测代码”，点**复制**。

{% hint style="info" %}
侧栏菜单项字面叫“监测代码”，就是本站说的**基础代码**。
{% endhint %}
{% endstep %}

{% step %}

### 打开 GTM，进入目标容器

左侧导航是**概览 / 设置 / 代码 / 触发器 / 变量 / 文件夹 / 模板**。右上角能看到容器 ID 和“工作区更改”计数。

<figure><img src="https://2044586328-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnucbKsG9WGIoxCpzyoHL%2Fuploads%2Fgit-blob-d6ef8643a98c996c4f177cbbc2ad07c0b2578edf%2Fstart-guide-othertag-gtm-03.png?alt=media" alt="GTM 容器概览与左侧导航"><figcaption></figcaption></figure>
{% endstep %}

{% step %}

### 左栏点“代码”，右上角点“新建”

<figure><img src="https://2044586328-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnucbKsG9WGIoxCpzyoHL%2Fuploads%2Fgit-blob-579d10f86a8e4fcbddc7f421941d9b2a3230edb7%2Fstart-guide-othertag-gtm-04.png?alt=media" alt="代码列表页右上角的“新建”按钮"><figcaption></figcaption></figure>
{% endstep %}

{% step %}

### 点上方的“代码配置”方框

新建出来的叫“未命名代码”，上下两块分别是**代码配置**和**触发条件**，都要填。

<figure><img src="https://2044586328-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnucbKsG9WGIoxCpzyoHL%2Fuploads%2Fgit-blob-0a0984340adc1b96057e9b42fff49c6930f8d830%2Fstart-guide-othertag-gtm-05.png?alt=media" alt="新建代码：代码配置与触发条件两块"><figcaption></figcaption></figure>
{% endstep %}

{% step %}

### 代码类型选“自定义 HTML”

右侧滑出“选择代码类型”。**这一项在列表最底部的“自定义”分组里** —— 上面一大段是 Google 代码和模板库，要一直往下滚。

<figure><img src="https://2044586328-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnucbKsG9WGIoxCpzyoHL%2Fuploads%2Fgit-blob-d237e4df355bf8608fc98f79141136b9768d6851%2Fstart-guide-othertag-gtm-06.png?alt=media" alt="列表最底部的“自定义”分组里的“自定义 HTML”"><figcaption></figcaption></figure>
{% endstep %}

{% step %}

### 把基础代码粘进 HTML 框

<figure><img src="https://2044586328-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnucbKsG9WGIoxCpzyoHL%2Fuploads%2Fgit-blob-cacde368242cfb6a9f54b1d52db78acefbb7c40e%2Fstart-guide-othertag-gtm-07.png?alt=media" alt="把 Ptengine 基础代码粘贴到自定义 HTML 的编辑框"><figcaption></figcaption></figure>

{% hint style="warning" %}
`xxxxxxxx` 的位置是**你自己档案的 ID**，直接用从 Ptengine 复制来的那段，别手打。
{% endhint %}
{% endstep %}

{% step %}

### 触发条件选“All Pages”

往下滚点**触发条件**方框，在“选择触发器”里挑 **All Pages**（类型是“网页浏览”）。只想监测部分页面就另建一个按 URL 条件的触发器。

<figure><img src="https://2044586328-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnucbKsG9WGIoxCpzyoHL%2Fuploads%2Fgit-blob-22d6e83ddfa72b4cbf54d7f8e40cdd1683df7dbe%2Fstart-guide-othertag-gtm-08.png?alt=media" alt="选择触发器列表里的 All Pages"><figcaption></figcaption></figure>
{% endstep %}

{% step %}

### 起个名字，保存，再“提交”

左上角把“未命名代码”改成看得懂的名字（建议带上 Ptengine），点**保存**。

{% hint style="danger" %}
**保存 ≠ 生效。** 保存只是存进工作区，必须再点右上角的**提交**发布出去，网站上才会真的加载这段代码。这是 GTM 最常见的坑。
{% endhint %}
{% endstep %}
{% endstepper %}

***

## 装完之后一定要验证 <a href="#verify" id="verify"></a>

两种方法都一样，**提交完也不等于就通了**：

1. 回 Ptengine，齿轮图标 →“基础设置”→“档案”→“监测代码”，点**验证安装**
2. 填一个已上线页面的 URL，看到绿色的“我们成功的接收到了网站数据”就说明通了

详细做法和排查思路见[确认基础代码是否正常发送](/cn/faq/start-guide/check-tag-firing.md)。

### 验证不通过时先查这三条 <a href="#troubleshooting" id="troubleshooting"></a>

| 现象                  | 多半是                           |
| ------------------- | ----------------------------- |
| GTM 里明明有这个代码，页面上却没有 | **只保存没提交** —— 右上角“工作区更改”还是非 0 |
| 预览模式下正常，正式访问没有      | 改动**只在预览环境**，没提交到正式           |
| 部分页面有、部分没有          | 触发器不是 All Pages，或被其他条件排除了     |

***

## 相关页面 <a href="#related-pages" id="related-pages"></a>

* [基础代码的安装方法](/cn/start-guide/quick/tag-setup.md) — 能直接改 HTML 时的做法
* [确认基础代码是否正常发送](/cn/faq/start-guide/check-tag-firing.md)
* [基础代码正确部署但没有数据流入](/cn/faq/tracking-code/no-data-after-deploy.md)
* [标签设置常见问题](/cn/faq/tracking-code/tag-setting-faq.md)
* [闪烁现象及应对方法](/cn/faq/experience-edit/anti-flicker.md) — 经 GTM 安装时尤其要看
