ryt_get_analytics {rytstat} | R Documentation |
Get statistics from 'YouTube Analytics API'
Description
The YouTube Analytics API enables you to generate custom reports containing YouTube Analytics data. The API supports reports for channels and for content owners.
Usage
ryt_get_analytics(
start_date = Sys.Date() - 14,
end_date = Sys.Date(),
metrics = c("views", "estimatedMinutesWatched", "averageViewDuration",
"averageViewPercentage", "subscribersGained"),
dimensions = "day",
filters = NULL,
sort = NULL,
max_results = NULL,
start_index = NULL
)
Arguments
start_date |
The start date for fetching YouTube Analytics data. The value should be in YYYY-MM-DD format. |
end_date |
The end date for fetching YouTube Analytics data. The value should be in YYYY-MM-DD format. |
metrics |
Character vector of YouTube Analytics metrics, such as views or likes,dislikes. See the documentation for channel reports or a list of the reports that you can retrieve and the metrics available in each report. The Metrics document contains definitions for all of the metrics. |
dimensions |
Character vector of YouTube Analytics dimensions, such as video or ageGroup,gender. The Dimensions document contains definitions for all of the dimensions. |
filters |
Character vector of filters that should be applied when retrieving YouTube Analytics data. The documentation for channel reports reports identifies the dimensions that can be used to filter each report, and the Dimensions document defines those dimensions. |
sort |
Character vector of dimensions or metrics that determine the sort order for YouTube Analytics data. By default the sort order is ascending. The - prefix causes descending sort order. |
max_results |
The maximum number of rows to include in the response. |
start_index |
The 1-based index of the first entity to retrieve. (The default value is 1.) Use this parameter as a pagination mechanism along with the max_results parameter. |
Value
tibble with analytics data
Examples
## Not run:
# auth
ryt_auth()
# get list of your videos
videos <- ryt_get_videos()
# function for loading video stat
get_videos_stat <- function(video_id) {
data <- ryt_get_analytics(
metrics = c('views', 'likes', 'dislikes', 'comments', 'shares'),
filters = stringr::str_glue('video=={video_id}')
)
if ( nrow(data) > 0 ) {
data <- mutate(data, video_id = video_id)
}
}
# load video stat
video_stat <- purrr::map_df(videos$id_video_id, get_videos_stat)
# join stat with video metadata
video_stat <- left_join(video_stat,
videos,
by = c("video_id" = "id_video_id")) %>%
select(video_id,
title,
day,
views,
likes,
dislikes,
comments,
shares)
## End(Not run)