Intro to rym

knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )

Intro to rym



  • Short description
  • Package syntax
  • Package installation
  • Authorization in Yandex. Metrics API

Short description

rym is an R interface for working with the Yandex Metrics API, its functions allow you to interact with the following APIs:

  1. Management API - allows you to get tables with such objects as Yandex.Metrics sufficient counters, a list of configured goals, filters and segments, as well as a list of users who have access to the counter.
  2. API Отчётов - allows you to receive information on the statistics of site visits and other data without using the Yandex.Metrica interface.
  3. API совместимый с Core API Google Analytics (v3) - allows you to query statistics using the names of the fields are the same as when working with Core Reporting API v3.
  4. Logs API - allows you to get raw, ungrouped data about your visit to your site from Yandex.Metrica.


For convenience, and a quick search for functions, all the functions of the rym package begin with therym prefix. The names of the functions are given in snake_register (snake_case), i.e. The name is written in lower case and separated by an underscore, (approx. rym_get_data) . Imana arguments are also written in lower case, but are separated by a dot (approx. token.path) .

Installing rym

The rym package can be installed from the main repository to store the R CRAN packages, as well as the dev version from GitHub. Installing with CRAN is done with the standard command: install.packages (" rym ").

To install rym from github you need thedevtools package.

install.packages("devtools") devtools::install_github("selesnow/rym")

Authorization in the Yandex.Metrics API

To work with the Yandex.Metrica API, you initially need to go through [authorization] (, there is a separate function rym_auth for this purpose. But in general, there is no need to go through authorization using this function. for any access to the API, using any of the available functions in the package, the authorization process will be started, which in rym happens as follows.

  1. When running any package function, a file with credentials is initially searched for in the folder specified in the argument token.path. The file name consists of login.rymAuth.RData, where login is the value specified in the argument with the same name. Thus, during one R-session, you can work with the counters available under any number of user accounts.
  2. If you have already gone through the authorization process and have given permission to record the received credentials in a local file, then the credentials will be loaded from there.
  3. If you are logging in for the first time or in the token.path argument you specified a folder in which the file with credentials was not previously saved, you will be redirected to the browser in which you must allow access to your account data. After that, you will be taken to the page where the seven-digit code will be generated to confirm authorization. Copy and paste it into the R-console in response to the query "Enter authorize code:".
  4. Next, you will be asked for permission to create the received credentials in the local file “Do you want save API credential in local file token.path / login.rymAuth.RData”. The request must be answered with one of the following values: yes, ok or save.
  5. After that, the login.rymAuth.RData file is saved in the folder specified in the token.path argument, and the next time you access the API, if you specify the same folder in the token.path argument, the credentials for accessing the API will be loaded from the login.rymAuth.RData file.

At the same time, in order to be able to work in the same R session with different Yandex.Metrica accounts, the following arguments are available to you in all the functions of the package:

  • login - user login, under which the Yandex.Metrica counter is available to you, from which you will request data;
  • new.user - a sign that the user must request permission to access the account (even if the user has already allowed access to this application). After receiving this parameter, Yandex.OAuth will prompt the user to allow access to the application and select the desired Yandex account;
  • token.path - the path to the folder where the file will be created to store your credentials to work with the Yandex.Metrica API.

Using these arguments, you can organize work with several user accounts in one R script.

Sample code to go through the authorization process

library(rym) rym_auth(login = "ваш логин", token.path = "C:/my_tokens/")

Before using this code, replace "your login", with the Yandex.Metric user login under which the account you need is available.