📜 ⬆️ ⬇️

Overview of R packages for online marketing, part 1

Hello everyone, if you do internet marketing, you probably have to deal with a lot of advertising services every day and at least one web analytics platform, if you need to at least once a month, or maybe even once a week with your hands to compile data on expenses, and other statistical information from all sources is fraught with not only large time costs, but also the probability of error when consolidating data from many sources in manual mode is quite large. In this article I’ll give you tips on ready-made extensions (packages) for the R language, with which you can automate the process of collecting data from most popular advertising systems and web analytics platforms.
image


What software we need


Since In this article we are talking about the R language, then you will need R itself and the RStudio development environment.


Both the R language and RStudio are free, free software, so you can freely download them from official sites.


How to get started with R packages


There is nothing difficult to get started with R packages, this is done as follows:


  1. Installing a package, usually with the base install.packages command;
  2. Connecting the package, each time you start a new session with R, this operation can be performed using the library or require command.

The difference between library and require is only that the library will generate an error if you try to connect a package that you have not installed, and require in the same case just shows a warning, while the script will continue.


To install the GitHub packages described in this article, you first need to install the devtools package, to do this, run the install.packages("devtools") .


CRAN or GitHub


In the course of reading this article, you may have a question from which repository it is better to install one or another package, in fact, on GitHub, as a rule, the most current dev versions of packages from developers, They have all the new features that may not be in the version of the package that is available on CRAN, but, accordingly, the dev version may not be sufficiently tested at the time of loading and contain errors.


The version on CRAN is carefully checked by a team of specialists before it is downloaded, and must comply with all the requirements of this repository policy. If you download a package from CRAN, you can be sure that it will not harm your data in any way and will not create any incomprehensible files on your computer without your permission. PC etc.


What skills are needed to work with R packages


In fact, you only need to understand the subject area of ​​Internet marketing, no programming skills are required in this case, because we will talk about ready-made packages, using functions from which you can automate the ruined part of your work.


The article will not have a detailed description of how to work with each function of the packages in question, since such an article would be too long, but I will describe the capabilities of each package, give a list of the main functions that you need, as well as give a link to the official documentation, with which you can already deal in most detail with all the functions available in one package or another. . In addition to each package, I will give a small sample of the code, so that you would have a clearer syntax.


ryandexdirect - download data from Yandex.Direct


Installing the package from GitHub: devtools::install_github("selesnow/ryandexdirect")
Official documentation: link


This package is designed to work with the API of one of the most popular advertising sites in the CIS - Yandex.Direct.


Ryandexdirect features


  1. Authorization in the Yandex API;
  2. Download a list of ad campaigns, keywords, ads;
  3. Loading statistics from Yandex.Direct accounts using the Reports service .
  4. Stop and resume ad impressions at the level of advertising campaigns, keywords and ads.
  5. Request balance and other parameters of the general account.
  6. Downloading reference information (exchange rates, geographical directory, etc.)

Main functions


The main functions available in ryandexdirect that you may need to work with the Yandex.Direct API:



Sample code for downloading statistics using ryandexdirect
 library(ryandexdirect) stat <- yadirGetReport(ReportType = "ACCOUNT_PERFORMANCE_REPORT", DateRangeType = "CUSTOM_DATE", DateFrom = "2018-01-01", DateTo = "2018-05-10", FieldNames = c("AdNetworkType", "Impressions", "Clicks", "Cost"), FilterList = c("CampaignId IN 123456 ,987654","Clicks GREATER_THAN 100"), IncludeVAT = "YES", IncludeDiscount = "NO", Login = " ", TokenPath = "C:/token_yandex") 

More details about each argument can be found in the official ryandexdirect or Yandex.Direct API Reports service .


RAdwords - download data from Google Ads (ex Google AdWords)


Installing a package with CRAN: install.packages("RAdwords")
Installing the package from GitHub: devtools::install_github("jburkhardt/RAdwords")
Official documentation: link


RAdwords Package Features


The main feature of this package is to authorize the Google Ads API and download statistics from all reports available on this interface.


The main functions of the package RAdwords


In fact, the package basically contains not very many functions, but I have used only three of them all the time:



An example of working with the RAdwords package
 library(RAdwords) #   adw_token <- doAyth() #   body <- statement(select=c('AccountDescriptiveName', 'ExternalCustomerId', 'AccountCurrencyCode', 'AdNetworkType1', 'RegionCriteriaId', 'CountryCriteriaId', 'CityCriteriaId', 'Device', 'Month', 'Year', 'Impressions', 'Clicks', 'Interactions', 'VideoViews', 'Cost', 'Conversions' ), report="GEO_PERFORMANCE_REPORT", start="2018-09-01", end="2018-09-20") my_data <- getData(clientCustomerId = "000-000-0000", google_auth = adw_token , statement = body, transformation = T) 

The list of reports available in the Google Ads API and fields can be found at the link in the official help.


adwordsR is a newer and more functional package for download = and data from Google Ads (ex Google AdWords)


Installing a package with CRAN: install.packages("adwordsR")
Installing a package from GitHub: devtools::install_github("cran/adwordsR")


The adwordsR package is similar in meaning to RAdwords, but it is newer and has a much larger set of functions.


AdwordsR features


In addition to the features of the RAdwords package described above, adwordsR gives you ample opportunities to work with TargetingIdeaService, the service with which you can get targeting options directly from a tool or platform that helps automate account optimization.


Main functions of the adwordsR package



An example of working with the adwordsR package
 library(adwordsR) #   adw_token <- generateAdwordsToken(saveNewToken = TRUE, addGitignore = FALSE) #     adw_token <- loadAdwordsToken() #   AdWordsData <- getReportData(reportType = "CAMPAIGN_PERFORMANCE_REPORT", startDate = "2018-08-01", endDate = "2018-08-15", clientCustomerId = "000-000-0000", credentials = adw_token, attributes = "CampaignName,CampaignStatus", segment = "Date,AdNetworkType1", metrics = "AverageCost,Clicks,Conversions", includeZeroImpressions = TRUE, useRequestedHeaders = FALSE) 

rfacebookstat - download data from Facebook advertising offices


Installing a package with CRAN: install.packages("rfacebookstat")
Installing the package from GitHub: devtools::install_github("selesnow/rfacebookstat")
Official documentation: link


Features of rfacebookstat


  1. Facebook API Login
  2. Download a list of business managers available to you;
  3. Download a list of advertising accounts;
  4. Download a list of ad campaigns, ad groups, ads.
  5. Download statistics at the level of the advertising account, advertising campaigns, ad groups and ads.
  6. Download a list of users who have access to the advertising account.
  7. Manage access to advertising accounts.

Main features of rfacebookstat



Sample code for working with rfacebookstat
 library(rfacebookstat) token <- fbGetToken(app_id = 00000000000000) fb_data <- fbGetMarketingStat(accounts_id = "act_00000000000000", level = "campaign", fields = "campaign_name,actions", action_breakdowns = "action_link_click_destination", date_start = "2017-11-01", date_stop = "2017-11-20", interval = "day", access_token = token) 

rvkstat - loading data from the Vkontakte API


Installing the package from GitHub: devtools::install_github("selesnow/rvkstat")
Official documentation: link


Enough powerful and multi-functional package for working with the API of the social network Vkontakte.


Rvkstat features


  1. Authorization in the API Vkontakte;
  2. Download data from advertising offices Vkontakte;
  3. Downloading reference information from Vkontakte;
  4. Download some information about the user Vkontakte;
  5. Download statistics from the advertising office Vkontakte;
  6. Download statistics on visits to Vkontakte communities.

The main functions of the rvkstat package


The rvkstat package at the time of this writing contains more than 30 functions, here are the most useful ones:



Sample code for working with the rvkstat package
 library(rvkstat) my_tok <- vkAuth(app_id = 111,app_secret = "efbscuyewb7cb4ru4bj") #    camp <- vkGetAdCampaigns(account_id = 1, access_token = my_tok$access_token) #     vk_stat_by_campaign <- vkGetAdStatistics(account_id = 1, ids_type = "campaign", ids = camp$id , period = "day", date_from = "2010-01-01", date_to = "2017-09-10", access_token = my_tok$access_token) 

rmytarget - loading data from the MyTarget API


Installing a package with CRAN: install.packages("rmytarget")
Installing the package from GitHub: devtools::install_github("selesnow/rmytarget")
Official documentation: link


Rmytarget package features


  1. Authorization in the MyTarget API according to the Code Grant scheme;
  2. Download a list of ad campaigns, advertisements.
  3. Download statistics at the level of advertising campaigns and announcements.

The main features of the rmytarget package



Sample code for working with rmytarget
 library(rmytarget) #    Ads <- myTarGetAdList(login = " ") #     a_stat <- myTarGetStats(date_from = "2016-08-01", date_to = "2016-08-10", object_type = "banners", object_id = Ads$id, stat_type = "day", login = " ") 

rym - loading data from the Yandex.Metrics API


Installing a package with CRAN: install.packages("rym")
Installing the package from GitHub: devtools::install_github("selesnow/rym")
Official documentation: link


Key features of the rym package


The rym package allows you to work with all APIs available in Yandex.Metrica:


  1. Authorization using the oAuth2 protocol;
  2. Work with management APIs ;
  3. Work with reports API ;
  4. Work with API compatible with Core API Google Analytics v3 ;
  5. Work with Logs API .

The main functions of the rym package



An example of working with the rym package
 reporting.api.stat <- rym_get_data(counters = "00000000,111111111", date.from = "2018-08-01", date.to = "yesterday", dimensions = "ym:s:date,ym:s:lastTrafficSource", metrics = "ym:s:visits,ym:s:pageviews,ym:s:users", filters = "ym:s:trafficSourceName=='   ' AND ym:s:isNewUser=='Yes'", sort = "-ym:s:date", accuracy = "full", login = " ", token.path = "metrica_token", lang = "ru") 

Conclusion


So, using the packages in this article, you can easily automate the collection of data from such sources as Yandex.Direct, Google Ads, Facebook, Vkontakte, MyTarget and Yandex.Metrica. Of course, this is far from an exhaustive list of services that are used by marketing specialists, but it’s quite difficult to fit all the tools into one article, so if this article gets positive reviews, I’ll write a sequel.


Further, the obtained data can be converted using the dplyr package, and recorded in various databases using the DBI interface, but these are already quite voluminous topics for the following articles.


If you also have interesting packages in your arsenal that can help automate data collection from advertising sources or web analytic platforms, leave them in the comments to this article.


')

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


All Articles