---
metadata:
  - name: generator
    content: Diplodoc Platform v5.54.5
alternate:
  - https://yandex.com/support/metrica/en/ecommerce/data.md
  - https://yandex.com/support/metrica/es/ecommerce/data.md
  - https://yandex.com/support/metrica/pt/ecommerce/data.md
  - https://yandex.com/support/metrica/ru/ecommerce/data.md
  - https://yandex.com/support/metrica/tr/ecommerce/data.md
  - https://yandex.com/support/metrica/zh/ecommerce/data.md
  - href: zh/ecommerce/data.md
    type: text/markdown
    title: Markdown version
  - href: ../llms.txt
    type: text/markdown
    title: llms.txt
title: 传输 E-commerce 数据
description: 在 E-commerce 中，每个商品项都是一个可对其执行特定操作的对象。此数据以 JavaScript 对象的形式传输，其中包含操作 ID 以及对此操作所针对商品项的描述列表。
---
> **Documentation Index:** Fetch the complete configuration index at https://yandex.com/support/metrica/zh/llms.txt


# 传输 E-commerce 数据

<!-- source: zh/_includes/code/counter-initialize/id-counter-initialize/rest-js.md -->
{% note warning %}

使用 JavaScript API 需要了解 HTML 和 JavaScript。 如果您不了解这些语言，请联系您的网站开发人员或站长。

{% endnote %}
<!-- endsource: zh/_includes/code/counter-initialize/id-counter-initialize/rest-js.md -->


## 关于数据容器及其向 Yandex Metrica 的传输 {#about}

在 E-commerce 中，每个商品都是一个可以执行某些活动的对象，例如查看完整的商品描述或将其添加到购物篮。 此数据作为 JavaScript 对象进行传输，其中包含操作 ID 以及针对其执行此活动的商品的描述列表。 在 JavaScript API 的上下文中，我们称这些对象为 _E-commerce 对象_。

要将数据作为 E-commerce 对象传输到 Yandex Metrica，请使用 [push](http://www.w3schools.com/jsref/jsref_push.asp) 方法将它们添加到全局命名空间 (window) 中特殊的 JavaScript 数组`window.dataLayer`。 我们称这个数组为_数据容器_。


{% note alert %}

当用户转到网站上的另一个页面时，不要传输数据。 例如，在“Checkout”按钮上使用 `onclick` 事件就属于这种情况。 在这种情况下，下一页可能会在计数器将数据发送到 Yandex Metrica 之前加载。 结果导致有关该事件的信息丢失。

{% endnote %}


数据容器必须位于全局命名空间中，并且其名称必须与计数器配置或初始化期间指定的名称匹配。 如果数据容器被命名为 `dataLayer`，或者 Yandex Metrica 计数器是通过设置为 `true` 的 [ecommerce](https://yandex.com/support/metrica/zh/code/counter-initialize.md#param-ecommerce) 参数初始化的，则假设数据容器是 `window.dataLayer` 数组。

```html

...


```

数据容器的名称及其包含的 E-commerce 对象的结构与 Google Analytics Enhanced Ecommerce 中的相应实体相匹配。 这意味着，如果您已经设置了向 Google Analytics Enhanced Ecommerce 发送数据（包括通过 [Global Site Tag](https://developers.google.com/gtagjs/?hl=en)），并在 Yandex Metrica 中启用了 Ecommerce 功能，那么后者将开始收集数据。 Yandex Metrica 还支持 GA4 Ecommerce，无需任何额外设置即可收集数据。


{% note alert %}

数据容器最多可包含 8,192 个字符。 
如果要在容器中发送更多数据，我们建议将订单拆分为带有子编号的部分（例如 `order1-1`、`order1-2`、`order1-3`）。 这样，订单和子订单都将在 Yandex Metrica 中显示。

要评估实际订单数量，请设置一个 JS 目标，并将其与其中一个子订单一起发送到 Yandex Metrica。 在发送一个拆分为子订单的订单的多个容器时，将达成一个目标，您可以用它来计算订单总数。

{% endnote %}


ecommerce 对象具有以下格式：

```javascript
window.dataLayer.push({
    "ecommerce": {
        "currencyCode": "RUB",
        "<actionType>": {
            "actionField": <actionField>,
            "products": [<productFieldObject>, <productFieldObject>, ...]
        }
    }
});
```


#|
||
**字段**
|
**类型**
|
**描述**
||
||
`ecommerce`[*](*Параметр)
|
Object
|
必填容器字段。
||
||
`currencyCode`
|
字符串
|
三个字母的 [ISO 4217 货币代码](https://www.six-group.com/en/products-services/financial-information/data-standards.html#scrollTo=currency-codes)。

如果传递不同的货币，则将发送空值，而不是货币和金额。
||
||
`<actionType>`[*](*Параметр) {#action_type}
|
—
|
字段名（代替`<actionType>`）是对一组产品执行的操作标识符。

可能的值：

- `impression`：查看产品列表。
- `click`：点击产品列表中的某个商品。
- `detail`: 查看商品。
- `add`：将商品添加到购物车。
- `remove`：从购物车中移除商品。
- `purchase`: 购买。
- `promoView`: 查看内部广告。
- `promoClick`: 点击内部广告。

如果有关移除商品的信息已传输到 Yandex Metrica，则报表可能会显示商品数量为负（总数是通过从添加的商品总数中减去删除的商品数来计算的）。 如果传输了商品的价格，则报表中的价格也可能为负值。
||
||
`actionField`[**](*Обязательный_параметр)
|
Object
|
一个 [actionField](#action_data) 对象。 描述所执行活动的附加数据。

仅当操作为[购买](#purchase)时才处理 (`<actionType>` — `purchase`)。
||
||
`商品`[*](*Параметр)
|
数组
|
针对其执行指定活动的商品的描述列表。 产品描述是 [productFieldObject](#product_data) 对象。

不适用于 `promoClick`、`promoView` 和 `impressions` 操作。
||
||
`promotions`[*](*Параметр)
|
数组
|
针对其执行指定活动的广告系列的描述列表。 广告系列描述是 [promoFieldObject](#promo_data) 对象。
||
|#

<sup >*</sup>必填参数。

<sup >**</sup>发送购买信息所需的必要参数。

### 商品数据 {#product_data}

描述特定商品的对象。

描述产品的对象结构表示为 **productFieldObject**。


#|

### Поля объекта {#product_data}

||
**字段**
|
**类型**
|
**描述**
||
||
`id`[*](*Указать)
|
字符串
|
商品 ID。 例如，SKU。

**必须指定 id 或 name**
||
||
`name`[*](*Указать)
|
字符串
|
商品名称。 例如“T恤”

**您必须指定 name 或 id**
||
||
`brand`
|
字符串
|
与商品相关的品牌或商标。 例如，“Yandex”
||
||
`category`
|
字符串
|
该商品所属的类别。

类别层次结构最多支持 4 级。 使用 / 符号分隔级别。 例如："Clothing / Men's clothing / T-shirts"
||
||
`coupon`
|
字符串
|
与商品关联的促销代码。 例如，“PARTNER_SITE_15”
||
||
`discount`
|
数字
|
折扣金额（数值）。
||
||
`list`
|
字符串
|
列出商品所属的列表。

为了评估列表在用户与产品交互的不同阶段的有效性，我们建议在列表被查看后发生的所有事件中指定商品列表。
||
||
`position`
|
整数
|
商品在列表中的位置。 例如，2
||
||
`price`
|
数字
|
商品价格。
||
||
`quantity`
|
整数
|
物品数量。
||
||
`variant`
|
字符串
|
商品的变体。 例如，“红”
||
|#

### 活动数据 {#action_data}

包含有关对某个商品或一组商品执行的活动的数据的对象。

仅当操作为购买时才处理 (`<actionType>` — `purchase`)。

描述操作的对象结构表示为 **actionField**。

在传输有关活动的数据时，Yandex Metrica 会创建一个目标。 这使您可以获取有关 Yandex Direct 广告系列收入的信息。 在 Yandex Direct 的可用目标列表中，此目标显示为“eCommerce: Purchase (tag №<tag number>)”。 您可以通过传输[goal_id](#goal_id) 字段来跟踪目标完成情况。


#|

### Поля объекта {#action_data}

||
**字段**
|
**类型**
|
**描述**
||
||
`id`[*](*Поле)
|
字符串
|
购买的商品的 ID。

必填信息。

例如：TRX#54321
||
||
`coupon`
|
字符串
|
与整个购买相关的促销代码
||
||
`goal_id` {#goal_id}
|
整数
|
[目标](https://yandex.com/support/metrica/zh/general/goals.md)号码。 指定是否此[活动](#action_type)为目标。 
目标必须设定为 [JavaScript 事件](https://yandex.com/support/metrica/zh/general/goals.md)类型。

您可以在 Yandex Metrica 界面中查看目标编号：转到**Settings** 部分并打开**Goals** 选项卡。
![](../_assets/order-ecommerce.png){.border-yes}
||
||
`revenue`
|
数字
|
获得的收入。

如果省略，则会自动计算为与购买相关的所有商品的价格总和
||
|#

### 促销活动数据 {#promo_data}

描述促销活动的对象。

该对象使用类似的容器，但使用 **promoFieldObject** 代替 **productFieldObject**。


#|

### Поля объекта {#promo_data}

||
**字段**
|
**类型**
|
**描述**
||
||
`id`[*](*Поле)
|
字符串
|
促销活动的 ID。

**必填信息**
||
||
`name`
|
字符串
|
促销广告系列名称
||
||
`creative`
|
字符串
|
广告横幅名称
||
||
`creative_slot`
|
字符串
|
广告横幅位
||
||
`position`
|
字符串
|
广告横幅位置
||
|#


## 例如 {#examples}

要传输信息，您需要在网站上创建一个脚本，该脚本将按照上述格式负责特定事件（例如下订单）。 以下是 Yandex Metrica 支持的活动脚本示例。

所有示例都假定[计数器已在启用电子商务的情况下初始化](https://yandex.com/support/metrica/zh/data/e-commerce.md)，并且[数据通过 window.dataLayer 容器传输](#about)。

### 查看商品列表 {#product-list}

数据必须在商品列表打开时发送。

```javascript
dataLayer.push({
    "ecommerce": {
        "currencyCode": "RUB",
        "impressions": [
            {
                "id": "P15432",
                "name": "T-shirt",
                "price": 477.60,
                "brand": "Yandex",
                "category": "Clothing / Men's clothing / T-shirts",
                "variant": "Red",
                "list": "Search",
                "position": 1
            },
            {
                "id": "P15435",
                "name": "T-shirt",
                "price": 500.60,
                "brand": "Yandex",
                "category": "Clothing / Men's clothing / T-shirts",
                "variant": "Blue",
                "list": "Search",
                "position": 2
            }
        ]
    }
});
```

### 点击商品列表项 {#click-on-product}

数据必须在用户点击商品链接时发送。

```javascript
dataLayer.push({
    "ecommerce": {
        "currencyCode": "RUB",
        "click": {
            "products": [
                {
                    "id": "39084",
                    "name": "Yandex Tumbler",
                    "price": 1089.69,
                    "brand": "Yandex",
                    "category": "Housewares / Tableware / Insulated bottles and thermal mugs",
                    "variant": "Red",
                    "list": "Search",
                    "position": 1
                }
            ]
        }
    }
});
```

### 查看商品 {#product-detail}

数据必须在带有商品卡的页面打开时发送。

```javascript
dataLayer.push({
    "ecommerce": {
        "currencyCode": "RUB",
        "detail": {
            "products": [
                {
                    "id": "P15432",
                    "name": "T-shirt",
                    "price": 477.60,
                    "brand": "Yandex",
                    "category": "Clothing / Men's clothing / T-shirts",
                    "variant": "Red",
                    "list": "Search results",
                    "position": 1
                }
            ]
        }
    }
});
```

### 将商品加入购物篮 {#add}

数据必须在订单加入购物篮时发送。 例如，在点击“Add to basket”时。

```javascript
dataLayer.push({
    "ecommerce": {
        "currencyCode": "RUB",
        "add": {
            "products": [
                {
                    "id": "43521",
                    "name": "Yandex bag",
                    "price": 654.32,
                    "brand": "Yandex",
                    "category": "Accessories / Bags",
                    "quantity": 1,
                    "list": "Category search results",
                    "position": 2
                }
            ]
        }
    }
});
```

### 从购物篮移除商品 {#remove}

数据必须在商品从购物篮移除时发送。

```javascript
dataLayer.push({
    "ecommerce": {
        "currencyCode": "RUB",
        "remove": {
            "products": [
                {
                    "id": "15243",
                    "name": "Yandex screen wipes for smartphones",
                    "category": "Smartphone accessories",
                    "quantity": 1,
                    "list": "Accessories",
                    "position": 3
                }
            ]
        }
    }
});
```

### 购买 {#purchase}

数据必须在订单确认后立即发送。

```javascript
dataLayer.push({
    "ecommerce": {
        "currencyCode": "RUB",
        "purchase": {
            "actionField": {
                "id": "TRX987"
            },
            "products": [
                {
                    "id": "25341",
                    "name": "Yandex men's sweatshirt",
                    "price": 1345.26,
                    "brand": "Yandex",
                    "category": "Clothing / Men's clothing / Hoodies and sweatshirts",
                    "variant": "Orange",
                    "quantity": 1,
                    "list": "Clothing",
                    "position": 1
                },
                {
                    "id": "25314",
                    "name": "Yandex women's sweatshirt",
                    "price": 1543.62,
                    "brand": "Yandex",
                    "category": "Clothing / Women's clothing / Hoodies and sweatshirts",
                    "variant": "White",
                    "quantity": 3,
                    "list": "Sweatshirts",
                    "position": 2
                }
            ]
        }
    }
});
```

### 查看内部广告 {#adv-view}

数据必须在用户查看广告时发送。

```javascript
dataLayer.push({
    "ecommerce": {
        "promoView": {
            "promotions": [
                {
                    "id": "BF001",
                    "name": "Black Friday",
                    "creative": "Banner_1",
                    "position": "Slot1"
                },
                {
                    "id": "SUMMER002",
                    "name": "Summer sales",
                    "creative": "Banner_3",
                    "position": "Slot2"
                }
            ]
        }
    }
});
```

### 点击内部广告 {#adv-click}

必须在用户点击广告或完成与广告相关的另一个目标活动时发送数据。

```javascript
dataLayer.push({
    "ecommerce": {
        "promoClick": {
            "promotions": [
                {
                    "id": "BF001",
                    "name": "Black Friday",
                    "creative": "Banner_1",
                    "position": "Slot1"
                }
            ]
        }
    }
});
```

### 了解详情

- [检查电子商务设置](https://yandex.com/support/metrica/zh/ecommerce/check.md)
- [故障排除](https://yandex.com/support/metrica/zh/ecommerce/troubleshooting.md)


<!-- source: zh/_includes/buttons/chat-button.md -->
[与我们聊天](https://yandex.com/chat/#/user/036e6a02-3620-9cdc-4c5e-a34667a7379e?utm_source=spravka){.button}

<!-- source: zh/_includes/styles/href-to-button.md -->

<!-- endsource: zh/_includes/styles/href-to-button.md -->
<!-- endsource: zh/_includes/buttons/chat-button.md -->

<!-- source: zh/_includes/reports/support-button.md -->
[联系我们](https://yandex-ad.cn/contact){.button}


<!-- source: zh/_includes/styles/cut-button.md -->

<!-- endsource: zh/_includes/styles/cut-button.md -->

<!-- source: zh/_includes/styles/href-to-button.md -->

<!-- endsource: zh/_includes/styles/href-to-button.md -->
<!-- endsource: zh/_includes/reports/support-button.md -->

<!-- source: zh/_includes/footer-links.md -->
- - -

<div class="borderless-table">

#|
||
实用链接

- [演示计数器](https://metrica.yandex.com/r/dashboard?)
- [添加计数器](https://metrica.yandex.com/add/)
- [免费计数器设置](https://yandex.com/promo/freeservice/metrica?utm_source=help_metrica_en&utm_medium=cpc&utm_campaign=1)
- [Yandex Metrica API](https://tech.yandex.com/metrika/)
- [提出您的想法](https://yandex.com/support/metrica/troubleshooting/idea.html)
|
线上培训



- [参加培训课程](https://yandex.com/adv/edu/online/metrika?utm_source=metrika_help&utm_medium=web&utm_campaign=static&utm_content=useful_links)

||
|#

</div>

<!-- source: zh/_includes/styles/table-style.md -->

<!-- endsource: zh/_includes/styles/table-style.md -->
<!-- endsource: zh/_includes/footer-links.md -->

[*Параметр]: 必填参数。

[*Обязательный_параметр]: 传输购买信息的必填参数。

[*Указать]: **您必须指定 name 或 id**

[*Поле]: 必填信息。