How to use Google Analytics and event-tracking for tracking user behaviour in a Shiny application?
Asked Answered
S

1

12

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":

However, my knowledge of javascript is zero, and I have problems implementing it in my Shiny application.

I have the following questions:

  1. 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?
  2. How can I track on which tab they have clicked?
  3. 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)
Shult answered 1/5, 2018 at 7:52 Comment(3)
In the same boat here. Have you figured out anything?Stockman
Also looking for solutions to this exact problem - any progress?Tadtada
Four years later still looking for the solution to thisDestinee
R
0

to answer your first question: yes you can. analytics lets you define any custom event you want: custom events. by adding the event to your onclick it can be done. for example you can name the event of the first button clickedOnBtn1 and the second one clickedOnBtn2 and put them in onclick. in your analytics console you will find these events under the event by name section. see the Step 4 - Create event trackers in your shiny document.

<a href="http://www.example.com" 
   onclick="ga('send', 'event', 'click', 'link', 'IKnow', 1)">
   I know when you click me
</a>;

to answer your second question: its exactly like above. you can put these custom functions anywhere you want.

to answer your third question: in the actions of your components

Rustyrut answered 6/10, 2022 at 10:13 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.