How to configure mailing of reports from Yandex.Metrica using R (from scratch)
Yandex. The metric is a great tool for collecting data about site visits, but, unfortunately, it happens that the web interface lacks one or another functionality - for example, automatic sending of a report. In this article I will describe in detail how to get robotic statistics using the R language.
1.1. Download and install the current version of R , as well as the integrated development environment of R Studio , in which you will be more comfortable working.
1.2. In R-Studio, create a new file and paste the code:
1.3. To start the package installation process, select all the text and click “Run”
2.1. We create the application oauth.yandex.ru/client/new
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.1.
We paste the code into R-Studio: Change: setwd, appToken, counterID, recipient mail, your mail and password
* If R-Studio swears on xlsx, then follow the link and download the corresponding version of java www.java.com/en/download/manual.jsp
** available metrics and groupings - you can see here
3.2. We go to your Gmail account and give permission to interact with "untrusted applications" (otherwise the letter will not be sent)
myaccount.google.com/u/4/security?hl=en&pageId=none#connectedapps
3.3. We configure the sending schedule:
3.4 Checking the task scheduler (Control Panel -> Schedule of task completion)
If there is a task, but the message is not sent - open the task properties, the tab "Actions" -> Change -> put 1 at the end of the line
Benefits of automatic reporting:
- You can pre-configure the required report format and not waste time unloading when approaching deadlines;
- There are no restrictions on the format and frequency of unloading
1. Install the R language and the required libraries
1.1. Download and install the current version of R , as well as the integrated development environment of R Studio , in which you will be more comfortable working.
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 the access token for the Yandex.Metrica API
2.1. We create the application oauth.yandex.ru/client/new
- Rights -> Yandex.Metrica, select "Obtaining statistics" and "creating counters" above;
- Callback URL -> select “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.
We paste the code into R-Studio: Change: setwd, appToken, counterID, recipient 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 corresponding version of java www.java.com/en/download/manual.jsp
** available metrics and groupings - you can see here
3.2. We go to your Gmail account and give permission to interact with "untrusted applications" (otherwise the letter will not be sent)
myaccount.google.com/u/4/security?hl=en&pageId=none#connectedapps
3.3. We configure the sending schedule:
3.4 Checking the task scheduler (Control Panel -> Schedule of task completion)
If there is a task, but the message is not sent - open the task properties, the tab "Actions" -> Change -> put 1 at the end of the line