This library will help you to get data from Yandex Metrika API. It uses mongoose style API to form requests, so if you are using mongo it will be natural to use this library without any issues.
First install lib:
npm i yandex-metrika-api
To initialize it creates new instance of service:
const ym = new YMService('MY_API_TOKEN');
Where MY_API_TOKEN
is your auth token for application. You can read more here in English or here in Russian. From this point you are ready to get data from api. Every request starts with get
method. After you can use filter
to filter results and dimentions
to form report.
const { data }: AxiosResponse<IYmResponse & IYmError> = await this.ymService
.get(dateFrom, dateTo, 'ym:s:visits', 'COUNTER_ID')
.filter([
{
filter: 'ym:s:trafficSourceName',
operator: '==',
value: 'Переходы из поисковых систем',
},
])
.dimensions([''])
.exec();
- dateFrom (string) - date in format
YYYY-MM-DD
. - dateTo (string) - date in format
YYYY-MM-DD
. - metrics (ymMetrics) - metrics
ym:s:visits
orym:s:pageviews
orym:s:users
. - COUNTER_ID (string) - counter id.
In filter
method you pass array of filters:
[
{
filter: 'ym:s:trafficSourceName',
operator: '==',
value: 'Переходы из поисковых систем',
},
{
beforeOperator: 'AND',
filter: 'ym:s:lastSearchPhrase',
operator: '!~',
value: 'brandName',
},
],
- beforeOperator (ymLogicOperator) -
OR
orAND
. Operator that will connect previous element of array to current. In this example - it will filter with first AND second condition. - filter (string) - filter name like
ym:s:trafficSourceName
orym:s:lastSearchPhrase
or so on. You can read more here. - operator (ymOperators) - operator between filter and value (docs).
- value (string) - value to compare with.
Array of strings in which dimension you want to present results. All dimensions and metrics can be found here.