How set up Google Analytics event tracker for certain web element in R Shiny?

I would like to assign a Google event tracker for certain web elements in the R Shiny application. Let's get the simplest R Shiny project with unique id "si_bins" for "sliderInput" element.

Here is Google Analytics script

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=XXXX"></script>
  <script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'XXXX');

  $(document).on('change', 'si_bins', function(e) {
    ga('send', 'event', 'sliderInput', 'select number of bins', 
    $(e.currentTarget).val());
  });

</script>

Here is R Shiny app

library(shiny)

server <- function(input, output) {
  output$uo_bins <- renderUI({
    sliderInput("si_bins", "Number of bins:", min = 1, max = 50,value = 30)
  })
  output$distPlot <- renderPlot({
    x    <- faithful[, 2] 
    bins <- seq(min(x), max(x), length.out = input$si_bins + 1)
    hist(x, breaks = bins, col = 'darkgray', border = 'white')
  })
}

ui <- fluidPage(
   titlePanel("Old Faithful Geyser Data"),
   sidebarLayout(
      sidebarPanel(uiOutput("uo_bins")),
      mainPanel(plotOutput("distPlot"))
   ))

shinyApp(ui = ui, server = server)

How set up this line of code correctly?

$(document).on('change', 'si_bins', function(e) {
      ga('send', 'event', 'sliderInput', 'select number of bins', 
          (e.currentTarget).val());
    });

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.