📜 ⬆️ ⬇️

How to set up mailing reports from Yandex. Metrics using R (from scratch)

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:



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"

image

2. Get access tokens to Yandex.Metrics API


2.1. Create an application oauth.yandex.ru/client/new


image

We get the following:

image

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:

image

2.4. Copy and save the token:

image

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

image

3.3. Set up the shipping schedule:

image

image

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

image

Source: https://habr.com/ru/post/337518/


All Articles