This example shows a visualization of traffic to a site broken down by day of week and hour of day.

# Start by loading the libraries we'll want to use.
library(dplyr)
library(tidyr)
library(highcharter)

# Set the view ID that we'll be using. You can get the view ID for a specific view
# https://ga-dev-tools.appspot.com/query-explorer/. It's the "ids" value.
view_id <- 81416156

ga_auth()

# Pull the data. This is set to pull the last 400 days of data.
date_range = c(Sys.Date() - 400, Sys.Date()),
metrics = "sessions",
dimensions = c("date","hour"),
max = -1)

# Added a column to the data with the weekday.
gadata$weekday <- ordered(weekdays(gadata$date, FALSE),
levels = c("Monday","Tuesday","Wednesday",
"Thursday","Friday","Saturday","Sunday")) 

Let’s look at the first few rows (the “head”) of our data to see where we stand so far:

date hour sessions weekday
2015-08-05 00 1 Wednesday
2015-08-05 01 2 Wednesday
2015-08-05 02 3 Wednesday
2015-08-05 03 4 Wednesday
2015-08-05 04 1 Wednesday
2015-08-05 05 5 Wednesday

Now that we have the data, we just have to do a little bit of additional work to get the data arranged properly to generate the heatmap. The code for this could be a lot simpler/shorter, but it’s split out here into separate lines to make it easier to follow. Trust us – this looks like it’s a lot, but it’s really just doing the equivalent of generating a pivot table with a very specific structure:

# Subset the data to just be the weekday, hour of the day, and sessions. (This
# means we're getting rid of the "date" column)
heatmap_data <- select(gadata, weekday, hour, sessions)

# Summarize the data by weekday-hour
heatmap_sums <- group_by(heatmap_data, weekday, hour) %>%
summarise(sessions = sum(sessions))

hchart(heatmap_matrix, type = "heatmap")