ga_model_make: Modelling function factory for Google Analytics data

View source: R/models.R

ga_model_makeR Documentation

Modelling function factory for Google Analytics data

Description

Create ga_model objects for easy application of models to data

Usage

ga_model_make(
  data_f,
  required_columns,
  model_f,
  output_f = function(df, ...) {
     plot(df)
 },
  required_packages = NULL,
  description = NULL,
  outputShiny = shiny::plotOutput,
  renderShiny = shiny::renderPlot,
  inputShiny = shiny::tagList()
)

Arguments

data_f

A function that gets the data

required_columns

What dimensions and metrics are required

model_f

A function that inputs data, and outputs a list of assets - must take data from result of data_f in first argument

output_f

A function that inputs the output from model_f, outputs a visualisation

required_packages

The packages needed for data_f and model_f to work

description

An optional description of what the model does

outputShiny

A shiny UI output function that will display the results renderShiny

renderShiny

A shiny render function that will create the output for outputShiny from output_f

inputShiny

Optional input shiny functions (like dateInput()) that will be used within the model's Shiny module. The id should be exactly the same as one of the variables in the model functions.

Details

The passed functions should all have ... to make them flexible in what arguments can be added. Do not have the same argument names in both functions. The data_f function result will feed to model_f

Value

A ga_model object to pass to ga_model

See Also

Other GA modelling functions: ga_model_edit(), ga_model_example(), ga_model_load(), ga_model_save(), ga_model_shiny_load(), ga_model_shiny_template(), ga_model_shiny(), ga_model_write(), ga_model()

Examples


## Not run: 

 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,
                            description = "Performs decomposition and creates plot")

 # fetches data and outputs decomposition
 ga_model(81416156, decomp_ga)

 # save the model for later
 model_location <- "decomp_ga.gamr"
 ga_model_save(decomp_ga, filename = model_location)

 # can load model from file
 ga_model(81416156, model_location)

 # or load model to an object and use 
 model2 <- ga_model_load(model_location)

 ga_model(81416156, model2)
 
 # for shiny include functions for the UI and server rendering
 decomp_ga <- ga_model_make(get_model_data,
                            required_columns = c("date", "sessions"),
                            model_f = decompose_sessions,
                            output_f = function(df, ...){graphics::plot(df)},
                            description = "Performs decomposition and creates a plot",
                            outputShiny = shiny::plotOutput,
                            renderShiny = shiny::renderPlot)


## End(Not run)

MarkEdmondson1234/googleAnalyticsR documentation built on Oct. 13, 2023, 4:40 a.m.