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 a basic command install.packages;
  2. Connecting the package, each time you start a new session with R, this operation can be performed using the libraryor command require.

The difference between libraryand requirelies only in the fact that it librarywill generate an error in case of an attempt to connect a package that you have not installed, and requirein the same case it will simply show a warning, while the script will continue.


To install the packages listed in the article with GitHub, you first need to install the devtools package, to do this, run the command 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 the one or another package, in fact, on GitHub, as a rule, the most current dev versions of packages from developers, i.e. 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 may 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:


  • yadirAuth - Authorization in the API;
  • yadirGetAds, yadirGetCampaignList, yadirGetKeyWords- Download the list of advertisements, advertising campaigns, keywords;
  • yadirGetReport - Download statistics from Yandex.Direct.
  • yadirGetDictionary - Download reference information.
  • yadirStartAds, yadirStartCampaigns, yadirStartKeyWords-

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 a package with 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:


  • doAuth - Authorization;
  • statement - Formation of the request body to the API;
  • getData - Download data from the API.

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 with 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


  • generateAdwordsToken - Request a token to work with the Google Ads API;
  • loadAdwordsToken - Download the requested and previously saved token;
  • getReportData - Download statistics from your Google Ads account.

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 a package with 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


  • fbGetToken - Authorization in the Facebook API;
  • fbGetBusinessManagers - Download a list of business manager;
  • fbGetAdAccounts - Download a list of advertising accounts;
  • fbGetCampaigns, fbGetAds, fbGetAdSets- Download the list of campaigns, ad groups and ads;
  • fbGetMarketingStat - Download statistics from advertising accounts Facebook.

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:


  • vkAuth - Authorization;
  • vkGetAdStatistics - Download statistics from the advertising office;
  • vkGetAdCampaigns, vkGetAds- Download the list of promotional campaigns and advertisements;
  • vkGetGroupStat - Download statistics on visiting groups and communities Vkontakte.

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 a package with 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


  • myTarAuth - Authorization;
  • myTarGetAdList, myTarGetCampaignList- Download the list of advertisements and promotional campaigns;
  • myTarGetStats - Download statistics for ads and advertising campaigns.

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 a package with 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


  • rym_auth - Authorization;
  • rym_get_counters - Download the list of counters Yandex.Metrica;
  • rym_get_data - Download data from API reports;
  • rym_get_ga - Download data from API compatible with Core API Google Analytics;
  • rym_get_logs - Download raw data from Logs API.

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 a package dplyr, 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.


Also popular now: