Event Tracker with Inputs using Google Analytics and R Shiny

Would somebody know the syntax for tracking events with Google Analytics and R Shiny?

I'd like to track which inputs the user is selecting when interacting with my apps. So in this example, I'd like to know when a user uses and makes changes to the 'PointUseInput' checkbox input.

I tried following the advice here, but I'm not super familiar with JavaScript

# ################################################################################################
# ################################################################################################
# # Sec 1a. Needed Libaries & Input Files

library(shiny)
library(shinydashboard)
library(leaflet)
library(dplyr)

##The Data
Map_DF <- data.frame("Point_ID" = c("A1", "B1", "C3"), 
                     "Latitude" = c(38.05, 39.08, 40.05), 
                     "Longitude" = c(-107.00, -107.05, -108.00),
                     "PointUse" = c("farm", "house", "well"))


################################################################################################
################################################################################################
#UI
ui <- dashboardPage(
    
    dashboardHeader(), 
    
    dashboardSidebar(
        

       ### tags$head(includeHTML(("google-analytics.html"))),  #Google Analytics html tag here
        
        
        checkboxGroupInput(inputId = "PointUseInput", label = "Select Point Use", choices = Map_DF$PointUse, selected = Map_DF$PointUse)
    ), 
    
    dashboardBody(
        fluidRow(leafletOutput(outputId = 'mapA'))
    )
)

################################################################################################
################################################################################################
server <- function(input, output, session) {
    
    ## The Filter
    filterdf <- reactive({
        Map_DF %>% 
            filter(PointUse %in% input$PointUseInput)
    })
    
    ## Base Map Creation
    output$mapA <- renderLeaflet({
        
        leaflet() %>%
            addProviderTiles(
                providers$Esri.DeLorme,
                options = providerTileOptions(
                    updateWhenZooming = FALSE,
                    updateWhenIdle = TRUE)
            ) %>%
            setView(lng = -107.50, lat = 39.00, zoom = 7)
    })
    
    ## Update Map with Filter Selection
    observe({
        leafletProxy("mapA", session) %>% 
            clearMarkers() %>% 
            addCircleMarkers(
                data = filterdf(),
                radius = 10,
                color = "red",
                lat = ~Latitude,
                lng = ~Longitude,
                popupOptions(autoPan = FALSE),
                popup = ~paste("PointUse: ", filterdf()$PointUse))
    })
    
}

################################################################################################
################################################################################################
shinyApp(ui = ui, server = server)

With the accompanying google-analytics JavaScript Code...

  $(document).on('change', 'select', function(e) {
    ga('send', 'event', 'category', 'action', 'label', value);
  });

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.