Google Analytics (GA) is a web analytics tool provided by Google to analyze and understand how customers use their websites. Google Analytics could present a piece of vital information for the companies to support their decision making, for instance, measure the marketing performance (e.g., revenue by marketing channel, conversion rate by campaign), assess website performance to boost conversions (e.g., bounce rate by the operating system, page load speed by browser type).
Shiny is an R package that enables us to build an interactive web-based dashboard. Shiny will compiles the R code into the HTML, CSS and JavaScript needed to display the dashboard on the web. What makes a Shiny app particularly powerful is that it can execute R code on the backend so the web-based app can perform any R calculation on our desktop.
We will use googleAnalyticsR to collect the data from Google Analytics and tidyverse to manipulate the dataset.
library(shiny)
library(shinythemes)
library(googleAnalyticsR)
library(tidyverse)
# list of metrics
# As currently written, these can only be # summable metrics, as the data gets pulled
# once as daily data and # then gets aggregate
metric_options <- list("Bounce Rate" = "bounceRate",
"Page Load" = "avgPageLoadTime",
"Transactions" = "transactions",
"Transaction Revenue" = "transactionRevenue",
"Sessions" = "sessions",
"Pageviews" = "pageviews",
"Total Events" = "totalEvents",
"Bounces" = "bounces",
"New Users" = "newUsers")
# list of dimensions
dimension_options <- list("Gender" = "userGender",
"City" = "city",
"Region" = "region",
"New vs. Returning" = "userType",
"Device Category" = "deviceCategory",
"Mobile Device Brand" = "mobileDeviceBranding",
"Country" = "country",
"Browser" = "browser",
"Operating System" = "operatingSystem",
"Default Channel Grouping" = "channelGrouping",
"Source" = "source",
"Medium" = "medium",
"Campaign" = "campaign")
# list of charts
chart_options <- list("Heatmap"="geom_tile",
"Scatterplot"="geom_point",
"Trendline"="geom_line",
"Boxplot"="geom_boxplot")
In general, accessing Google Analytics data from an R-Shiny-based dashboard requires users to log in to their Google account. The googleAnalyticsR package provides a login mechanism for users by utilizing googleAuthUI function, as shown from this link: https://code.markedmondson.me/googleAnalyticsR/articles/shiny.html.
But, how if we donโt want to publish the dashboard or we just need to test the code before publishing it to the server, log in to the Google accounts repeatedly would be a waste of time. So, probably it would be better to login once and use that pre-authorized Google credentials to access Google Analytics data.
ui <- fluidPage(
#As we are not going to use Login button, we don't have to include this
#line of code
#googleAuthUI("login"),
#authDropdownUI("auth_menu")
theme = shinytheme("united"),
# Application title
titlePanel("Shiny Dashboard"),
# Sidebar with a slider input for number of bins
# Sidebar with the user-controllable inputs
sidebarLayout(
sidebarPanel(
# The start date picker
dateInput("startdate","Start Date: ", value = Sys.Date() - 30),
# The end date picker
dateInput("enddate","End Date: ", value = Sys.Date()),
# Horizontal line just to break up the settings a bit.
tags$hr(style="border-color: #777777;"),
# The plot type dropdown
selectInput("plottype", label = "Chart Type:",
choices = chart_options,
selected = "geom_tile"),
# Horizontal line just to break up the settings a bit.
tags$hr(style="border-color: #777777;"),
# The metric selector (dropdown)
selectInput("metric", label = "Metric",
choices = metric_options,
selected = "sessions"),
# Horizontal line just to break up the settings a bit.
tags$hr(style="border-color: #777777;"),
# The dimension selector (dropdown) for the X-dimension
selectInput("x_dim", label = "Select the X dimension and how many values to show:",
choices = dimension_options,
selected = "deviceCategory"),
# Select the max number of values to show in the X dimension
sliderInput("dim_x_count",
label = NULL,
min = 1,
max = 10,
value = 3),
# Horizontal line just to break up the settings a bit.
tags$hr(style="border-color: #777777;"),
# The dimension selector (dropdown) for the X-dimension
selectInput("y_dim", label = "Select the Y dimension and how many values to show:",
choices = dimension_options,
selected = "channelGrouping"),
# Select the max number of values to show in the Y dimension
sliderInput("dim_y_count",
label = NULL,
min = 1,
max = 10,
value = 6)
),
# Show the heatmap and sparklines
mainPanel(
plotOutput("plot")
)
)
)
Instead of calling callModule(googleAuth, "login") function, we are going to access the Google pre-authorized token (users will be redirected to Google Authentication page if no credential found or accessing the dashboard for the first time)
# Define server logic required to draw a histogram
server <- function(input, output) {
#instead of calling access_token <- callModule(googleAuth, "login")
#we are going to use ga_auth function to get the token
#get pre-authorized google analytics token
#NOTE: we also need a minor modification when calling the with_shiny function below
access_token <- ga_auth()
base_data <- reactive({
# Calculate the start and end dates.
start_date <- as.character(input$startdate)
end_date <- as.character(input$enddate)
# Set up the dimensions being used
dimensions <- c("date",input$x_dim, input$y_dim)
metrics <- c(input$metric)
# Pull the data.
# NOTE:as we have modified the access token from calling a Google Authentication module
# to simply retrieving the credentials, the access_token is a string, so we don't
# have to use access_token()
ga_data <- with_shiny(google_analytics,
viewId = input$viewid,
date_range = c(start_date,end_date),
metrics = metrics,
dimensions = dimensions,
shiny_access_token = access_token)
# Rename the columns to be generic names -- that just makes it easier
# for all future transformations
colnames(ga_data)[1:3] <- c("date","dim_x","dim_y")
# We want to return the entire data frame -- not just the column names,
# so throw the data frame here as the last object in the function.
ga_data
})
# Build the plot
output$plot <- renderPlot({
default_metric <- if_else(is.null(input$metric), "sessions", input$metric)
plot_data <- base_data()
plot_xdims <- reactive({calc_dim_x_includes(plot_data, input$dim_x_count, default_metric)})
plot_ydims <- reactive({calc_dim_y_includes(plot_data, input$dim_y_count, default_metric)})
plot_x <- plot_xdims()
plot_y <- plot_ydims()
if (input$plottype1=="geom_tile"){
#build heatmap plot
} else if (input$plottype1 == "geom_line"){
#build line plot
} else if (input$plottype1 == "geom_boxplot"){
#build box plot
} else if (input$plottype1 == "geom_point"){
#build scatter plot
}
})
}
# Run the application
shinyApp(ui = ui, server = server)