inst/doc/tubern_examples.R

## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  eval = FALSE
)

## ----setup--------------------------------------------------------------------
# library(tubern)
# 
# # First-time setup - shows setup guide
# yt_oauth()
# 
# # Or with your credentials
# yt_oauth("your-client-id.apps.googleusercontent.com", "your-client-secret")

## ----dates--------------------------------------------------------------------
# # Get common date range options
# get_common_date_ranges()
# 
# # Use relative dates in reports
# report <- get_report(
#   ids = "channel==MINE",
#   metrics = "views,likes",
#   start_date = "last_30_days"  # End date calculated automatically
# )
# 
# # More examples
# get_report(ids = "channel==MINE", metrics = "views", start_date = "this_month")
# get_report(ids = "channel==MINE", metrics = "views", start_date = "last_quarter")
# get_report(ids = "channel==MINE", metrics = "views", start_date = "yesterday")

## ----channel_overview---------------------------------------------------------
# # Get comprehensive channel performance
# overview <- get_channel_overview("last_30_days")
# 
# # Customize what metrics to include
# overview <- get_channel_overview(
#   "this_month",
#   include_engagement = TRUE,
#   include_subscribers = TRUE
# )

## ----top_videos---------------------------------------------------------------
# # Get top 10 videos by views
# top_videos <- get_top_videos("last_7_days")
# 
# # Get more videos with custom metrics
# top_videos <- get_top_videos(
#   "last_30_days",
#   max_results = 25,
#   metrics = c("views", "likes", "comments", "shares", "estimatedMinutesWatched")
# )

## ----demographics-------------------------------------------------------------
# # Full demographic breakdown
# demographics <- get_audience_demographics("last_90_days")
# 
# # Age groups only
# age_data <- get_audience_demographics("this_month", dimension = "ageGroup")
# 
# # Gender breakdown only
# gender_data <- get_audience_demographics("this_quarter", dimension = "gender")

## ----geographic---------------------------------------------------------------
# # Top countries by views
# geo_performance <- get_geographic_performance("last_30_days")
# 
# # US states/provinces (requires filtering to US)
# us_states <- get_geographic_performance(
#   "this_month",
#   dimension = "province",
#   filters = "country==US"
# )

## ----devices------------------------------------------------------------------
# # Performance by device type - use get_report directly
# device_performance <- get_report(
#   ids = "channel==MINE",
#   metrics = "views,estimatedMinutesWatched",
#   dimensions = "deviceType",
#   start_date = "last_30_days"
# )
# 
# # Operating system breakdown
# os_performance <- get_report(
#   ids = "channel==MINE",
#   metrics = "views,estimatedMinutesWatched",
#   dimensions = "operatingSystem",
#   start_date = "this_month"
# )
# 
# # Both device and OS
# device_os <- get_report(
#   ids = "channel==MINE",
#   metrics = "views,estimatedMinutesWatched",
#   dimensions = "deviceType,operatingSystem",
#   start_date = "last_7_days"
# )

## ----daily--------------------------------------------------------------------
# # Daily performance trends
# daily_data <- get_daily_performance("last_30_days")
# 
# # With engagement metrics
# daily_engagement <- get_daily_performance(
#   "this_month",
#   metrics = c("views", "likes", "comments", "shares")
# )

## ----revenue------------------------------------------------------------------
# # Set up OAuth with monetary scope first
# yt_oauth("your-client-id", "your-client-secret", scope = "monetary")
# 
# # Get revenue data
# revenue <- get_revenue_report("last_month")
# 
# # Revenue in different currency
# revenue_eur <- get_revenue_report("this_quarter", currency = "EUR")

## ----validation---------------------------------------------------------------
# # Invalid metrics get helpful suggestions
# # get_report(ids = "channel==MINE", metrics = "vews", start_date = "last_7_days")
# # Error: Invalid metric(s): vews
# # Did you mean: 'vews' -> 'views'
# 
# # Check available metrics and dimensions
# get_available_metrics()
# get_available_metrics("view")  # Filter by pattern
# 
# get_available_dimensions()
# get_available_dimensions("country|city")  # Geographic dimensions

## ----dataframes---------------------------------------------------------------
# # Get data and convert to clean data.frame
# report <- get_daily_performance("last_30_days")
# df <- yt_to_dataframe(report)
# head(df)
# 
# # Convert to tibble (if tibble package installed)
# tbl <- yt_to_tibble(report)
# 
# # Keep original column names
# df_orig <- yt_to_dataframe(report, clean_names = FALSE)

## ----export-------------------------------------------------------------------
# # Export to CSV
# report <- get_top_videos("last_7_days")
# file_path <- yt_export_csv(report, "top_videos.csv")
# 
# # Auto-generated filename with timestamp
# file_path <- yt_export_csv(report)

## ----plots--------------------------------------------------------------------
# # Automatic plot creation (requires ggplot2)
# daily_report <- get_daily_performance("last_30_days")
# yt_quick_plot(daily_report)  # Line plot over time
# 
# # Bar chart for top videos
# top_videos <- get_top_videos("last_7_days")
# yt_quick_plot(top_videos, chart_type = "bar")
# 
# # Custom x and y columns
# geo_data <- get_geographic_performance("last_30_days")
# yt_quick_plot(geo_data, x_col = "country", y_col = "views")

## ----summary------------------------------------------------------------------
# # Get summary statistics
# report <- get_channel_overview("last_30_days")
# summary <- yt_extract_summary(report)
# print(summary)

## ----diagnostics--------------------------------------------------------------
# # Check if everything is set up correctly
# diagnose_tubern()
# 
# # Check API quota status
# check_api_quota()

## ----errors-------------------------------------------------------------------
# # tubern now provides helpful error messages and suggestions
# # for authentication, API quota, parameter errors, etc.
# 
# # Example: If OAuth token expires, tubern will suggest running yt_oauth() again
# # Example: If API quota is exceeded, tubern will suggest ways to reduce usage

## ----workflow1----------------------------------------------------------------
# # Set up authentication
# yt_oauth("your-client-id", "your-client-secret")
# 
# # Get overview data
# overview <- get_channel_overview("last_30_days")
# overview_df <- yt_to_dataframe(overview)
# 
# # Get daily trends
# daily <- get_daily_performance("last_30_days")
# daily_df <- yt_to_dataframe(daily)
# 
# # Get top videos
# top_videos <- get_top_videos("last_30_days", max_results = 10)
# videos_df <- yt_to_dataframe(top_videos)
# 
# # Get geographic performance
# geo <- get_geographic_performance("last_30_days")
# geo_df <- yt_to_dataframe(geo)
# 
# # Create visualizations
# yt_quick_plot(daily)      # Daily trend
# yt_quick_plot(top_videos) # Top videos
# yt_quick_plot(geo)        # Geographic performance
# 
# # Export data
# yt_export_csv(overview, "channel_overview.csv")
# yt_export_csv(daily, "daily_performance.csv")
# yt_export_csv(top_videos, "top_videos.csv")

## ----workflow2----------------------------------------------------------------
# # Demographic analysis
# demographics <- get_audience_demographics("last_90_days")
# demo_df <- yt_to_dataframe(demographics)
# 
# # Device analysis
# devices <- get_report(
#   ids = "channel==MINE",
#   metrics = "views,estimatedMinutesWatched",
#   dimensions = "deviceType",
#   start_date = "last_90_days"
# )
# device_df <- yt_to_dataframe(devices)
# 
# # Geographic analysis
# geography <- get_geographic_performance("last_90_days", max_results = 50)
# geo_df <- yt_to_dataframe(geography)
# 
# # Combined analysis with dplyr (if available)
# if (require(dplyr)) {
#   # Top countries by watch time
#   top_countries <- geo_df %>%
#     arrange(desc(estimated_minutes_watched)) %>%
#     head(10)
# 
#   print(top_countries)
# }

## ----workflow3----------------------------------------------------------------
# # Analyze video performance over time
# videos_monthly <- get_top_videos("this_month", max_results = 50)
# videos_df <- yt_to_dataframe(videos_monthly)
# 
# # Compare with previous month
# videos_prev <- get_top_videos("last_month", max_results = 50)
# videos_prev_df <- yt_to_dataframe(videos_prev)
# 
# # Daily performance for trend analysis
# daily_trends <- get_daily_performance(
#   "last_30_days",
#   metrics = c("views", "estimatedMinutesWatched", "likes", "comments")
# )
# trends_df <- yt_to_dataframe(daily_trends)
# 
# # Visualize trends
# yt_quick_plot(daily_trends, x_col = "day", y_col = "views")

## -----------------------------------------------------------------------------
# # Run diagnostics
# diagnose_tubern()
# 
# # Re-authenticate
# yt_oauth("your-client-id", "your-client-secret")

## -----------------------------------------------------------------------------
# # Check quota status
# check_api_quota()
# 
# # Reduce data scope
# get_report(ids = "channel==MINE", metrics = "views",
#           start_date = "yesterday", max_results = 10)

## -----------------------------------------------------------------------------
# # Check available options
# get_available_metrics()
# get_available_dimensions()
# 
# # Use helper functions for validation
# get_channel_overview("last_7_days")  # Pre-validated parameters

Try the tubern package in your browser

Any scripts or data that you put into this service are public.

tubern documentation built on April 12, 2026, 5:07 p.m.