User Tracking in Shiny Apps: Leveraging {shiny.telemetry} for Comprehensive User Distinction

[This article was first published on Tag: r - Appsilon | Enterprise R Shiny Dashboards, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Delving into the realm of Shiny app development, the path to success often proves to be a complex and evolving landscape. Whether you’re a seasoned developer, a data enthusiast, or a tech-savvy manager, the obstacles of developing and delivering compelling applications remain.

In this dynamic environment, solutions like {shiny.telemetry} emerge as essential tools, offering not only advanced event-tracking capabilities but also a nuanced approach to addressing the intricacies of user engagement, adoption, and impact assessment.

Join us on this exploration of the significance of user event tracking, the integration of telemetry solutions, and how these advancements empower individuals across various roles to navigate the complexities of Shiny app development successfully.

Table of Contents

TL;DR:

  • {shiny.telemetry} is a versatile and technology-agnostic tool essential for monitoring user engagement, assessing the success of rollouts, and understanding user behaviour.
  • There are complexities involved in tracking interactions of users who are not logged in, particularly in environments utilizing technologies like Posit Connect.
  • Explore using cookies – small files stored on users’ browsers – to distinguish and track repeat visits by non-logged users effectively.
  • {shiny.telemetry} plays a crucial role in driving data-driven decision-making and product development, and has the ability to provide comprehensive insights for creating user-centric solutions.

The Business Imperative

The business imperative becomes evident as there is a pressing need to showcase the success of Data-Driven product rollouts to stakeholders. Tracking adoption emerges as a critical factor, emphasizing the importance of seamless communication within the team and the ability to identify early adopters.

Here are three key aspects that highlight the significance of user event tracking:

1. Strategic Decision-Making:

  • User event tracking facilitates strategic decision-making by providing real-time insights into user behaviour.
  • The ability to pinpoint early adopters and understand user challenges empowers teams to optimize product functionality proactively.

2. Demonstrating Impact:

  • Through tracking user events, the focus shifts from building solutions to creating impactful tools.
  • Utilizing granular data from tools like {shiny.telemetry} enables teams to showcase the tangible impact of their creations, impressing stakeholders with a results-oriented approach.

3. Continuous Improvement:

  • Embracing user event tracking instils a culture of continuous improvement within organizations.
  • Understanding feature usage patterns and promptly addressing user challenges allows teams to enhance their products iteratively, ensuring they evolve in tandem with the dynamic needs of users.

All these underscore the significance of robust user event-tracking solutions.

Introducing {shiny.telemetry}

At the forefront of addressing these challenges is {shiny.telemetry}. This versatile package not only facilitates user event tracking but is also technology agnostic, making it a comprehensive solution for organizations seeking impactful insights. The ability to showcase rollout success, monitor adoption trends, and identify potential challenges positions {shiny.telemetry} as an indispensable tool.

shiny.telemetry tracking

Here are some useful resources:

The Challenge

Navigating the intricacies of user engagement in Shiny app development often poses a significant challenge: tracking interactions of users who aren’t logged in, especially when leveraging technologies like Posit Connect.

The critical question emerges – how does one differentiate between non-logged users effectively? Moreover, the quest for technology agnosticism raises another concern: how can you obtain insights into the distinct users utilizing the app while remaining neutral to underlying technologies?

This challenge, if left unaddressed, can impede businesses from gaining a holistic view of user behaviour, potentially leading to misinterpretations of analytics. Fortunately, with the integration of {shiny.telemetry} and a cookies-based solution, we present a comprehensive approach to overcome these hurdles, providing a clearer understanding of user interactions regardless of their logged status.

Explore how R can transform your business workflows in our post: 5 Ways R Programming and R Shiny Can Improve Your Business Workflows.

Addressing the Not-Logged User Dilemma

The Cookie-Based Solution

A significant challenge in user event tracking is distinguishing non-logged users. The cookie-based approach provides a nuanced solution. Cookies, small files stored on a user’s browser, allow web applications to recognize repeated visits. By setting cookies for each visitor, even those not logged in, {shiny.telemetry} can distinguish repeated visits from the same browser, effectively differentiating one non-logged user from another.

Proxy Recognition

It’s important to note that the cookies-based approach acts as a proxy. In scenarios where the same user accesses the application using an incognito tab or clears browser data, the cookies-based approach will consider those as coming from different users.

Minimal telemetry Setup

Let’s kick off the Shiny app journey with a minimal setup to track events using telemetry. This isn’t just about creating a visually appealing interface; it’s about understanding user behaviour, preferences, and needs. Telemetry, in this context, becomes the silent observer, capturing invaluable data that can shape future iterations of the app. The app is designed to have a minimal setup to get us started on the path of event tracking.

library(shiny)
library(shiny.telemetry)


data_storage <- DataStorageLogFile$new("logs.txt")
telemetry <- Telemetry$new("myApp", data_storage)
shinyApp(
 ui = fluidPage(
   use_telemetry(),
   numericInput("n", "n", 1),
   plotOutput('plot')
 ),
 server = function(input, output) {
   telemetry$start_session()
   output$plot <- renderPlot({ hist(runif(input$n)) })
 }
)

In the first two lines of highlighted code above, we first set the scene in the global environment of a shiny app, by initializing a simple data storage to a .txt file and a telemetry R6 object.

Explore the fundamentals of Object-Oriented Programming in R: Dive into our concise guide for an easy introduction to OOP concepts.

The next highlighted line, the function call use_telemetry() attaches all necessary JS dependencies through the UI constructor of the shiny app.

Finally, telemetry$start_session() starts the session-specific tracking.

Extending telemetry With Cookies

Preparing the dependencies

Let’s set the groundwork by downloading a JavaScript Cookie library. We can easily grab it from an online CDN service:

fs::dir_create("www/")
download.file(
 url = "https://cdn.jsdelivr.net/npm/js-cookie@2/src/js.cookie.min.js",
 destfile = "www/js.cookie.js"
)

We will also need the following JS functions, added as {shinyjs} extensions for simplicity.

jsCode <- "
 shinyjs.bindcookie = function(params) {
   var cookie = Cookies.get(params[0]);
   Shiny.setInputValue('jscookie', [params[0], cookie]);
 }


 shinyjs.setcookie = function(params) {
   Cookies.set(params[1], escape(params[0]), { expires: 0.5 });
 }
"

shinyjs.bindcookie(): Fetches the value of a specified cookie in your Shiny app. Leveraging the js-cookie library updates a Shiny input to keep your app seamlessly synchronized with the cookie’s information.

shinyjs.setcookie(): Sets a cookie with a specific value and a user-friendly expiration time of 0.5 days.

The shiny.telemetry App

Regarding extending the previous minimal example with cookies, in the UI part of the app, we load the {shinyjs} dependencies, and also update the server function:

library(shiny)
library(shinyjs)
library(shiny.telemetry)


data_storage <- DataStorageLogFile$new("logs.txt")
telemetry <- Telemetry$new("myApp", data_storage)


shinyApp(
 ui = fluidPage(
   tags$head(tags$script(src = "js.cookie.js")),
   useShinyjs(),
   extendShinyjs(text = jsCode, functions = c("bindcookie", "setcookie")),
   use_telemetry(),
   numericInput("n", "n", 1),
   plotOutput('plot'),
   verbatimTextOutput('output')
 ),
 server = function(input, output) {
   telemetry$start_session(login = FALSE)
   js$bindcookie("uid")


   proxy_user_id <- reactive({
     uid <- input$jscookie[2]
     if(is.null(uid) || is.na(uid)) {
       uid <- uuid::UUIDgenerate() js$setcookie(uid, "uid") } uid }) |>
     bindEvent(input$jscookie, ignoreNULL = FALSE)


   observe({
     telemetry$log_login(username = proxy_user_id())
   })


   output$plot <- renderPlot({ hist(runif(input$n)) })
 }
)

1. js$bindcookie("uid"): Initialise the shiny custom binding to get access on the server side to the proxy user ID (UID) stored in the cookie.
2. checkCookie <- reactive({ ... }): Defines a reactive expression to manage the UID.

  • Attempts to extract the UID from the Shiny input variable jscookie.
  • If the UID is absent or NA, generate a new UID.
  • Sets the new UID as a cookie using js$setcookie.
  • Returns the UID for further use.

3. observe({ telemetry$log_login(username = proxy_user_id()) }): Observes UID changes and logs login events through telemetry.

Conclusion

In the journey of data-driven decision-making, {shiny.telemetry} emerge as an indispensable tool. By adopting this package, organizations not only address current challenges but also lay the foundation for a future where user-focused solutions dominate.

The nuances of tracking non-logged users through a cookie-based approach further solidify the effectiveness of these tools in providing comprehensive insights. With the right tools in hand, the path to successful Data-Driven product development becomes clearer, ensuring organizations stay at the forefront of innovation.

Interested in diving deeper into the capabilities of shiny.telemetry? Join André Veríssimo and Wlademir Prates at Shiny Gatherings #10 for an enlightening session titled ‘Open Source Spotlight: Navigating shiny.telemetry for User Tracking in Shiny Apps.’ Sign up now for the event on January 30th.

You May Also Like

This article was co-authored by Wlademir Prates.

The post appeared first on appsilon.com/blog/.

To leave a comment for the author, please follow the link and comment on their blog: Tag: r - Appsilon | Enterprise R Shiny Dashboards.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)