Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanpraski/dee3649accd84f3e676002085f3a271c to your computer and use it in GitHub Desktop.
Save ryanpraski/dee3649accd84f3e676002085f3a271c to your computer and use it in GitHub Desktop.
R Script to create a heatmap the visualizes Google Analytics sessions by day of week and hour of the day. Tutorial: http://www.ryanpraski.com/r-heatmap-tutorial-for-google-analytics/
library(googleAnalyticsR)
library(ggplot2)
#Authorized Google Analytics R- this will open a webpage
#You must be logged into your Google Analytics account on your web browser
ga_auth()
#Make sure to replace this with your viewId. You can use google_analytics_account_list() to find your viewId.
my_id <- 94579701
#Session by Hour of Day and Day of Week Query
df1 <- google_analytics_4(my_id,
date_range = c("365daysAgo", "yesterday"),
metrics = c("sessions"),
dimensions = c("dayOfWeek","hour"),
anti_sample = TRUE)
#heatmap ggplot
df1 %>%
ggplot(aes(x=dayOfWeek, y=hour, fill=sessions)) +
geom_tile() +
scale_fill_continuous(low = 'white', high = 'red')
@ryanpraski
Copy link
Author

ryanpraski commented Apr 6, 2017

Heatmap Hour of Day Day of Week Traffic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment