Last active
April 6, 2017 21:20
-
-
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/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
Author
ryanpraski
commented
Apr 6, 2017
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment