Code base for twitchETL and this example can be found on GitHub

Retrieving top games using the twitchETL package:

require(twitchETL)
require(dplyr)
require(readr)
library(httr)

# get access token
clientID <- 'xxx'
clientSecret <- 'xxx'
r <- POST(paste0("https://id.twitch.tv/oauth2/token?client_id=", clientID, "&client_secret=", clientSecret, "&grant_type=client_credentials"))
stop_for_status(r)
access_token <- content(r, "parsed", "application/json")$access_token

query_timestamp <- Sys.time() %>% as.character

topGames <- getTopGameIDs(clientID, access_token, 10)
gameIDs <- topGames$game_id %>% as.character

topGamesStreams <- lapply(gameIDs, function(g) {
      Sys.sleep(2)
      return(getCurrentStreams(clientID, access_token, game_id = g))
  }) %>% bind_rows

df <- topGamesStreams %>%
  left_join(topGames, by = 'game_id')

# write to CSV
df %>%
  write_csv(paste0('~/twitch_snapshot_', Sys.time(), '.csv'))

# OR write to database
db <- RMySQL::dbConnect(RMySQL::MySQL(), user = "xxx", password = 'xxx', dbname = "xxx", host = "localhost")
RMySQL::dbWriteTable(db, value = df %>% mutate(query_timestamp = query_timestamp), name = "snapshots", append = TRUE, row.names = F)
dbDisconnect(db)

A brief analysis of Twitch data

options(warn = -1) 

require(RMySQL)
require(dplyr)
require(ggplot2)
require(ggdark)
require(ggiraph)
require(stringr)
require(lubridate)
require(highcharter)

# db_permissions.Rdata should contain the following environment variables:
## host: 'xxx.xxx.xx.xx'
## dbname: 'abc'
## user: def'
## password: 'ghi'

#save(host, dbname, user, password, file = 'db_permissions.Rdata')
load('db_permissions.Rdata') # permissions loaded from a local SQL database

conn <- dbConnect(MySQL(), host = host, dbname = 'twitch',  user = user, password = password)
#dbListTables(conn)

# Aggregate ranking of games as of latest pull of all time
df <- tbl(conn, 'snapshots') %>%
   group_by(game_name) %>%
   summarise(viewer_count = sum(viewer_count)) %>% 
   collect

# Daily snapshots of average views
df2 <- tbl(conn, 'snapshots') %>%
  collect %>%
  mutate(Period = ceiling_date(as_datetime(pull_timestamp), unit = 'days')) %>%
  group_by(game_name, Period) %>%
  summarise(viewer_count = mean(viewer_count))
DT::datatable(df)

Overall popularity of all time since May

Note: multiple views per hour will be counted multiple times.

g <- ggplot(df, aes(x = reorder(game_name, viewer_count), y = viewer_count)) + 
  geom_bar_interactive(stat = 'identity', aes(tooltip = viewer_count, data_id = str_replace_all(game_name, "'", ""))) + 
  xlab('') + ylab('Total Viewer Count') + 
  labs(caption = 'Source: databasement.org; Twitch API') +
  ggtitle('Most viewed games on Twitch since May') +
  coord_flip() + 
  dark_theme_minimal() + 
  theme(text = element_text(size = 9), 
        plot.title.position = 'plot',
        plot.caption.position = 'plot',
        plot.caption = element_text(size = 6, hjust = 0)) 
ggiraph::ggiraph(ggobj = g, height_svg = 8)
Dark Souls II Pacify Five Nights at Freddy's 3 Speed Swing Spore Marbles On Stream Stigmatized Property Sports & Fitness Halo: The Master Chief Collectio Disc Jam Embr Catherine: Full Body Outlast NieR Automata Crane Game Toreba Legends of Runeterra Madden NFL 20 Games + Demos The Coin Game Red Dead Redemption 2 Gunfire Reborn Plants vs. Zombies The Isle Skate 3 Tom Clancy's Rainbow Six: Siege Worms W.M.D Mobile Legends: Bang bang The Elder Scrolls V: Skyrim The Elder Scrolls Online Black Desert Online Pogostuck: Rage With Your Friend Doom Eternal RISK: Global Domination F1 2020 Welcome to the Game II Super Mario 64 Raft Warhammer A Way Out Mighty Fling Monster Hunter World Crucible The Forest Summer Game Fest rFactor 2 Clubhouse Games: 51 Worldwide Cl Pinturillo 2 Monopoly Plus Scrap Mechanic Words with Friends 2 Garry's Mod Poly Bridge 2 Rocket Arena Death Stranding King of Crabs Tabletop RPGs Super Smash Bros. Ultimate Among Us Korean Baseball League Mortal Kombat 11 Maneater Rocket League Cyberpunk 2077 SpongeBob SquarePants: Battle fo NCAA Football 14 Dungeons & Dragons Detroit: Become Human Pokémon Sword/Shield F1 2019 Destiny 2 Dead by Daylight Trackmania Hunt: Showdown Ghost of Tsushima Talk Shows & Podcasts The Last of Us Pummel Party Slots NBA 2K20 Teamfight Tactics PLAYERUNKNOWN'S BATTLEGROUNDS Overwatch Chess Minecraft Dungeons Path of Exile Garena Free Fire Sea of Thieves Terraria Hyper Scape Special Events Hearthstone Music & Performing Arts Apex Legends The Last of Us Part II FIFA 20 World of Warcraft Minecraft Dota 2 Escape From Tarkov VALORANT Counter-Strike: Global Offensive Call of Duty: Modern Warfare Grand Theft Auto V Fortnite League of Legends Just Chatting 0e+00 2e+08 4e+08 6e+08 8e+08 Total Viewer Count Most viewed games on Twitch since May Source: databasement.org; Twitch API

Popularity over time

Note: multiple views per hour will be counted multiple times.

g <- ggplot(df2 %>% filter(Period >= '2020-05-15'), aes(x = Period, y = reorder(game_name, viewer_count))) + 
  geom_tile_interactive(aes(fill = log(viewer_count), tooltip = viewer_count, data_id = Period)) + 
  xlab('') + ylab('Average views per day') + 
  labs(caption = 'Source: databasement.org; Twitch API') +
  ggtitle('Twitch views over time') +
  dark_theme_minimal() + 
  theme(text = element_text(size = 9), 
        plot.title.position = 'plot',
        plot.caption.position = 'plot',
        plot.caption = element_text(size = 6, hjust = 0)) +
  scale_fill_viridis_c_interactive('total views per day', option = 'inferno') +
  theme(legend.position = 'hidden')
gg <- ggiraph::ggiraph(ggobj = g, height_svg = 12)
gg <- girafe_options(gg, opts_toolbar(position = 'top'))
gg
Dark Souls II Monster Hunter World The Elder Scrolls Online The Elder Scrolls V: Skyrim Black Desert Online Hunt: Showdown Teamfight Tactics NBA 2K20 Hearthstone Dead by Daylight Apex Legends Music & Performing Arts F1 2020 Sea of Thieves Red Dead Redemption 2 Raft Madden NFL 20 PLAYERUNKNOWN'S BATTLEGROUNDS Path of Exile Halo: The Master Chief Collectio Marbles On Stream The Forest Overwatch Terraria Garena Free Fire World of Warcraft SpongeBob SquarePants: Battle fo Super Smash Bros. Ultimate Games + Demos Rocket League Super Mario 64 Death Stranding Mortal Kombat 11 FIFA 20 Trackmania Dota 2 Pokémon Sword/Shield VALORANT Tom Clancy's Rainbow Six: Siege Destiny 2 Minecraft Chess Gunfire Reborn Slots Legends of Runeterra The Last of Us Hyper Scape Maneater Pacify NieR Automata Counter-Strike: Global Offensive Sports & Fitness Minecraft Dungeons Crucible Dungeons & Dragons Call of Duty: Modern Warfare Outlast Tabletop RPGs Escape From Tarkov Talk Shows & Podcasts F1 2019 Grand Theft Auto V Korean Baseball League Rocket Arena Fortnite Pummel Party Plants vs. Zombies The Last of Us Part II rFactor 2 Mobile Legends: Bang bang Poly Bridge 2 Doom Eternal A Way Out Among Us Garry's Mod Clubhouse Games: 51 Worldwide Cl Ghost of Tsushima Detroit: Become Human The Isle League of Legends Scrap Mechanic Just Chatting Monopoly Plus Pogostuck: Rage With Your Friend RISK: Global Domination Warhammer Skate 3 Summer Game Fest Catherine: Full Body NCAA Football 14 King of Crabs Cyberpunk 2077 Special Events The Coin Game Spore Mighty Fling Worms W.M.D Embr Pinturillo 2 Five Nights at Freddy's 3 Crane Game Toreba Speed Swing Disc Jam Words with Friends 2 May 15 Jun 01 Jun 15 Jul 01 Jul 15 Average views per day Twitch views over time Source: databasement.org; Twitch API