diff --git a/DESCRIPTION b/DESCRIPTION index 2aa4b17..4e8aed8 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: shinyusertracking Title: Add user tracking to Shiny apps -Version: 1.0.3.000 +Version: 1.0.4.000 Authors@R: person( "Mark", "McPherson", , @@ -11,9 +11,11 @@ Description: Log session and user data to a Google sheet. License: MIT + file LICENSE Encoding: UTF-8 Roxygen: list(markdown = TRUE) -RoxygenNote: 7.3.1 +RoxygenNote: 7.3.3 Config/testthat/edition: 3 VignetteBuilder: knitr +Depends: + R (>= 4.1.0) Imports: googlesheets4, lubridate, diff --git a/R/usertracking.R b/R/usertracking.R index 19c7b6c..c96559f 100644 --- a/R/usertracking.R +++ b/R/usertracking.R @@ -96,7 +96,8 @@ check_cols <- function(columns) { "username", "login", "logout", - "duration" + "duration", + "tag" ) if (is.null(columns)) { @@ -127,7 +128,7 @@ check_cols <- function(columns) { #' setup_sheet("A new Shiny app", c("login", "logout", "duration")) #' } setup_sheet <- function(sheet_name = pkgload::pkg_name(), - columns = c("login", "logout", "duration"), + columns = c("login", "logout", "duration", "tag"), creds = ".google-sheets-credentials") { columns <- check_cols(columns) @@ -165,6 +166,7 @@ setup_sheet <- function(sheet_name = pkgload::pkg_name(), #' duration. By default login, logout and duration will be logged. #' @param creds File used to store credentials. Defaults to `.google-sheets-credentials`. #' @param no_local Whether to ignore local sessions. Default is TRUE. +#' @param tag A custom label for this app. If NULL, defaults to the app's URL. #' #' @return Nothing, used for side-effects only #' @@ -185,9 +187,9 @@ setup_sheet <- function(sheet_name = pkgload::pkg_name(), #' shinyApp(ui, server) #' } use_logging <- function(sheet_name = pkgload::pkg_name(), - columns = c("login", "logout", "duration"), + columns = c("login", "logout", "duration", "tag"), creds = ".google-sheets-credentials", - no_local = TRUE) { + no_local = TRUE, tag = NULL) { # If session appears to be local, do nothing is_server <- nzchar(Sys.getenv("SHINY_PORT")) if (no_local && !is_server) { @@ -209,6 +211,13 @@ use_logging <- function(sheet_name = pkgload::pkg_name(), session <- get("session", parent.frame()) + # Construct URL if tag is NULL + if (is.null(tag)) { + tag <- paste0(session$clientData$url_protocol, "//", + session$clientData$url_hostname, + session$clientData$url_pathname) + } + set_credentials(creds) googlesheets4::gs4_auth( @@ -221,7 +230,8 @@ use_logging <- function(sheet_name = pkgload::pkg_name(), username = ifelse(is.null(session$user), "unknown", session$user), login = Sys.time(), logout = lubridate::NA_POSIXct_, - duration = NA_character_ + duration = NA_character_, + tag = tag ) session$onSessionEnded(function() { diff --git a/man/setup_sheet.Rd b/man/setup_sheet.Rd index 2978534..4dd52e5 100644 --- a/man/setup_sheet.Rd +++ b/man/setup_sheet.Rd @@ -6,7 +6,7 @@ \usage{ setup_sheet( sheet_name = pkgload::pkg_name(), - columns = c("login", "logout", "duration"), + columns = c("login", "logout", "duration", "tag"), creds = ".google-sheets-credentials" ) } diff --git a/man/use_logging.Rd b/man/use_logging.Rd index 14537ce..5c13a88 100644 --- a/man/use_logging.Rd +++ b/man/use_logging.Rd @@ -6,9 +6,10 @@ \usage{ use_logging( sheet_name = pkgload::pkg_name(), - columns = c("login", "logout", "duration"), + columns = c("login", "logout", "duration", "tag"), creds = ".google-sheets-credentials", - no_local = TRUE + no_local = TRUE, + tag = NULL ) } \arguments{ @@ -20,6 +21,8 @@ duration. By default login, logout and duration will be logged.} \item{creds}{File used to store credentials. Defaults to \code{.google-sheets-credentials}.} \item{no_local}{Whether to ignore local sessions. Default is TRUE.} + +\item{tag}{A custom label for this app. If NULL, defaults to the app's URL.} } \value{ Nothing, used for side-effects only diff --git a/vignettes/adding-logging-to-a-shiny-app.Rmd b/vignettes/adding-logging-to-a-shiny-app.Rmd index b813553..5d0f86e 100644 --- a/vignettes/adding-logging-to-a-shiny-app.Rmd +++ b/vignettes/adding-logging-to-a-shiny-app.Rmd @@ -44,7 +44,7 @@ The file in which credentials are saved will be added to the `.gitignore` file a ## Set up logging sheet -Once the credentials are added you can add a new sheet for the app. To do this, again ensuring you are in the root directory of the R project containing the app, run `setup_sheet()`. This will append a new sheet to the sheet with ID provided earlier. By default, `login`, `logout` and `duration` will be logged. Although `sessionid` and `username` are also available, these have potential to be treated as PII. +Once the credentials are added you can add a new sheet for the app. To do this, again ensuring you are in the root directory of the R project containing the app, run `setup_sheet()`. This will append a new sheet to the sheet with ID provided earlier. By default, `login`, `logout`, `duration` and `tag` will be logged. Although `sessionid` and `username` are also available, these have potential to be treated as PII. Before the sheet can be added, authorisation must take place. You have two options. @@ -68,6 +68,12 @@ server <- function(input, output, session) { } ``` +By default, the `tag` used will be the deployed app's URL. You can set a custom `tag` like + +```r +shinyusertracking::use_logging(tag = "Version for 2023/24") +``` + ## Shiny apps as packages If your shiny app is a package, such as when using `golem`, you will need to add this package to your `DESCRIPTION` file in `Imports` and `Remotes`.