title: Shinyjs: How to detect if a user clicked on a tab?
body: <p>I want to track users journey in a Shiny app and for that I need to detect if a user clicked on a tab. For that I’m using <code>shinyjs</code> library and a custom js tracking function. But currently I can’t come up how to use <code>input$tabs</code> instead of <code>id</code> in <code>onclick()</code> function. When I use <code>tabName</code> of a tab as <code>id</code> the function doesn’t react on a click. </p>
  
  <pre><code>library(shiny)
  library(shinydashboard)
  library(shinyjs)
  
  ui = dashboardPage(
  
      dashboardHeader(title = "Shiny"),
  
      dashboardSidebar(
        sidebarMenu(id = "tabs",
  
          menuItem("Section_1", tabName = "section_1", icon = icon("align-justify"), 
                   startExpanded = TRUE, selected = TRUE,
                   menuSubItem("Subsection 1", tabName = "report_1", selected = TRUE),
                   menuSubItem("Subsection 2", tabName = "report_2")),
          menuItem("Section_2", tabName = "section_2", icon = icon("align-justify"))
  
          )
        ),
  
      dashboardBody(
  
        useShinyjs(),
  
        tabItems(
          tabItem("report_1", h1(id = "a", "a")),
          tabItem("report_2", h1(id = "b", "b")),
          tabItem("section_2", h1(id = "c", "c")))
        )
      )
  
  
  server &lt;- function(input, output, session) {
  
    onclick("report_1", alert("tab = report_1"))
    onclick("report_2", alert("tab = report_2"))
    onclick("section_2", alert("tab = section_2"))
  
    onclick("a", alert("tab = report_1"))
    onclick("b", alert("tab = report_2"))
    onclick("c", alert("tab = section_2"))
  
  }
  
  shinyApp(ui=ui, server=server) 
  </code></pre>