> 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/experience/setting/custom-event-setup.md). Agents can also search and read this documentation through its [MCP server](https://helps.ptengine.com/~gitbook/mcp).

# 自定义事件的设置方法

可视化事件设置点不到的行为（比如提交成功、加购带商品属性），用 `ptengine.track()` 写代码上报。

{% hint style="info" %}
**不写代码也能设事件** —— 大多数点击、浏览类行为用[可视化事件设置](https://helps.ptengine.com/cn/insight/event/setup.md)就够了，不用改网站代码。这一页讲的是可视化设不了的情况。
{% endhint %}

***

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

* 目标页面上**已经装好**[**基础代码**](https://helps.ptengine.com/cn/start-guide/quick/tag-setup.md)，否则 `ptengine` 对象不存在
* 需要能改网站的 HTML 或 JS。**改源码的部分建议交给工程师**
* 事件设好后，数据要 **30 分钟～1 小时**才会出现在报告里

{% hint style="success" %}
**事件对 Insight 和 Experience 都生效**，不用分别设两次。
{% endhint %}

***

## 在哪里拿代码 <a href="#where-to-get-the-code" id="where-to-get-the-code"></a>

{% stepper %}
{% step %}

### 齿轮图标 → 数据设置

页面上有**事件**、**事件属性**、**用户属性**三个标签页，已设好的事件都列在这里。**来源**那一列会标明每个事件是从哪来的（基础代码 / 体验）。

<figure><img src="https://2044586328-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FnucbKsG9WGIoxCpzyoHL%2Fuploads%2Fgit-blob-a9beadd265f8b1e366fe6950cb228ce11104a130%2Fexperience-setting-data-01.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-8413e3e96882a6a1972f80a43ff6845736160b22%2Fexperience-setting-custom-event-01.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-2088273a62ad8e9cfe932a031987df5ce1c9b702%2Fexperience-setting-custom-event-02.png?alt=media" alt="安装向导第二步：设置事件，含 ptengine.track 示例"><figcaption></figcaption></figure>
{% endstep %}
{% endstepper %}

***

## 代码怎么写 <a href="#how-to-write" id="how-to-write"></a>

```javascript
ptengine.track("check_out", {
    product_category: "Clothing",
    product_pricing: "1293"
});
```

| 部分        | 含义                                 |
| --------- | ---------------------------------- |
| 第一个参数     | **事件名**，自己起。之后在报告里就按这个名字找          |
| 第二个参数（可选） | **事件属性**，键值对。属性让你能进一步拆分，比如按商品类别看加购 |

{% hint style="warning" %}
事件名定下来就**别再改**。改了相当于换了个新事件，前后数据不会合并。
{% endhint %}

***

## 放在哪里 <a href="#where-to-put-it" id="where-to-put-it"></a>

放在**事件实际发生的那个页面**上，而且要在事件发生的时刻被调用。

* 直接贴进 HTML 源码：放在 `</body>` 之前
* 用 GTM 的话见[通过Google Tag Manager配置](https://helps.ptengine.com/cn/start-guide/othertag/tag-gtm.md)

### 示例 1：点击 Login 按钮 <a href="#configuration-examples" id="configuration-examples"></a>

**写在 HTML 事件属性里：**

```html
<button onclick="ptengine.track('login_click')">Login</button>
```

**用 JavaScript 绑定：**

```javascript
document.getElementById('btnLogin').addEventListener('click', function () {
    ptengine.track('login_click');
});
```

### 示例 2：点击 Submit，同时上报邮箱和提交时间 <a href="#example-2" id="example-2"></a>

```html
<button id="btnSubmit"
    onclick="ptengine.track('form_submit', {
        email: document.getElementById('email').value,
        submit_date: new Date()
    });">
Submit</button>
```

```javascript
document.getElementById('btnSubmit').addEventListener('click', function () {
    ptengine.track('form_submit', {
        email: document.getElementById('email').value,
        submit_date: new Date()
    });
});
```

***

## 做完之后怎么确认 <a href="#verify" id="verify"></a>

1. 在页面上实际触发一次这个行为
2. 回**数据设置 → 事件**，看新事件有没有出现在列表里
3. 数值要 30 分钟～1 小时才进[事件分析](https://helps.ptengine.com/cn/insight/event.md)报告
4. 没出现 → 先在浏览器控制台敲 `ptengine`，没有这个对象说明基础代码没装好或还没加载完

{% hint style="info" %}
在**数据设置**里点某一行的 **⋯** 可以给事件或事件属性加说明，方便团队其他人看懂这个事件是干什么的。
{% endhint %}

***

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

* [可视化事件设置方法](https://helps.ptengine.com/cn/insight/event/setup.md) — 不写代码的做法
* [事件分析](https://helps.ptengine.com/cn/insight/event.md) — 设好之后在哪看
* [通过Google Tag Manager配置](https://helps.ptengine.com/cn/start-guide/othertag/tag-gtm.md)
* [基础代码的安装方法](https://helps.ptengine.com/cn/start-guide/quick/tag-setup.md)
