Channels

Yt::Channel represents a YouTube channel. Initialize using either the YouTube ID or URL:

channel = Yt::Channel.new id:  'UCxO1tY8h1AhOz0T4ENwmpow'
# => #<Yt::Channel:0x... @id="UCxO1tY8h1AhOz0T4ENwmpow">

Some methods from other classes also return a Yt::Channel object (e.g.: Yt::Account#channel) or a collection of Yt::Channel objects (e.g.: Yt::Account#subscribed_channels).


Authentication

Most methods of Yt::Channel retrieve public data from YouTube (e.g.: fetch a channel’s title and videos).
To use these methods (marked with   below), you only need to get a Server API key from Google and configure:

Yt.configuration.api_key = "<your api key>"              ## replace with your API key

channel = Yt::Channel.new id: 'UCxO1tY8h1AhOz0T4ENwmpow' ## use any channel ID
channel.title
# => "Fullscreen"

Other methods acts on behalf of YouTube accounts (e.g.: subscribe to a channel, delete playlists).
To use these methods (marked with   below), you need to get an API Client ID/Secret from Google, obtain an access or refresh token from the account you want to act as, and pass the account as the :auth parameter:

Yt.configuration.client_id = "<your ID>"           ## replace with your client ID
Yt.configuration.client_secret = "<your secret>"   ## replace with your client secret
account = Yt::Account.new refresh_token: "<token>" ## use the account’s refresh token

channel = Yt::Channel.new id: 'UCxO1tY8h1AhOz0T4ENwmpow', auth: account
channel.subscribe
# => true (the account subscribed to the channel)

Lastly, some methods acts on behalf of YouTube content owners (e.g.: fetch the earnings of a channel).
To use these methods (marked with   below), you need to get an API Client ID/Secret from Google, obtain an access or refresh token from the content owner, and pass the content owner as the :auth parameter:

Yt.configuration.client_id = "<your ID>"           ## replace with your client ID
Yt.configuration.client_secret = "<your secret>"   ## replace with your client secret
owner = Yt::ContentOwner.new owner_name: "<name>", ## replace with owner’s credentials
                             refresh_token: "<token>"

channel = Yt::Channel.new id: 'UCxO1tY8h1AhOz0T4ENwmpow', auth: owner
channel.estimated_revenue since: 3.days.ago
# => {Wed, 8 May 2014 => 12.4, Thu, 9 May 2014 => 3.2, Fri, 10 May 2014 => …}

List of Yt::Channel methods

any authentication works Channel’s Snippet
→ Yt docschannel.id # => "UCxO1tY8h1AhOz0T4ENwmpow"
→ Yt docschannel.title # => "Fullscreen"
→ Yt docschannel.description # => "The media company for the connected generation"
→ Yt docschannel.published_at # => 2006-03-23 06:13:25 UTC
→ Yt docschannel.thumbnail_url # => "https://yt3.ggpht.com/-KMnbKDBl60w/photo.jpg"
any authentication works Channel’s Statistics
→ Yt docschannel.view_count # => 9
→ Yt docschannel.comment_count # => 1
→ Yt docschannel.video_count # => 103
→ Yt docschannel.subscriber_count # => 453
→ Yt docschannel.subscriber_count_visible? # => true
any authentication works Channel’s Privacy Status
→ Yt docschannel.privacy_status # => "public"
→ Yt docschannel.public? # => true
→ Yt docschannel.unlisted? # => false
→ Yt docschannel.private? # => false
must authenticate as another channel’s account Channel’s Subscription
→ Yt docschannel.subscribe # => nil
→ Yt docschannel.unsubscribe # => true
→ Yt docschannel.subscribed? # => false
must authenticate as the channel’s account or content owner Channel’s Updates
→ Yt docschannel.delete_playlists title: /2013$/ # => [true, true]
must authenticate as the channel’s content owner Channel’s Content Owner Details
→ Yt docschannel.content_owner # => "Cy8DxxmTcwc7ci4zPWrKiw"
→ Yt docschannel.linked_at # => 2011-03-01 23:32:00 UTC
any authentication works Channel’s Associations
→ Yt docschannel.videos # => #<Yt::Collections::Videos …>
→ Yt docschannel.playlists # => #<Yt::Collections::Playlists …>
→ Yt docschannel.related_playlists # => #<Yt::Collections::RelatedPlaylists …>
→ Yt docschannel.subscribed_channels # => #<Yt::Collections::SubscribedChannels …>
must authenticate as the channel’s content owner Channel’s Monetization
→ Yt docschannel.estimated_revenue # => {Fri, 14 Oct 2016 => 455.6, …}
→ Yt docschannel.ad_impressions # => {Fri, 14 Oct 2016 => 847, …}
→ Yt docschannel.playback_based_cpm # => {Wed, 8 May 2014 => 0.34, Thu, 9 May 2014 …}
→ Yt docschannel.monetized_playbacks # => {Wed, 8 May 2014 => 53345, …}

List of Yt::Channel analytics methods

By default, analytics methods return the total value for the lifetime of a channel (e.g., all the views of a channel).
To set specific starting and ending dates, use the :since and :until options.
To obtain one value per each day of the range you specify (rather than the total), use the by: :day option.
To limit the results to a specific location, use the :in option .

channel.views # => {total: 949}

range = {since: 2.months.ago, until: 1.month.ago} # use a Date or String
channel.views range # => {total: 219}
channel.views range.merge(by: :day) # => {Wed, 8 May 2014 => 12, Thu, 9 May 2014 => …}

channel.views in: 'FR' # => {total: 45}
channel.views in: {country: 'US'} # => {total: 113}
channel.views in: {state: 'TX'} # => {total: 11}
# NOTE: Not all methods can be limited by US state; check the docs for details.

For brevity, the options above are omitted in the examples below.

must authenticate as the channel’s account or content owner Channel’s Analytics
→ Yt docschannel.views # => {total: 345}
→ Yt docschannel.comments # => {total: 900}
→ Yt docschannel.likes # => {total: 934}
→ Yt docschannel.dislikes # => {total: 445}
→ Yt docschannel.shares # => {total: 913}
→ Yt docschannel.subscribers_gained # => {total: 334}
→ Yt docschannel.subscribers_lost # => {total: 345}
→ Yt docschannel.videos_added_to_playlists # => {total: 334}
→ Yt docschannel.videos_removed_from_playlists # => {total: 987}
→ Yt docschannel.average_view_duration # => {total: 10}
→ Yt docschannel.average_view_percentage # => {total: 34.3}
→ Yt docschannel.annotation_clicks # => {total: 91}
→ Yt docschannel.annotation_click_through_rate # => {total: 2.98}
→ Yt docschannel.annotation_close_rate # => {total: 3.34}
→ Yt docschannel.estimated_minutes_watched # => {total: 12}
→ Yt docschannel.card_impressions # => {total: 3}
→ Yt docschannel.card_clicks # => {total: 3}
→ Yt docschannel.card_click_rate # => {total: 0.65}
→ Yt docschannel.card_teaser_impressions # => {total: 3}
→ Yt docschannel.card_teaser_clicks # => {total: 3}
→ Yt docschannel.card_teaser_click_rate # => {total: 0.65}

→ Yt docschannel.views by: :country # => {"US" => 33, "MX" => 12, …}
→ Yt docschannel.comments by: :country # => {"US" => 17, "FR" => 12, …}
→ Yt docschannel.likes by: :country # => {"US" => 32, "SG" => 3, …}
→ Yt docschannel.dislikes by: :country # => {"US" => 87, "MT" =>…}
→ Yt docschannel.shares by: :country # => {"US" => 44, "GR" => 7, …}
→ Yt docschannel.subscribers_gained by: :country # => {"CO" => 32, …}
→ Yt docschannel.subscribers_lost by: :country # => {"BM" => 1, …}
→ Yt docschannel.favorites_added by: :country # => {"BG" => 181, …}
→ Yt docschannel.favorites_removed by: :country # => {"PA" => 0, …}
→ Yt docschannel.videos_added_to_playlists by: :country # => {"BG" => 181, …}
→ Yt docschannel.videos_removed_from_playlists by: :country # => {"PA" => 0, …}
→ Yt docschannel.average_view_duration by: :country # => {"AW" => 456, …}
→ Yt docschannel.average_view_percentage by: :country # => {"PK" => 38.85, …}
→ Yt docschannel.annotation_clicks by: :country # => {"GT" => 329, …}
→ Yt docschannel.annotation_click_through_rate by: :country # => {"US" => …}
→ Yt docschannel.annotation_close_rate by: :country # => {"ZW" => 0.1, …}
→ Yt docschannel.estimated_minutes_watched by: :country # => {"US" => 12, …}

→ Yt docschannel.views by: :state # => {"TX" => 42, "SD" => 33, …}
→ Yt docschannel.average_view_duration by: :state # => {"CA" => 456, …}
→ Yt docschannel.average_view_percentage by: :state # => {"IL" => 38.85, …}
→ Yt docschannel.annotation_clicks by: :state # => {"TX" => 329, …}
→ Yt docschannel.annotation_click_through_rate by: :state # => {"TX" => …}
→ Yt docschannel.annotation_close_rate by: :state # => {"TX" => 0.1, …}
→ Yt docschannel.estimated_minutes_watched by: :state # => {"TX" => 12, …}

→ Yt docschannel.views by: :video # => {#<Yt::Video @id=…> => 9, …}
→ Yt docschannel.views by: :playlist, includes: [:status] # => {#<Yt::Playlist> => …}
→ Yt docschannel.views by: :traffic_source # => {advertising: 53, channel: 7, …}
→ Yt docschannel.views by: :playback_location # => {watch: 467, embedded: 53, …}
→ Yt docschannel.views by: :embedded_player_location # => {"fullscreen.net" => 9, …}
→ Yt docschannel.views by: :device_type # => {mobile: 457, tv: 954, …}
→ Yt docschannel.views by: :subscribed_status # => {subscribed: 57, unsubscribed: 54}
→ Yt docschannel.views by: :related_video # => {#<Yt::Video @id=…> => 7, …}
→ Yt docschannel.views by: :search_term # => {"fullscreen" => 7, "music" => 3, …}
→ Yt docschannel.views by: :referrer # => {"Google Search" => 7, "ytimg.com" => 3, …}

→ Yt docschannel.estimated_minutes_watched by: :video # => {#<Yt::Video> => 9, …}
→ Yt docschannel.estimated_minutes_watched by: :playlist # => {#<Yt::Playlist> => …}
→ Yt docschannel.estimated_minutes_watched by: :traffic_source # => {channel: 7, …}
→ Yt docschannel.estimated_minutes_watched by: :playback_location # => {watch: 1, …}
→ Yt docschannel.estimated_minutes_watched by: :embedded_player_location # => {…}
→ Yt docschannel.estimated_minutes_watched by: :device_type # => {tv: 954, …}
→ Yt docschannel.estimated_minutes_watched by: :related_video # => {#<Yt::Video> => …}
→ Yt docschannel.estimated_minutes_watched by: :search_term # => {"music" => 7, …}
→ Yt docschannel.estimated_minutes_watched by: :referrer # => {"Facebook" => 7, …}

→ Yt docschannel.viewer_percentage # => {female: {'18-24' => 4.12, '25-34' => …}, …}
→ Yt docschannel.viewer_percentage by: :gender # => {female: 12.3, male: 87.7}
→ Yt docschannel.viewer_percentage by: :age_group # => {'18-24' => 4.1, '25-34' => …}

→ Yt docschannel.reports only: [:views, :shares] # => {views: {…}, shares: {…}}