How do you send event values if they are strings to Google Analytics using R Shiny?

Viewed 157

Below I have the Javascript that sends events to Google Analytics:

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

  gtag('config', 'UA-XXXXXXXXX-1');
  

        
  $(document).on('shiny:inputchanged', function(event) {
    if (event.name === 'hub') {
        gtag('event', 'updates', {
        'event_category': 'sidebar',
        'event_label': event.name,
        'value': event.value
        });
       }
     });          
</script>

Below are my Shiny App's ui and server code:

ui <-
   fluidPage(
               
               tags$head(includeHTML(("google-analytics.html"))),
               
               # Sidebar
               sidebarLayout(
                 sidebarPanel(
                   radioButtons("hub", label = h3("Hub"),
                                choices = list("Location 1", "Location 2",
                                               "Both"), 
                                selected = "Location 1"))), 
              
               # Main Panel
               mainPanel(verbatimTextOutput("test"))
)
server <- function(input, output) {
   output$test<-renderPrint({input$hub})
}

Right now, the events will be sent to Google Analytics, so I will know if users change the hub input. But I would also like to know which hub they changed it to. So, I want the information Location 2 to be sent to Google Analytics as well. If this were a numeric value then it would be sent via event.value. So, how can I send the "event value" when it is not numeric and when instead it is a string?

0 Answers
Related