Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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", ,
Expand All @@ -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,
Expand Down
20 changes: 15 additions & 5 deletions R/usertracking.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ check_cols <- function(columns) {
"username",
"login",
"logout",
"duration"
"duration",
"tag"
)

if (is.null(columns)) {
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
#'
Expand All @@ -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) {
Expand All @@ -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(
Expand All @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion man/setup_sheet.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions man/use_logging.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion vignettes/adding-logging-to-a-shiny-app.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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`.
Expand Down