if (FALSE) {
 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)
}
Run the code above in your browser using DataLab