Skip to content

Instantly share code, notes, and snippets.

@madilk
Last active February 27, 2021 17:01
Show Gist options
  • Save madilk/074f6598b49cfe60bb46aaac51c6ff46 to your computer and use it in GitHub Desktop.
Save madilk/074f6598b49cfe60bb46aaac51c6ff46 to your computer and use it in GitHub Desktop.
gganimate plot using google analytics
#http://code.markedmondson.me/googleAnalyticsR/articles/setup.html
install.packages("googleAnalyticsR")
library(googleAnalyticsR)
#change location to your service key folder
ga_auth(json_file = "C:\\location-folder-for your-service-account-json\\service-key.json")
account_list <- ga_account_list()
account_list$viewId
#change view id to your GA view
ga_id <- 1234567
data <- google_analytics(ga_id,
date_range = c("2019-01-01", "2020-11-30"),
metrics = "sessions",
dimensions = c("date","channelGrouping"),
anti_sample = TRUE)
head(data)
summary(data)
install.packages("gganimate")
library(gganimate)
#https://gganimate.com/articles/gganimate.html
#transition_reveal params http://search.r-project.org/library/gganimate/html/transition_reveal.html
p <- ggplot(subset(data,channelGrouping=="Organic Search"),
aes(x=date,y=sessions,
colour=channelGrouping)) +
geom_line() +
transition_reveal(date) +
theme(legend.position = "bottom") +
ggtitle("Daily Organic Search Traffic to AnalyticsLog.com")
p
#https://stackoverflow.com/questions/51440496/using-gganimate-to-export-gif
anim_save("organic search.gif",p)
p <- ggplot(subset(data,channelGrouping=="Organic Search"),
aes(x=date,y=sessions,
colour=channelGrouping)) +
geom_line() +
transition_reveal(date) +
theme(legend.position = "bottom") +
ggtitle("Daily Organic Search Traffic to AnalyticsLog.com")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment