GoogleAnalyticsModelR
An R package for creating ready made models to work with googleAnalyticsR data
Install / Use
/learn @IronistM/GoogleAnalyticsModelRREADME
googleAnalyticsModelR
Creating ready made models to work with googleAnalyticsR data
Setup
install.packages(c("remotes","googleAnalyticsR"))
remotes::install_github("IronistM/googleAnalyticsModelR")
Useage
For end users, they can just load the model then apply it to their data:
library(googleAnalyticsR) # assume auto-authentication
library(googleAnalyticsModelR)
# fetches data and outputs decomposition
my_viewid <- 81416156
decomp_ga <- "inst/models/decomp_ga.gamr"
d1 <- ga_model(my_viewid, model = decomp_ga)

#repeat with another viewId
d2 <- ga_model(123875646, model = decomp_ga)

# Example CausalImpact
ci <- ga_model(81416156, model = "inst/models/causalImpact_model.gamr",
event_date = Sys.Date() - 51,
predictors = "Direct",
response = "Organic Search")

Forecasting example with prophet
The model loading can itself be done in a function, until the final end user works with data like:
library(prophet)
library(dygraphs)
library(googleAnalyticsR)
forecast_data <- ga_model_prophet(81416156,
date_range = c(Sys.Date() - 400, Sys.Date() - 1),
forecast_days = 30,
metric = "sessions",
dim_filter=NULL,
interactive_plot = FALSE)
print(forecast_data$plot)

Creating model .gamr objects
To create your own models, you need to predefine all the functions to
look after the fetching, modelling and viewing of the data. You then
pass those functions to the ga_model_make() function.
The functions need to follow these specifications:
data_f- A function to collect the data you will need. The first argument should be theview_idwhich will be pass the viewId of Google Analytics property to fetch data from.model_f- A function to work with the data you have fetched. The first argument should be the data.frame that is produced by the data fetching function,data_f().output_f- A function to plot the data. The first argument should be the data.frame that is produced by the model function,model_f().- All functions you create must include
...as an argument. - All functions must use different arguments (apart from
...), to avoid clashes.
If you want to also create the Shiny modules, then you also need to
specify: * outputShiny - the output function for the UI, such as
plotOutput * renderShiny - the render function for the server, such
as renderPlot
You then supply supporting information to make sure the user can run the model:
required_columns- Specification of which columns the data will fetch. It will fail if they are not present.required_packages- The packages the end user needs to have installed to run your functions.description- A sentence on what the model is so they can be distinguished.
To create the example model above, the above was applied as shown below:
get_model_data <- function(viewId,
date_range = c(Sys.Date()- 300, Sys.Date()),
...){
google_analytics(viewId,
date_range = date_range,
metrics = "sessions",
dimensions = "date",
max = -1)
}
decompose_sessions <- function(df, ...){
decompose(ts(df$sessions, frequency = 7))
}
decomp_ga <- ga_model_make(get_model_data,
required_columns = c("date", "sessions"),
model_f = decompose_sessions,
output_f = graphics::plot,
description = "Performs decomposition and creates a plot",
outputShiny = shiny::plotOutput,
renderShiny = shiny::renderPlot)
Advanced use
The more arguments you provide to the model creation functions, the more complicated it is for the end user, but the more flexible the model. It is suggested making several narrow useage models is better than one complicated one.
For instance, you could modify the above model to allow the end user to specify the metric, timespan and seasonality of the decomposition:
get_model_data <- function(viewId,
date_range = c(Sys.Date()- 300, Sys.Date()),
metric,
...){
o <- google_analytics(viewId,
date_range = date_range,
metrics = metric,
dimensions = "date",
max = -1)
# rename the metric column so its found for modelling
o$the_metric <- o[, metric]
o
}
decompose_sessions <- function(df, frequency, ...){
decompose(ts(df$the_metric, frequency = frequency))
}
decomp_ga_advanced <- ga_model_make(get_model_data,
required_columns = c("date"), # less restriction on column
model_f = decompose_sessions,
output_f = graphics::plot,
description = "Performs decomposition and creates a plot",
outputShiny = shiny::plotOutput,
renderShiny = shiny::renderPlot)
It would then be used via:
result <- ga_model(81416156, decomp_ga_advanced, metric="users", frequency = 30)

str(result, max.level = 1)
## List of 3
## $ input :'data.frame': 301 obs. of 3 variables:
## ..- attr(*, "totals")=List of 1
## ..- attr(*, "minimums")=List of 1
## ..- attr(*, "maximums")=List of 1
## ..- attr(*, "rowCount")= int 301
## $ output:List of 6
## ..- attr(*, "class")= chr "decomposed.ts"
## $ plot : NULL
Working with the model object
The model objects prints to console in a friendly manner:
decomp_ga_advanced
## ==ga_model object==
## Description: Performs decomposition and creates a plot
## Data args: viewId date_range metric
## Input data: date
## Model args: df frequency
## Packages:
You can save and load model objects from a file. It is suggested to save
them with the .gamr suffix.
# save model to a file
ga_model_save(decomp_ga_advanced, filename = "my_model.gamr")
# load model again
ga_model_load("my_model.gamr")
You can use models directly from the file:
ga_model(81416156, "my_model.gamr")
If you need to change parts of a model, ga_model_edit() lets you
change individual aspects:
ga_model_edit(decomp_ga_advanced, description = "New description")
## ==ga_model object==
## Description: New description
## Data args: viewId date_range metric
## Input data: date
## Model args: df frequency
## Packages:
You can also pass it the filename, which will load, make the edit, then save the model to disk again:
ga_model_edit("my_model.gamr", description = "New description")
More complicated example
CausalImpact example
To make your own portable GA Effect, this model uses the CausalImpact and dygraphs libraries to make a plot of your GA data.
This example model is available via ga_model_example("ga-effect.gamr")
library(googleAnalyticsR)
get_ci_data <- function(viewId,
date_range = c(Sys.Date()-600, Sys.Date()),
...){
google_analytics(viewId,
date_range = date_range,
metrics = "sessions",
dimensions = c("date", "channelGrouping"),
max = -1)
}
# response_dim is the channel to predict.
# predictors help with forecast
do_ci <- function(df,
event_date,
response = "Organic Search",
predictors = c("Video","Social","Direct"),
...){
message("CausalImpact input data columns: ", paste(names(df), collapse = " "))
# restrict to one response
stopifnot(is.character(response),
length(response) == 1,
assertthat::is.date(event_date),
is.character(predictors))
pivoted <- df %>%
tidyr::spread(channelGrouping, sessions)
stopifnot(response %in% names(pivoted))
## create a time-series zoo object
web_data_xts <- xts::xts(pivoted[-1], order.by = as.Date(pivoted$date), frequency = 7)
pre.period <- as.Date(c(min(df$date), event_date))
post.period <- as.Date(c(event_date + 1, max(df$date)))
predictors <- intersect(predictors, names(web_data_xts))
## data in order of response, predictor1, predictor2, etc.
model_data <- web_data_xts[,c(response,predictors)]
# deal with names
names(model_data) <- make.names(names(model_data))
# remove any NAs
model_data[is.na(model_data)] <- 0
CausalImpact::CausalImpact(model_data, pre.period, post.period)
}
dygraph_plot <- function(impact, event_date, ...){
require(dygraphs)
## the data for the plot is in here
ci <- impact$series
ci <- xts::xts(ci)
## the dygraph output
dygraph(data=ci[,c('respons
