diff --git a/R/cookies.R b/R/cookies.R index 95255ab..6a6d6ff 100644 --- a/R/cookies.R +++ b/R/cookies.R @@ -244,15 +244,18 @@ cookie_banner_server <- function( #' init_cookies #' #' @description -#' init_cookies() creates a local copy of the JavaScript file -#' required for cookies to work. -#' It checks whether there is already a www/ folder and if not, it creates one. -#' It then checks whether the cookie-consent.js file exists in the www/ folder. -#' If the file exists, it will print a message in the console to let you know. -#' If the file doesn't exist, it will pull a copy from the GitHub repo. -#' If it cannot connect to the repo then it will print "Download failed". -#' No input parameters are required -#' Call init_cookies() in the console to run the function +#' init_cookies() creates a local copy of the JavaScript file required for +#' cookies to work. It checks whether there is already a www/ folder and if +#' not, it creates one. It then checks whether the cookie-consent.js file +#' exists in the www/ folder. If the file exists, it will print a message in +#' the console to let you know it has overwritten it. If the file doesn't +#' exist, it will make one and confirm it has done so. +#' +#' No input parameters are required, run `init_cookies()` in the console to run +#' the function +#' +#' @param create_file Boolean, TRUE by default, if FALSE then will print to +#' the console rather than create a new file #' #' @return NULL #' @export @@ -261,29 +264,58 @@ cookie_banner_server <- function( #' if (interactive()) { #' init_cookies() #' } -init_cookies <- function() { - sub_dir <- "www" +init_cookies <- function(create_file = TRUE) { + if (!is.logical(create_file)) { + stop("create_file must always be TRUE or FALSE") + } - output_dir <- file.path(sub_dir) + # Create the JS for the file ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + cookie_js <- "function getCookies(){ + var res = Cookies.get(); + Shiny.setInputValue('cookies', res); +} - if (!dir.exists(output_dir)) { - dir.create(output_dir) - } else { - message("www folder already exists!") - } +Shiny.addCustomMessageHandler('cookie-set', function(msg){ + Cookies.set(msg.name, msg.value); + getCookies(); +}) - github_area <- "https://raw.githubusercontent.com/dfe-analytical-services/" +Shiny.addCustomMessageHandler('cookie-clear', function(msg){ + Cookies.remove(msg.name); + getCookies(); +}) - tryCatch( - utils::download.file( - url = paste0(github_area, "dfeshiny/main/inst/cookie-consent.js"), - destfile = "www/cookie-consent.js" - ), - error = function(e) { - return("Download failed") - }, - message("Cookie script updated") - ) +$(document).on('shiny:connected', function(ev){ + getCookies(); +}) + +Shiny.addCustomMessageHandler('analytics-consent', function(msg){ + gtag('consent', 'update', { + 'analytics_storage': msg.value + }); +})" + # End of JS for the file ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + if (create_file == TRUE) { + sub_dir <- "www" + + output_dir <- file.path(sub_dir) + + if (!dir.exists(output_dir)) { + dir.create(output_dir) + } + + if (file.exists("www/cookie-consent.js")) { + message("www/cookie-consent.js already exists, updating file...") + cat(cookie_js, file = "www/cookie-consent.js", sep = "\n") + message("...file successfully updated") + } else { + cat(cookie_js, file = "www/cookie-consent.js", sep = "\n") + message("Created cookies script as www/cookie-consent.js") + } + } else { + cat(cookie_js, file = "", sep = "\n") + } } #' cookies_panel_ui diff --git a/inst/cookie-consent.js b/inst/cookie-consent.js deleted file mode 100644 index d3f220f..0000000 --- a/inst/cookie-consent.js +++ /dev/null @@ -1,26 +0,0 @@ -function getCookies(){ - var res = Cookies.get(); - Shiny.setInputValue('cookies', res); -} - -Shiny.addCustomMessageHandler('cookie-set', function(msg){ - Cookies.set(msg.name, msg.value); - getCookies(); -}) - -Shiny.addCustomMessageHandler('cookie-clear', function(msg){ - Cookies.remove(msg.name); - getCookies(); -}) - -$(document).on('shiny:connected', function(ev){ - getCookies(); -}) - -Shiny.addCustomMessageHandler('analytics-consent', function(msg){ - gtag('consent', 'update', { - 'analytics_storage': msg.value - }); -}) - - diff --git a/man/init_analytics.Rd b/man/init_analytics.Rd index 9d75c1f..01dcdc0 100644 --- a/man/init_analytics.Rd +++ b/man/init_analytics.Rd @@ -12,14 +12,13 @@ init_analytics(ga_code, create_file = TRUE) \item{create_file}{Boolean TRUE or FALSE, default is TRUE, false will return the HTML in the console and is used mainly for testing or comparisons} } -\value{ -TRUE if written, FALSE if not, character vector of HTML if create_file = FALSE -} \description{ Creates the google-analytics.html script in order to allow the activation of analytics via GA4. For the full steps required to set up analytics, please refer to the documentation in the README. } \examples{ -init_analytics(ga_code = "0123456789") +if (interactive()) { + init_analytics(ga_code = "0123456789") +} } diff --git a/man/init_cookies.Rd b/man/init_cookies.Rd index fc03492..58c4475 100644 --- a/man/init_cookies.Rd +++ b/man/init_cookies.Rd @@ -4,18 +4,22 @@ \alias{init_cookies} \title{init_cookies} \usage{ -init_cookies() +init_cookies(create_file = TRUE) +} +\arguments{ +\item{create_file}{Boolean, TRUE by default, if FALSE then will print to +the console rather than create a new file} } \description{ -init_cookies() creates a local copy of the JavaScript file -required for cookies to work. -It checks whether there is already a www/ folder and if not, it creates one. -It then checks whether the cookie-consent.js file exists in the www/ folder. -If the file exists, it will print a message in the console to let you know. -If the file doesn't exist, it will pull a copy from the GitHub repo. -If it cannot connect to the repo then it will print "Download failed". -No input parameters are required -Call init_cookies() in the console to run the function +init_cookies() creates a local copy of the JavaScript file required for +cookies to work. It checks whether there is already a www/ folder and if +not, it creates one. It then checks whether the cookie-consent.js file +exists in the www/ folder. If the file exists, it will print a message in +the console to let you know it has overwritten it. If the file doesn't +exist, it will make one and confirm it has done so. + +No input parameters are required, run \code{init_cookies()} in the console to run +the function } \examples{ if (interactive()) { diff --git a/tests/testthat/test-init_cookies.R b/tests/testthat/test-init_cookies.R new file mode 100644 index 0000000..da6f745 --- /dev/null +++ b/tests/testthat/test-init_cookies.R @@ -0,0 +1,14 @@ +test_that("Rejects non-boolean for create_file", { + expect_error(init_cookies(create_file = "Funky non-boolean")) +}) + +test_that("Javascript writes out", { + output <- capture.output(init_cookies(create_file = FALSE)) + + # Expect to find a couple of other key parts + expect_equal(output[1], "function getCookies(){") + expect_true(grepl("Shiny\\.setInputValue\\('cookies'", output[3])) + expect_true(grepl("cookie-set", output[6])) + expect_true(grepl("cookie-clear", output[11])) + expect_true(grepl("analytics-consent", output[20])) +})