# Yandex.Metrica

[Yandex.Metrica](https://metrica.yandex.com/) is a web analytics platform that lets you get an in-depth understanding of your audience's behavior and use the insights to drive business growth.

Find the open source code for this destination in the [GitHub repository](https://github.com/rudderlabs/rudder-sdk-js/tree/develop/packages/analytics-js-integrations/src/integrations/YandexMetrica).

## Connection compatibility

{{< destination-config >}}

## Get started

Once you have confirmed that the source platform supports sending events to Yandex.Metrica, follow these steps:

1. From your [RudderStack dashboard](https://app.rudderstack.com/), add the source. Then, from the list of destinations, select **Yandex.Metrica**.
2. Assign a name to your destination and click **Continue**.

### Connection settings 

To successfully set up Yandex.Metrica as a destination, you need to configure the following settings:

- **Tag ID**: Enter your Yandex.Metrica tag ID.
- **Enable Clickmap**: Enable this setting to collect data for [click mapping](https://yandex.com/support/metrica/code/counter-initialize.html#:~:text=a%20child%20window-,clickmap,-true).
- **Enable Tracklinks**: Enable this setting to track user clicks on the [outbound links](https://yandex.com/support/metrica/code/counter-initialize.html#:~:text=browser%27s%20address%20bar-,trackLinks,-true).
- **Enable Accurate Track Bounce**: This setting enables the accurate bounce rate with a non-bounce event that is registered after 15 seconds (15000 ms). For more information on this setting, refer to the [Yandex.Metrica documentation](https://yandex.com/support/metrica/code/counter-initialize.html#:~:text=Description-,accurateTrackBounce,-true).
- **Enable WebVisor**: Enable this setting to use Yandex.Metrica's [sesssion replay feature](https://yandex.com/support/metrica/code/counter-initialize.html#:~:text=1%20for%20YAN-,webvisor,-false).
- **Container name**: Enter the name of your Yandex.Metrica ecommerce data container that you set while [creating a new counter](https://yandex.ru/support/metrica/general/creating-counter.html). If not specified, RudderStack sets it to `dataLayer` by default.
- **Map your event name with supported Yandex.Metrica event name**: Use this setting to map the RudderStack event to the Yandex.Metrica event selected from the dropdown.
- **Goal ID**: Enter your Yandex.Metrica goal ID.
- **Client-side Events Filtering**: This setting lets you specify which events should be blocked or allowed to flow through to Yandex.Metrica. For more information on this setting, refer to the [Client-side Events Filtering]({{< ref "sources/event-streams/sdks/event-filtering.md" >}}) guide.
- **Use device mode to send events**: As this is a [web device mode]({{< ref "destinations/rudderstack-connection-modes.md#device-mode" >}})-only destination, this setting is enabled by default and cannot be disabled.
- **Consent management settings**: Configure the consent management settings for the specified source by choosing the **Consent management provider** from the dropdown and entering the relevant consent category IDs. See [Consent Management in RudderStack]({{< ref "data-governance/consent-management/overview.md" >}}) for more information on this feature.

{{< info >}}
Refer to the [FAQ]({{< ref "#faq" >}}) section for more information on obtaining the Yandex.Metrica tag ID, container name, and goal ID.
{{< /info >}}

## Identify

You can use the [`identify`]({{< ref "event-spec/standard-events/identify.md" >}}) call to identify a user in Yandex.Metrica. If a contact already exists, RudderStack updates the contact details.

A sample `identify` call is shown below:

```javascript
rudderanalytics.identify('1hKOmRA4el9Z', {
      firstName: 'Alex',
      lastName: 'Keener',
      email: "alex@example.com"
    }
```

RudderStack uses Yandex.Metrica's [`setUserID`](https://yandex.com/support/metrica/objects/set-user-id.html?lang=en) method to associate the `userId` with the user's client ID during a particular session. To update the information of a user with a specfic `userId`, RudderStack uses the [`userParams`](https://yandex.com/support/zout_metrica-de-2k55914b38f571cgc45c3db06adedbd0/objects/user-params.html?lang=en) method.

### Supported mappings

The following table lists the mappings between the RudderStack attributes and the Yandex.Metrica properties:

| RudderStack property | Yandex.Metrica property |
| :--------------| :---------|
| `userId` <br/> `anonymousId` <br/> <span style="color: #4D4DFF;font-size:12px;">Required</span> | `UserID` |

## Track

You can use the [`track`]({{< ref "event-spec/standard-events/track.md" >}}) call to send your [ecommerce events]({{< ref "event-spec/ecommerce-events-spec/_index.md" >}}) to Yandex.Metrica.

A sample `track` call is shown below:

```javascript
rudderanalytics.track("Order Completed", {
  order_id: "34897497",
  coupon: "BB20",
  currency: "INR",
  products: [{
      product_id: "43521",
      currency: "INR",
      name: "Rudder Bag",
      price: 3700.99,
      brand: "Nice",
      category: "Bags",
      quantity: 3,
    },
    {
      product_id: "37333",
      currency: "INR",
      name: "RudderStack T-shirt",
      price: 1651.55,
      brand: "Adidas",
      category: "Bags",
      quantity: 3,
    },
  ],
});
```

### Event mapping

{{< warning >}}
This destination does not strictly adhere to the [RudderStack Ecommerce Event Spec]({{< ref "event-spec/ecommerce-events-spec/" >}}).
{{< /warning >}}

The following table lists the default mappings between the RudderStack events and the Yandex.Metrica events:

| RudderStack property | Yandex.Metrica property | Description |
| :--------------| :---------| :-----|
| `Product Viewed` | `detail` | View the full description of the product. |
| `Product List Viewed` | `detail` | View the full description of the product list. |
| `Product Added` | `add` | Add an item to the basket. |
| `Product Removed` | `remove` | Remove an item from the basket. |
| `Order Completed` | `purchase` | Complete the product purchase. | 

You can also map any other event to the above Yandex.Metrica events via the dashboard settings.

### Supported property mappings

The following table lists the properties mapping for Yandex.Metrica's `detail`, `add`, and `remove` events:

| RudderStack property | Yandex.Metrica property |
| :--------------| :---------|
| `properties.product_id` <br/> `properties.sku` <br/> `properties.products[index].product_id` <br/> `properties.products[index].sku` <br/> <span style="color: #4D4DFF;font-size:12px;">Required, if name is not present.</span> | `id` |
| `properties.name` <br /> `properties.products[index].name` <br/> <span style="color: #4D4DFF;font-size:12px;">Required, if product_id/sku is not present.</span> | `name` |
| `properties.brand` <br /> `properties.products[index].brand` | `brand` |
| `properties.category` <br /> `properties.products[index].category` | `category` |
| `properties.coupon` <br /> `properties.products[index].coupon` | `coupon` |
| `properties.position` <br /> `properties.products[index].position` | `position` |
| `properties.price` <br /> `properties.products[index].price` | `price` |
| `properties.quantity` <br /> `properties.products[index].quantity` | `quantity` |
| `properties.variant` <br /> `properties.products[index].variant` | `variant` |

The following mappings are applicable only for the `purchase` event:

| RudderStack property | Yandex.Metrica property |
| :--------------| :---------|
| `properties.order_id` <br/> <span style="color: #4D4DFF;font-size:12px;">Required</span> | `id` |
| `properties.revenue` | `revenue` |

## Page

You can use the [`page`]({{< ref "event-spec/standard-events/page.md" >}}) call to send all page view data to Yandex.Metrica.

A sample `page` call is shown below:

```javascript
rudderanalytics.page(
  "Cart",
  "Cart Viewed", {
    referrer: "https://www.mysite.com/search?q=bestseller",
    title: "The best seller items",
    url: "https://www.mysite.com/bestseller/1"
  });
```

RudderStack uses Yandex.Metrica's [`hit`](https://yandex.com/support/metrica/objects/hit.html) method to send all page view data.

### Supported mappings

The following table lists the mappings between the RudderStack attributes and the Yandex.Metrica properties:

| RudderStack property | Yandex.Metrica property |
| :--------------| :---------|
| `context.page.url` <br/> <span style="color: #4D4DFF;font-size:12px;">Required</span> | `url` |
| `context.page.title` | `title` |
| `context.page.referrer` | `referer` |

## FAQ 

#### What is the Yandex.Metrica container name? Where can I find it?

You can set your Yandex.Metrica data container name while setting up your counter for your website:

{{< image src="images/event-stream-destinations/yandex-metrica-container-name.webp" alt="Yandex.Metrica container name" >}}

#### Where can I find my Yandex.Metrica tag ID?

To obtain your Yandex.Metrica tag ID, follow these steps:

1. Log into your [Yandex.Metrica](https://metrica.yandex.com/) dashboard. 
2. From the left sidebar, click **Visitors**. You can see the tag ID listed in the dashboard as well as in the URL:

{{< image src="images/event-stream-destinations/yandex-metrica-tag-id.webp" alt="Yandex.Metrica tag ID" >}}

#### Where can I find my Yandex.Metrica goal ID?

To obtain your Yandex.Metrica tag ID, follow these steps:

1. Log into your [Yandex.Metrica](https://metrica.yandex.com/) dashboard. 
2. From the left sidebar, click **Goals**. You can see the goal ID listed in the dashboard against your created targets/goals:

{{< image src="images/event-stream-destinations/yandex-metrica-goal-id.webp" alt="Yandex.Metrica goal ID" >}}

