I developed a Shiny application, and I would like to track user behaviour (e.g. on which buttons did the user click, which inputs did they change, ...) using Google Analytics. I found these two interesting "tutorials":
- https://shiny.rstudio.com/articles/google-analytics.html
- https://shiny.rstudio.com/articles/usage-metrics.html
However, my knowledge of javascript is zero, and I have problems implementing it in my Shiny application.
I have the following questions:
- How can I track on which button users have clicked? What if I have multiple buttons throughout my application, like in the code below. Can I track whether users have clicked on button1 or button2?
- How can I track on which tab they have clicked?
- Where in my code do I have to put these event trackers?
A short reproducible code:
ui <- navbarPage("Test app", id = "inTabSet",
header = singleton(tags$head(HTML(
"<script>
(function(i,s,o,g,r,a,m){
i['GoogleAnalyticsObject']=r;i[r]=i[r]||
function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();
a=s.createElement(o), m=s.getElementsByTagName(o)[0];
a.async=1;
a.src=g;m.parentNode.insertBefore(a,m)
})
(window, document, 'script',
'//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXXXXXX-1', 'auto');
ga('send', 'pageview');
</script>"
))),
tabPanel("Informatie",
h2("Welcome!"),
br(),
actionButton('button1', 'First button')
),
tabPanel("First tab",
h2("Welcome to the first tab!"),
uiOutput('first_tab')
)
)
server <- function(input, output, session){
output$first_tab <- renderUI({
sidebarLayout(
div(id = "Sidebar", sidebarPanel(
sliderInput('slider', "Slider input", min = 0, max = 100, value = 50),
actionButton('button2', 'Second button')
)),
mainPanel(
h2("This is the mainpanel")
)
)
})
}
shinyApp(ui, server)