knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)

ytr

R wrapper to the YouTube Analytics API.

Install

# install.packages("remotes")
remotes::install_github("JohnCoene/ytr")

Functions

Setup

Create a free application in the Google Console to obtain your client and secret.

# OAuth
token <- ytr_oauth("something.apps.googleusercontent.com",
    "xxXx0X0x0X0xXX0")
token <- readRDS("token.rds")

Examples

It's not the most approachable API, even though it consists of just one endpoint, a good place to start is the "sample requests" page.

library(ytr)

# Total views and likes in the last 3 days
token %>% 
  ytr_reports() %>% 
  ytr_parse()

# Top 10 videos by subs gained in the last 7 days
token %>% 
  ytr_reports(
      start = Sys.Date() - 7,
      metrics = c("estimatedMinutesWatched","views","likes","subscribersGained"), 
      dimensions = "video", 
      maxResults = 10, 
      sort = "-subscribersGained"
    ) %>% 
  ytr_parse()

# Filter specific videos (by ID)
token %>% 
  ytr_reports(
      start = Sys.Date() - 7,
      metrics = c("estimatedMinutesWatched","views","likes","subscribersGained"), 
      dimensions = "video", 
      maxResults = 10, 
      sort = "-subscribersGained",
      filters = "video==x2l7KscqRro,cFhONVldyE0"
    ) %>% 
  ytr_parse()


JohnCoene/ytr documentation built on June 30, 2019, 12:33 a.m.