# Loading data from Yandex Metrica to a ClickHouse® data mart

# Loading data from Yandex Metrica to a ClickHouse® data mart using Yandex Data Transfer


{% note info %}

You can transfer data from a Yandex Metrica source if the [Metrica Pro](https://yandex.com/support/metrica/pro/intro.html) package is enabled or when using [Data Streaming from Yandex Metrica](https://yandex.cloud/en/marketplace/products/varioqub/metrica-data-streaming).

{% endnote %}

Data Transfer enables you to transfer metrics from [Yandex Metrica](https://metrika.yandex.com/) to a ClickHouse® cluster. This approach guarantees the completeness of the data transferred from Yandex Metrica and allows:

* Processing data with ClickHouse® tools.
* Streaming data from ClickHouse® to other locations.
* Visualizing data with [Yandex DataLens](https://datalens.ru/promo) or other services.

To transfer data:

1. [Prepare and activate your transfer](#prepare-transfer).
1. [Test the transfer](#verify-transfer).

If you no longer need the resources you created, [delete them](#clear-out).


## Required paid resources {#paid-resources}

* Managed Service for ClickHouse® cluster, which includes the use of computing resources allocated to hosts, storage and backup size (see [Managed Service for ClickHouse® pricing](../../managed-clickhouse/pricing.md)).
* Public IP addresses if public access is enabled for cluster hosts (see [Virtual Private Cloud pricing](../../vpc/pricing.md)).
* [Metrica Pro](https://yandex.com/support/metrica/pro/price.html).


## Getting started {#before-you-begin}

Set up the infrastructure:

1. [Select](https://yandex.com/support/metrica/general/tag-id.html) an existing metric in Yandex Metrica or [create and install](https://yandex.com/support/metrica/general/creating-counter.html) a new one.

1. Create a [target Managed Service for ClickHouse® cluster](../../managed-clickhouse/operations/cluster-create.md) with your preferred configuration.

## Prepare and activate your transfer {#prepare-transfer}

1. [Create an endpoint](../operations/endpoint/index.md#create) for the [`Metrica` source](../operations/endpoint/source/metrika.md).

    Hits and sessions are transferred as separate tables.

1. [Create a target endpoint](../operations/endpoint/index.md#create):

    * **Database type**: `ClickHouse`
    * **Endpoint parameters** → **Connection type**: `Managed cluster`

        Select your target cluster from the list and specify its [connection settings](../operations/endpoint/target/clickhouse.md).

1. [Create a transfer](../operations/transfer.md#create) of the **_Replication_**-type that will use the endpoints you created.
1. [Activate](../operations/transfer.md#activate) the transfer.

Transfers process only the latest data, leaving historical data untouched. If you deactivate and then reactivate the transfer:

1. Data collected by the Yandex Metrica tracking tag while the transfer was deactivated will not be migrated.
1. Depending on the cleanup policy selected in the target endpoint, existing data tables will be:

    * **Drop**: Deleted along with all data and recreated with the same names.
    * **Truncate**: Truncated while preserving their schemas.
    * **Don't cleanup**: Left intact for future data writing.

## Test the transfer {#verify-transfer}

1. Wait for the transfer status to change to **Replicating**.

1. Make sure the metric data from Yandex Metrica has been transferred to the Managed Service for ClickHouse® database:

    1. [Connect to the cluster](../../managed-clickhouse/operations/connect/clients.md#clickhouse-client) using `clickhouse-client`.

    1. Verify that the hit and session tables have appeared in the database:

        ```sql
        SELECT table FROM system.tables
        WHERE database = '<ClickHouse®_database_name>'
        ```

        Result:

        ```text
        ┌─table───────────────────────┐
        │ hits_dt...                  │
        │ visits_dt...                │
        └─────────────────────────────┘
        ```

    1. Check whether the hit and session tables contain the relevant metric data:

        ```sql
        SELECT * FROM <name_of_hit_or_visit_table>
        ```

## Delete the resources you created {#clear-out}

{% note info %}

Before deleting the created resources, [deactivate the transfer](../operations/transfer.md#deactivate).

{% endnote %}

To minimize resource consumption, delete the resources you no longer need:

1. [Delete the transfer](../operations/transfer.md#delete-transfer).
1. [Delete the source and target endpoints](../operations/endpoint/index.md#delete).
1. [Delete the Managed Service for ClickHouse® cluster](../../managed-clickhouse/operations/cluster-delete.md).
1. Delete the Yandex Metrica tracking counter from your [Yandex Metrica Pro](https://yandex.com/support/metrica/pro/intro.html) account.

_ClickHouse® is a registered trademark of [ClickHouse, Inc](https://clickhouse.com)._