Yandex. Metrics is a great tool for collecting data on site visits, but, unfortunately, it happens that the web interface lacks this or that functionality - for example, automatic report sending. In this article I will describe in detail how to get statistics on robotics using the language R.
Benefits of automatically sending reports:
- You can pre-configure the required report format and not waste time on uploads when deadlines approach;
- There are no restrictions on the format and frequency of unloading
1. Install the R language and the necessary libraries
1.1. Download and install the
current version of R , as well as the
R Studio integrated development environment in which it will be more convenient for you to work.
1.2. In R-Studio, create a new file and paste the code:
')
install.packages("xlsx") install.packages("mailR") install.packages("taskscheduleR")
1.3. To start the package installation process, select all the text and click "Run"

2. Get access tokens to Yandex.Metrics API
2.1. Create an application
oauth.yandex.ru/client/new- Rights -> Yandex.Metrica, select “Getting statistics” and “creating counters” above;
- Callback URL -> choose “substitute URL for development”;
- Save

We get the following:
2.2. We follow the link:
oauth.yandex.ru/authorize?response_type=token&client_id= <application identifier>, where instead of <identifier> we substitute our ID value.
2.3. We give permission:
2.4. Copy and save the token:

3. Set up automatic report sending.
3.1. Paste the code into R-Studio:
Change: setwd, appToken, counterID, recipient's mail, your mail and password
library(xlsx) library(mailR) setwd("D:/R") appToken <-" " date1 <-format(Sys.Date()-1, "%Y-%m-%d") date2 <-format(Sys.Date()-1, "%Y-%m-%d") counterID <-" " metrics <-"ym:s:visits,ym:s:robotPercentage" dimensions <-"ym:s:UTMSource" api_request <-paste("https://api-metrika.yandex.ru/stat/v1/data.csv?id=",counterID,"&date1=",date1,"&date2=",date2,"&metrics=",metrics,"&dimensions=",dimensions,"&oauth_token=",appToken,sep="") chem <-read.csv(file=api_request, encoding = "UTF-8") filename <-paste("yandexbots_",date1,".xlsx",sep="") write.xlsx(chem, file=filename) textofbody <-paste (" ! ", date1, sep="") send.mail(from = "your_email@gmail.com", to = " ", subject = "", body = textofbody, encoding = "utf-8", smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "your_email@gmail.com", passwd = " ", ssl = T), authenticate = TRUE, send = TRUE, attach.files = filename, file.names = filename, debug = TRUE)
* If R-Studio swears on xlsx, then follow the link and download the appropriate version of java
www.java.com/en/download/manual.jsp** Available metrics and groupings -
can be found here.3.2. Go to your Gmail account and give permission to interact with "unreliable applications" (otherwise the letter will not be sent)
myaccount.google.com/u/4/security?hl=en&pageId=none#connectedapps
3.3. Set up the shipping schedule:

3.4 Checking the Task Scheduler (Control Panel -> Task Schedule)
If the task is, but the letter is not sent - open the properties of the task, the tab “Actions” -> Change -> put 1 at the end of the line
