Transmitting E-commerce data

Warning

Working with the JavaScript API requires knowledge of HTML and JavaScript. If you don’t know these languages, contact your website developer or webmaster.

About the data container and its transmission to Yandex Metrica

In E-commerce, every product item is an object that certain actions can be performed on, such as viewing the complete item description or adding it to the basket. This data is transmitted as JavaScript objects containing the action ID and a list of descriptions of items that this action was performed on. In the context of the JavaScript API, we call these objects E-commerce objects.

To transmit data as E-commerce objects to Yandex Metrica, add them to the special JavaScript array window.dataLayer in the global namespace (window) using the push method. We call this array the data container.

Alert

Don’t transmit the data when the user is going to another page on the site. An example is when an onclick event is used on the “Checkout” button. In this case, the next page might load before the tag sends the data to Yandex Metrica. As a result, information about the event is lost.

The data container must be located in the global namespace, and its name must match the name specified during tag configuration or initialization. If the data container is named dataLayer, or the Yandex.Metrica counter was initiated with the ecommerce parameter set to true, it is assumed that the data container is the window.dataLayer array.

<script type="text/javascript">
    // Tag code initialization
    ym(XXXXXX, 'init', {
        ...
        // If the “ecommerce” parameter is enabled during initialization, the data container will always be window.dataLayer
        // If you've enabled the parameter via the interface, you can change the name in the tag settings 
        ecommerce: true
        ...
    });
    // Data container (JavaScript array) in the global namespace (window)
    window.dataLayer = window.dataLayer || [];
</script>
...
<script type="text/javascript">
    // Use the “push” method to add an E-commerce object
    window.dataLayer.push(
        // E-commerce object
        {
            "ecommerce": {
                ...
                "currencyCode": "RUB",
                "<actionType>": {
                    "actionField": <actionField>,
                    "products" : [<productFieldObject>, <productFieldObject>, ...]
                }
                ...
            }
        }
    );
</script>

The name of the data container and the structure of the E-commerce objects it contains match the corresponding entities in Google Analytics Enhanced Ecommerce. This means that if you have already set up data sending to Google Analytics Enhanced Ecommerce, including via the Global Site Tag, and enabled Ecommerce in Yandex Metrica, the latter will start collecting data. Yandex Metrica also supports GA4 Ecommerce and collects data without any additional settings.

Alert

The data container may contain a maximum of 8,192 characters.
If you are sending more data in the container, we recommend splitting the order into parts with sub-numbers (e.g., order1-1, order1-2, order1-3). This way, both orders and sub-orders will be displayed in Yandex Metrica.

To assess the actual number of orders, set a JS goal and send it to Yandex Metrica with one of the sub-orders. When sending multiple containers of one order divided into sub-orders, one goal will be achieved, and you will be able to use it to calculate the total number of orders.

An ecommerce object has the following format:

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

Field

Type

Description

ecommerce*

Object

Required container field

currencyCode

String

Three-letter ISO 4217 currency code.

If a different currency is passed, null values are sent instead of currencies and amounts.

<actionType>*

The field name (substituted in place of <actionType>) is the identifier of an action performed with a set of products.

Possible values:

  • impressions: Viewing the product list.
  • click: Clicking an item in the product list.
  • detail: Viewing an item.
  • add: Adding the item to the shopping cart.
  • remove: Removing an item from the shopping cart.
  • purchase: Purchasing.
  • promoView: Viewing an internal ad.
  • promoClick: Clicking an internal ad.

If information about removing the item was transmitted to Yandex Metrica, the report might show a negative number of items (the total is calculated by subtracting the number of deleted items from the total number of added items). If the price of the item was transmitted, it might also have a negative value in the report.

actionField**

Object

An actionField object. Additional data describing the action performed.

Only processed if the action is a purchase (<actionType> — purchase).

products*

Array

List of descriptions of items that the specified action was performed on. Product descriptions are productFieldObject objects.

Not used for the promoClick, promoView, and impressions actions.

promotions*

Array

List of descriptions for the ad campaign that the specified action was performed on. Ad campaign descriptions are promoFieldObject objects.

* Required parameter.

** Required parameter for sending the purchase information.

Item data

An object describing a particular item.

The structure of the object describing the item is denoted as productFieldObject.

Field

Type

Description

id*

String

Item ID. For example, the SKU.

It is necessary to specify either id or name

name*

String

Item name. For example, “T-shirt”

You must specify either "name" or "id"

brand

String

The brand or trademark associated with the item. For example, “Yandex”

category

String

The category the item belongs to.

The hierarchy of categories supports up to 4 levels. Use the / symbol to separate levels. For example, “Clothing / Men's clothing / T-shirts”.

coupon

String

A promo code associated with the item. For example, “PARTNER_SITE_15”

discount

Number

Discount amount (a numeric value).

list

String

List that the item belongs to.

To evaluate the effectiveness of a list at different stages of user interaction with the product, we recommend specifying the product list in all events that occurred after the list was viewed.

position

Integer

Item position in the list. For example, 2

price

Number

Item price.

quantity

Integer

Item quantity.

variant

String

A variation of the item. For example, “Red”

Action data

An object containing data about an action performed with an item or set of products.

Only processed if the action is a purchase (<actionType> — purchase).

The structure of the object describing the action is denoted as actionField.

When transmitting data about an action, Yandex Metrica creates a goal. This allows you to get information about revenue from Yandex Direct ad campaigns. In the list of available goals in Yandex.Direct, this goal is shown as “eCommerce: Purchase (tag № )”. You can track goal completion yourself by transmitting the goal_id field.

Field

Type

Description

id*

String

ID of the product purchased.

Required information.

Example: TRX#54321

coupon

String

A promo code associated with the entire purchase

goal_id

Integer

The goal number. Specified if this action was the goal.
The goal must be set as a JavaScript event type.

You can view a goal number in the Yandex Metrica interface: go to the Settings section and open the Goals tab.

revenue

Number

The revenue received.

If omitted, it is calculated automatically as the sum of the prices of all the items associated with the purchase

Promo campaign data

An object describing promo campaigns.

This object uses a similar container but with promoFieldObject instead of productFieldObject.

Field

Type

Description

id*

String

ID of the promo campaign.

Required information

name

String

Promo campaign name

creative

String

Ad banner name

creative_slot

String

Ad banner slot

position

String

Ad banner position

Examples

To transmit information, you need to create a script on the site that will be responsible for a certain event (for example, making an order) in the format described above. Below are examples of scripts for actions supported by Yandex Metrica.

All the examples assume that the tag was initialized with E-commerce enabled, and data is transferred via the window.dataLayer container.

Viewing a product list

The data must be sent the moment the product list is opened.

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
            }
        ]
    }
});

Clicking a product list item

The data must be sent the moment the user clicks the product link.

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
                }
            ]
        }
    }
});

Viewing a product

The data must be sent the moment the page opens with the product card.

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
                }
            ]
        }
    }
});

Adding an item to the basket

The data must be sent at the moment when the order is added to the basket. For example, upon clicking “Add to basket”.

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
                }
            ]
        }
    }
});

Removing an item from the basket

The data must be sent at the moment when the item is removed from the basket.

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

The data must be sent the moment the order is confirmed.

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
                }
            ]
        }
    }
});

Viewing an internal ad

The data must be sent the moment the user views the ad.

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"
                }
            ]
        }
    }
});

Clicking an internal ad

The data must be sent the moment the user clicks the ad or completes another target action related to the ad.

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

Learn more

Chat with us

Write an email

Please note: Our support team will never initiate a call to you. Do not follow any instructions of people who call you and introduce themselves as the Yandex Metrica support team.


Required parameter.

Required parameter for transmitting purchase information.

You must specify either "name" or "id".

Required information.