The hardware and bandwidth for this mirror is donated by METANET, the Webhosting and Full Service-Cloud Provider.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]metanet.ch.

rytstat - R interface for work with YouTube APIs

CRAN status Lifecycle: experimental R-CMD-check

rytstat package is an R interface for working with the following YouTube APIs:

That is, the rytstat allows you to request any data available in the YouTube Creator Studio for further analysis and visualization using the R language.

Privacy Policy

The rytstat package for authorization uses the gargle package, the credentials obtained during authorization are stored exclusively on your local PC, you can find out the folder into which the credentials are cached using the ryt_auth_cache_path() function.

For loading data from your YouTube channel rytstat needs next scopes:

For more details see Official YouTube API documentation.

The package does not transfer your credentials or data obtained from your advertising accounts to third parties, however, the responsibility for information leakage remains on the side of the package user. The author does not bear any responsibility for their safety, be careful when transferring cached credentials to third parties.

For more details, I recommend that you read the following articles from the official documentation of the gargle package:

Authorization process

You run gads_auth('me@gmail.com') and start OAuth Dance in the browser:

Typical OAuth dance in the browser, when initiated from within R
Typical OAuth dance in the browser, when initiated from within R

Upon success, you see this message in the browser:

Authentication complete. Please close this page and return to R.

And you credentials cached locally on your PC in the form of RDS files.

Key points

Use own OAuth client

You can use own OAuth app:

app <- httr::oauth_app(appname = "app name", key = "app id", secret = "app secret")
ryt_auth_configure(app = app)

# or from json file 
ryt_auth_configure(path = 'D:/ga_auth/app.json')

# run authorization
ryt_auth('me@gmail.com')

Install

You can install rytstat from CRAN or GitHub:

install.packages("rytstat")

или GitHub:

devtools::install_github('selesnow/rytstat')

Examples

Auth

library(rytstat)
library(httr)

# auth app
app <- oauth_app(
    appname = 'my app',
    key = 'app id', 
    secret = 'app secret')

ryt_auth_configure(app = app)

ryt_auth(email = 'me@gmail.com')

Using YouTube Data API

# load channel data
channel <- ryt_get_channels()

# load videos
videos <- ryt_get_video_list()
video_details <- ryt_get_video_details(video_id = videos$id_video_id)

# load playlists
pl <- ryt_get_playlists()
pl_items <- ryt_get_playlist_items(
     playlist_id = pl$id[1],
     part = c('contentDetails', 'snippet'),
     fields = 'items(id,snippet/channelId,snippet/title,contentDetails/videoId)'
)

# search channels, playlists or videos
search_res_videos <- ryt_search(
    type            = 'video',
    q               = 'r language tutorial',
    published_after = '2022-03-01T00:00:00Z',
    published_before = '2022-06-01T00:00:00Z',
    max_results     = 10 
)

search_res_playlists <- ryt_search(
    type             = 'playlist',
    q                = 'r language tutorial',
    published_after  = '2022-03-01T00:00:00Z',
    published_before = '2022-06-01T00:00:00Z',
    max_results      = 50
)

search_res_channels <- ryt_search(
    type             = 'channel',
    q                = 'r language tutorial',
    published_after  = '2022-03-01T00:00:00Z',
    published_before = '2022-06-01T00:00:00Z',
    max_results      = 50
)

Using YouTube Analytics API

library(rytstat)

# get list of videos
videos <- ryt_get_video_list()

# get statistics by day and videos
# you can specify no more than 500 videos at a time
video_stat <- ryt_get_analytics(
  start_date = '2021-01-01', 
  end_date = '2021-09-01',
  dimensions = c('day', 'video'),
  metrics = c('views', 
              'likes', 
              'dislikes', 
              'comments', 
              'shares'),
  filters = str_glue('video=={str_c(head(videos$id_video_id, 500), collapse=",")}')
)

Using YouTube Reporting API

# auth
ryt_auth('me@gmail.com')

# get reporting data
## create job
ryt_create_job('channel_basic_a2')

## get job list
jobs2 <- ryt_get_job_list()

## get job report list
reports <- ryt_get_report_list(
  job_id = jobs$id[1],
  created_after = '2021-10-20T15:01:23.045678Z'
)

# get report data
data <- ryt_get_report(
  download_url = reports$downloadUrl[1]
)

# delete job
ryt_delete_job(jobs$id[1])

Author

Alexey Seleznev, Head of analytics dept. at Netpeak
Telegram Channel: R4marketing
YouTube Channel: R4marketing
email: selesnow@gmail.com
facebook: facebook.com/selesnow
blog: alexeyseleznev.wordpress.com

These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.