Learn R Programming

rytstat - R interface for work with YouTube APIs

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:

  • View monetary and non-monetary YouTube Analytics reports for your YouTube content
  • View your YouTube account
  • View and manage your assets and associated content on YouTube
  • View YouTube Analytics reports for your YouTube content
  • Manage your YouTube account

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:

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

  • By default, gargle caches user tokens centrally, at the user level, and their keys or labels also convey which Google identity is associated with each token.
  • Token storage relies on serialized R objects. That is, tokens are stored locally on your PC in the form of RDS files.

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

Copy Link

Version

Install

install.packages('rytstat')

Monthly Downloads

145

Version

0.3.2

License

MIT + file LICENSE

Maintainer

Alexey Seleznev

Last Published

May 31st, 2024

Functions in rytstat (0.3.2)

rytstat-package

rytstat: Work with 'YouTube API'
ryt_token

Produce configured token
ryt_user

Get info on current user
ryt_get_videos

Get list of your videos from 'YouTube'
ryt_has_token

Is there a token on hand?
ryt_search

Search channels, playlists or videos on YouTube
ryt_get_video_details

Get detail data of your videos on 'YouTube'
ryt_auth

Authorization in YouTube API
ryt_get_playlists

Get playlist from 'YouTube API'
ryt_get_playlist_items

Get playlist items data on 'YouTube'
ryt_auth_configure

Edit and view auth configuration
ryt_get_channel_activities

Returns a list of channel activity events
ryt_deauth

Suspend authorization
ryt_get_comments

Returns a list of comment threads of video or channel
ryt_get_channels

Get channel info from 'YouTube API'
ryt_get_analytics

Get statistics from 'YouTube Analytics API'
ryt_get_report_types

Returns a list of report types that the channel or content owner can retrieve. Each item in the list contains an id property, which identifies the report's ID, and you need this value to schedule a reporting job.