/
simple_app.R
68 lines (48 loc) · 1.46 KB
/
simple_app.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
library(shiny) # R webapps
library(googleAuthR) # auth login
# takes JSON client secrets from GAR_CLIENT_WEB_JSON
# set before calls to googleAnalyticsR to make sure it doesn't use default project.
gar_set_client(scopes = c("https://www.googleapis.com/auth/analytics.readonly"))
library(googleAnalyticsR) # get google analytics
ui <- fluidPage(
gar_auth_jsUI("auth"),
# shiny UI elements
column(width = 12, authDropdownUI("auth_dropdown",
inColumns = TRUE)),
dateRangeInput("datepicker", NULL, start = Sys.Date() - 300),
plotOutput("trend_plot")
)
server <- function(input, output, session) {
# get auth token
auth <- callModule(gar_auth_js, "auth")
# get ga_accounts
ga_accounts <- reactive({
req(auth())
with_shiny(
ga_account_list,
shiny_access_token = auth()
)
})
view_id <- callModule(authDropdown, "auth_dropdown",
ga.table = ga_accounts)
ga_data <- reactive({
req(view_id())
req(input$datepicker)
with_shiny(
google_analytics,
view_id(),
date_range = input$datepicker,
dimensions = "date",
metrics = "sessions",
max = -1,
shiny_access_token = auth()
)
})
output$trend_plot <- renderPlot({
req(ga_data())
ga_data <- ga_data()
plot(ga_data$sessions, type = "l")
})
}
# Run the application
shinyApp(ui = ui, server = server)