E-commerce

Tip. Working with the JavaScript API requires knowledge of HTML and JavaScript. If you don't have these skills, contact your website developer or webmaster.

Yandex.Metrica offers features for collecting and analyzing e-commerce data.

Enabling E-commerce

To connect E-commerce:

  1. In the Settings section, go to the Tag tab and enable the option Dispatch ecommerce data to Metrica. A container for collecting data from dataLayer will be added to the code snippet by default.

  2. Embed (or update) the code snippet on all the site pages that contain information about products and orders (item details, the “Add to basket” button, and so on).
  3. Put the dataLayer container on the site pages and configure sending events involving products to Yandex.Metrica.

Data representation and transfer

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 to the Yandex.Metrica service as e-commerce objects, you need to put them in a special JavaScript array using the push method. We call this array the data container.

Attention. 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 tag was initiated with the e-commerce parameter set to true, it is assumed that the data container is the window.dataLayer array.

<script type="text/javascript">
window.dataLayer = window.dataLayer || [];
</script>
...
<script type="text/javascript">
window.dataLayer.push ({...});
</script>

The name of the data container and the structure of the e-commerce objects in it correspond to the same entities in Google Analytics Enhanced E-commerce. This means that if you have already set up transmitting data to Google Analytics Enhanced E-commerce (including via the Global Site Tag) and enabled E-commerce in Yandex.Metrica, the latter will start collecting data the same way.

Note. The data container may contain a maximum of 2048 characters.

An e-commerce object has the following format:

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

Object

Required container field

currencyCode

String

Three-letter ISO 4217 currency code.

If a different currency is passed, null values will be 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:

  • detail — Viewing the full item description (product profile).
  • add — Adding the item to the basket.
  • remove — Removing the item from the basket.
  • purchase — Purchasing.

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

<actionField> type of object. Additional data describing the action performed.

Processed only if the action is a purchase (actionType>purchase).

products *

Array

List of descriptions of items that the specified action was performed on. Item descriptions are <productFieldObject> objects.

* Required parameter.

** Required parameter for transmitting purchase information.

Item data

An object describing a particular item.

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

Object field
FieldTypeDescription
id *

String

Item ID. For example, the SKU.

You must specify either "name" or "id"

name *

String

Name of the product. 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"

position

Number

The position of the item in the list. For example, 2

price

Number

Price of a product unit

quantity

Integer

Quantity of product units

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.

Processed only 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 № <tag ID>)”. You can track goal completion yourself by transmitting the goal_id field.

Object field
FieldTypeDescription
id *

String

Purchase ID.

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.

To see the goal number, go to Settings (the Goals tab) in the Yandex.Metrica interface.

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

Examples

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

Viewing the full item description

dataLayer.push({
    "ecommerce": {
        "detail": {
            "products": [
                {
                    "id": "P15432",
                    "name" : "T-shirt",
                    "price": 477.60,
                    "brand": "Yandex",
                    "category": "Clothing/Men's clothing/T-shirts",
                    "variant" : "Red"
                },
                {
                    "name": "Yandex pin",
                    "price": 50,
                }
            ]
        }
    }
});

Adding an item to the basket

dataLayer.push({
    "ecommerce": {
        "add": {
            "products": [
                {
                    "id": "43521",
                    "name": "Yandex bag",
                    "price": 654.32,
                    "brand": "Yandex",
                    "category": "Accessories/Bags",
                    "quantity": 2
                }
            ]
        }
    }
});

Removing an item from the basket

dataLayer.push({
    "ecommerce": {
        "remove": {
            "products": [
                {
                    "id": "15243",
                    "name": "Set of Yandex phone screen dusting cloths",
                    "category": "Mobile phone accessories",
                    "quantity": 1
                }
            ]
        }
    }
});

Purchase

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

Solutions to problems

If the information transmitted using E-commerce doesn't show up in Yandex.Metrica reports, consider the following reasons:

  • Errors in the transmitted fields. To check the validity of your data, use the JSON.stringify(dataLayer) command in the browser console. For validation, we recommend contacting your webmaster or other person responsible for maintaining the website.

  • The actionField field doesn't contain data. To transmit information about a purchase, you must use actionField.
  • The tag might be blocked by the Adblock Plus extension.
  • The user left the page before the tag loaded.
  • The page has a redirect loop.