Usage tracking of shiny applications

Hi all,

Rstudio server pro is used to deploy the 10 applications. There are almost 40 users across these 10 applications.

Wanted to check if there is a way to track the users activity across these application. Like

  1. There Login time
  2. There Logout time
  3. No of users visiting the applications etc?

Is there a way to achieve this?

Hi, RStudio Server Pro (now Workbench) it is not really made to deploy Shiny applications that non-developers consume. The RStudio Connect product is where you want to deploy your Shiny apps. This article may help clarify: Dev/Test/Prod with RStudio Team - RStudio :: Solutions. Maybe a conversation with your Customer Success representative at RStudio may help clarify any questions about this.

Hi,

Here is a crude way in which you could track sessions

library(shiny)

ui <- fluidPage(
  
  tableOutput("myOutput")
  
)

#Track sessions outside the server function so the variable is permanent
#Alternative is to write to database or file
users = reactiveValues(
  logTable = data.frame(
    id = character(),
    login = character(),
    logout = character()
  )
)

server <- function(input, output, session) {
  
  #Register session start. Isolate needed for reactive environment
  isolate({
    users$logTable = rbind(
      users$logTable,
      list(id = session$token, 
           login = as.character(Sys.time()),
           logout = NA)
    )
  })
  
  #Register session end
  session$onSessionEnded(function(){
    isolate({
      users$logTable[users$logTable$id == session$token,
                     "logout"] = as.character(Sys.time())
    })
  })
  
  #Display info
  output$myOutput = renderTable({
    users$logTable
  })

}

shinyApp(ui, server)

image

Every time a users logs in or out, the table will be updated. Note that refreshing the page ends the session, so this simple code cannot see if a user refreshes or if the same user visits multiple times. This purely is counting sessions. The number of active sessions is the number of NA in the logout column of the table.

For a more permanent tracking you should replace the users variable with a function that writes the data to a file or database.

Hope this helps,
PJ

Small doubt. Can we also add user name here ? :slight_smile:

If your application asks the user to enter a username, of course you can add that info to the table.

no no. Without asking , can we not add it? Like session$username? Basically, it will be system login name?

Hi,

Shiny is a web-app and thus cannot access client system data. This would be a huge privacy issue :slight_smile: If your app runs online, you might be able to gather the user's IP address, if they are not blocking tracking, but otherwise the only real way I know of is to provide them with a log-in screen where they enter their username that you gave them.

PJ

Got it. But you know :slight_smile: I just added this code to the your codebase. This gives the username:) . Is it not correct?

list(id = session$token, 
           user = Sys.getenv("RSTUDIO_USER_IDENTITY"),   ## giving username
           login = as.character(Sys.time()),
           logout = NA)
    )

Also in the output you mentioned, there is no logout time capturing when the user closes the application right? But with this the main info (time the user stays on the application. Logout - Login) is not captured :slight_smile:

No that won't work,

This is the local user that is running the R session. When you host a app, this will then become the ID of the server, which is the same for everyone. Clients connect to your server which hosts the app.

PJ

Hi,

There is logout capturing. The logout column is the time the user quits the app (or refreshes the page). You can calculate time spent by subtracting logout from login. The number of rows in the table is the total number of sessions that was started, the number of NA in the logout is the total number of active session (i.e. user has not disconnected yet)

PJ

One last tip: if your app is running online (e.g. the internet and not just locally), you can use Google analytics to get much more details on how people use your app and analyse nearly everything you like

https://shiny.rstudio.com/articles/google-analytics.html

PJ

I see. Also forgot to tell u. When the user access our shiny applications he uses pam authentication to open the application. can we use this at least to see which user is using the application??

Hi,

I'm not sure about that, I have no experience with PAM authentication. Maybe @edgararuiz knows more about that.

PJ

Thanks. @edgararuiz can you help me here

Hi, in case if we provide the login page, how can we capture the username? :slight_smile:

If you just have an internal app with minimal security needs, you can just create a modal window that asks for a username and put that in your table. If you need a secure and more private way of real authentication you can look into packages like auth0.

PJ

Very useful! I have basically a Rmd file hosted in shinyapps.io. Is it possible to add it there too? Would the code look identical to the one written above? Thank you.

Hi,

I can't know that fur sure, it depends a bit on the app, but why don't you try and just find out :slight_smile:

PJ

1 Like

Thanks. I am going to try :slight_smile: Shall I put this piece of code in one of the Rmd cells? Where would the table be showing? I wouldn't want the table showing to the users.