From 6260d780b311322446084c06a619c1dde55a47b2 Mon Sep 17 00:00:00 2001 From: Rich Bielby Date: Wed, 8 Nov 2023 10:50:53 +0000 Subject: [PATCH 01/34] Added initialise google analytics function. --- .gitignore | 1 + R/initialisers.R | 93 +++++++++++++++++++++++++++++++++++++++++++ google-analytics.html | 66 ++++++++++++++++++++++++++++++ 3 files changed, 160 insertions(+) create mode 100644 R/initialisers.R create mode 100644 google-analytics.html diff --git a/.gitignore b/.gitignore index 5b6a065..b576802 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ .Rhistory .RData .Ruserdata +google_analytics.html diff --git a/R/initialisers.R b/R/initialisers.R new file mode 100644 index 0000000..6d8e092 --- /dev/null +++ b/R/initialisers.R @@ -0,0 +1,93 @@ +library(dplyr) +library(stringr) + +#' initialise_analytics +#' +#' @param ga_code The Google Analytics code +#' +#' @return +#' @export +#' +#' @examples +initialise_analytics <- function(ga_code = "XXXX") { + html_script <- " + + + + + " %>% gsub("XXXXXXXXXX", ga_code, .) + if (file.exists("google-analytics.html")) { + warning("Analytics file already exists. If you have any customisations in that file, make sure you've backed those up before over-writing.") + user_input <- str_trim(readline(prompt = "Are you happy to overwrite the existing analytics script (y/N)")) + if (user_input %in% c("y", "Y")) { + permission_to_write <- TRUE + } else { + permission_to_write <- FALSE + } + } else { + permission_to_write <- TRUE + } + if (overwrite) { + cat(html_script, file = "google-analytics.html") + } +} diff --git a/google-analytics.html b/google-analytics.html new file mode 100644 index 0000000..a853ee3 --- /dev/null +++ b/google-analytics.html @@ -0,0 +1,66 @@ + + + + + + \ No newline at end of file From 23568ef8acd37ae2caf96acf83b5168e6631f655 Mon Sep 17 00:00:00 2001 From: Rich Bielby Date: Wed, 8 Nov 2023 10:57:40 +0000 Subject: [PATCH 02/34] Added extra documentation info --- DESCRIPTION | 4 +++- NAMESPACE | 1 + R/initialisers.R | 7 ++++--- R/testing.R | 4 ++-- man/initialise_analytics.Rd | 20 ++++++++++++++++++++ man/tidy_code.Rd | 6 ++++++ 6 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 man/initialise_analytics.Rd diff --git a/DESCRIPTION b/DESCRIPTION index af82084..6ee3a4c 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -10,6 +10,8 @@ Encoding: UTF-8 Imports: shinyGovstyle, shiny, - styler + styler, + stringr, + dplyr Roxygen: list(markdown = TRUE) RoxygenNote: 7.2.3 diff --git a/NAMESPACE b/NAMESPACE index 1fd58ea..db8b940 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,4 +1,5 @@ # Generated by roxygen2: do not edit by hand +export(initialise_analytics) export(support_panel) export(tidy_code) diff --git a/R/initialisers.R b/R/initialisers.R index 6d8e092..71f1532 100644 --- a/R/initialisers.R +++ b/R/initialisers.R @@ -5,11 +5,11 @@ library(stringr) #' #' @param ga_code The Google Analytics code #' -#' @return +#' @return TRUE if written, FALSE if not #' @export #' -#' @examples -initialise_analytics <- function(ga_code = "XXXX") { +#' @examples initialise_analytics(ga_code = "0123456789") +initialise_analytics <- function(ga_code = "0123456789") { html_script <- " " %>% gsub("XXXXXXXXXX", ga_code, .) if (file.exists("google-analytics.html")) { - warning("Analytics file already exists. If you have any customisations in that file, make sure you've backed those up before over-writing.") - user_input <- str_trim(readline(prompt = "Are you happy to overwrite the existing analytics script (y/N)")) + message("Analytics file already exists. If you have any customisations in that file, make sure you've backed those up before over-writing.") + user_input <- str_trim(readline(prompt = "Are you happy to overwrite the existing analytics script (y/N) ")) if (user_input %in% c("y", "Y")) { - permission_to_write <- TRUE + write_out <- TRUE } else { - permission_to_write <- FALSE + write_out <- FALSE } } else { - permission_to_write <- TRUE + write_out <- TRUE } - if (overwrite) { + if (write_out) { cat(html_script, file = "google-analytics.html") + message("Google analytics script created in google-analytics.html.") + message("You'll need to add the following line to your ui.R script to start + recording analytics:") + message('tags$head(includeHTML(("google-analytics.html"))),') + } else { + message("Original Google analytics html script left in place.") } - return(permission_to_write) } diff --git a/google-analytics.html b/google-analytics.html index a853ee3..4ccb68e 100644 --- a/google-analytics.html +++ b/google-analytics.html @@ -12,18 +12,18 @@ - + " %>% gsub("XXXXXXXXXX", ga_code, .) if (file.exists("google-analytics.html")) { - message("Analytics file already exists. If you have any customisations in that file, make sure you've backed those up before over-writing.") - user_input <- stringr::str_trim(readline(prompt = "Are you happy to overwrite the existing analytics script (y/N) ")) + message("Analytics file already exists.") + mesage("If you have any customisations in that file, make sure you've backed + those up before over-writing.") + user_input <- stringr::str_trim( + readline( + prompt = "Are you happy to overwrite the existing analytics script (y/N) " + ) + ) if (user_input %in% c("y", "Y")) { write_out <- TRUE } else { From f46e7e615f194cff5eb40b677e7b011447c60ece Mon Sep 17 00:00:00 2001 From: Rich Bielby Date: Wed, 27 Mar 2024 13:25:07 +0000 Subject: [PATCH 14/34] Cleaning up line lengths in tidy code --- R/tidy_code.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/tidy_code.R b/R/tidy_code.R index e5f860c..238f83a 100644 --- a/R/tidy_code.R +++ b/R/tidy_code.R @@ -6,7 +6,8 @@ #' @param subdirs List of sub-directories to #' (recursively search for R scripts to be styled) #' -#' @return TRUE if any changes have been made to any scripts, FALSE if all passed. +#' @return TRUE if any changes have been made to any scripts, FALSE if all +#' passed. #' @export #' #' @examples From 5a45bf8b3af8f2e2c10ba02ab7b6fa995c982043 Mon Sep 17 00:00:00 2001 From: Rich Bielby Date: Wed, 27 Mar 2024 13:45:15 +0000 Subject: [PATCH 15/34] Updated installation guidance in readme --- README.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 720f597..180725d 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,14 @@ R package containing preferred methods for creating official DfE R-Shiny dashboa # Installing the package To install, run `renv::install("dfe-analytical-services/dfeshiny")`. +## Installing functionality in development from a branch + +It can be useful when developing the package in particular to trial new or updated functionality in a shiny app. To do this, you can install the required branch using (replacing `branch-name` with the name of the branch you wish to install): + +`renv::install("dfe-analytical-services/dfeshiny@branch-name")` + +That will install the code from the named branch as dfeshiny in your app. You will need to restart your R session before it will start using the latest version that you've installed. + ## Potential errors when installing If you get `ERROR [curl: (22) The requested URL returned error: 401]`, and don't know why, try running `Sys.unsetenv("GITHUB_PAT")` to temporarily clear your GitHub PAT variable. @@ -26,7 +34,7 @@ For analytics to function on your dashboard, you will need to: - request a Google Analytics key from the [Statistics Development Team](mailto:statistics.development@education.gov.uk) - create a html file with the javascript required for your dashboard to connect to Google Analytics -To create the latter, we provide the function `dfeshiny::initialise_analytics()`. You should run this code from the R console providing your Google Analytics code as follows: +To create the latter, we provide the function `dfeshiny::initialise_analytics()`. You should run this code from the R console providing your Google Analytics code as follows (replacing `ABCDE12345` with the code obtained from the Statistics Devlopment team): ``` initialise_analytics("ABCDE12345") From a7c4d5b4c322e461ca92b9a9fea1dd65ffb8bd84 Mon Sep 17 00:00:00 2001 From: Rich Bielby Date: Wed, 27 Mar 2024 14:07:25 +0000 Subject: [PATCH 16/34] Updated installation guidance in readme --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 180725d..51a9abc 100644 --- a/README.md +++ b/README.md @@ -33,8 +33,9 @@ For analytics to function on your dashboard, you will need to: - request a Google Analytics key from the [Statistics Development Team](mailto:statistics.development@education.gov.uk) - create a html file with the javascript required for your dashboard to connect to Google Analytics +- add the line: `tags$head(includeHTML(("google-analytics.html"))),` to the ui.R file. -To create the latter, we provide the function `dfeshiny::initialise_analytics()`. You should run this code from the R console providing your Google Analytics code as follows (replacing `ABCDE12345` with the code obtained from the Statistics Devlopment team): +To create the latter, we provide the function `dfeshiny::initialise_analytics()`. You should run this code from the R console providing your Google Analytics code as follows (replacing `ABCDE12345` with the code obtained from the Statistics Development team): ``` initialise_analytics("ABCDE12345") From 1bb8e70bd848d3327a459b6e7d1bb1f357a8114d Mon Sep 17 00:00:00 2001 From: Rich Bielby Date: Wed, 27 Mar 2024 16:25:37 +0000 Subject: [PATCH 17/34] Adjusting line lengths in analyics function --- R/initialisers.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/initialisers.R b/R/initialisers.R index 9d46d5f..8a3ba1b 100644 --- a/R/initialisers.R +++ b/R/initialisers.R @@ -43,7 +43,8 @@ gtag('consent', 'default', { gtag('config', 'G-XXXXXXXXXX'); /* -The custom trackers below can be tailored to match the inputs used in your dashboard. +The custom trackers below can be tailored to match the inputs used in your +dashboard. */ $(document).on('change', 'select#selectPhase', function(e) { From 84a37211b9f7c9d3264efb6c3d82b5019a0bf0b5 Mon Sep 17 00:00:00 2001 From: Rich Bielby Date: Wed, 27 Mar 2024 16:27:45 +0000 Subject: [PATCH 18/34] Adjusted messaging spacing in initialiser script --- R/initialisers.R | 1 + 1 file changed, 1 insertion(+) diff --git a/R/initialisers.R b/R/initialisers.R index 8a3ba1b..1d8f008 100644 --- a/R/initialisers.R +++ b/R/initialisers.R @@ -105,6 +105,7 @@ dashboard. } if (write_out) { cat(html_script, file = "google-analytics.html") + message("") message("Google analytics script created in google-analytics.html.") message("You'll need to add the following line to your ui.R script to start recording analytics:") From 07d1bb2751c4e3c2e0d63297e0c945a093779703 Mon Sep 17 00:00:00 2001 From: Rich Bielby Date: Wed, 27 Mar 2024 16:49:02 +0000 Subject: [PATCH 19/34] Tweaks to initialisers code --- R/initialisers.R | 2 +- man/tidy_code.Rd | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/R/initialisers.R b/R/initialisers.R index 1d8f008..6ccdea4 100644 --- a/R/initialisers.R +++ b/R/initialisers.R @@ -88,7 +88,7 @@ dashboard. " %>% gsub("XXXXXXXXXX", ga_code, .) if (file.exists("google-analytics.html")) { message("Analytics file already exists.") - mesage("If you have any customisations in that file, make sure you've backed + message("If you have any customisations in that file, make sure you've backed those up before over-writing.") user_input <- stringr::str_trim( readline( diff --git a/man/tidy_code.Rd b/man/tidy_code.Rd index 373cdb1..24d74b1 100644 --- a/man/tidy_code.Rd +++ b/man/tidy_code.Rd @@ -11,7 +11,8 @@ tidy_code(subdirs = c("R", "tests")) (recursively search for R scripts to be styled)} } \value{ -TRUE if any changes have been made to any scripts, FALSE if all passed. +TRUE if any changes have been made to any scripts, FALSE if all +passed. } \description{ Script to apply styler code styling to scripts within the From e9e0d8fe32b987a9925da1559affa5d56858b7a3 Mon Sep 17 00:00:00 2001 From: Rich Bielby Date: Wed, 27 Mar 2024 16:54:34 +0000 Subject: [PATCH 20/34] Removed google analyics html --- DESCRIPTION | 1 - google-analytics.html | 66 ------------------------------------------- 2 files changed, 67 deletions(-) delete mode 100644 google-analytics.html diff --git a/DESCRIPTION b/DESCRIPTION index 3711721..9adf761 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -10,7 +10,6 @@ Description: R package containing preferred methods for creating official DfE R-Shiny dashboards. License: GPL (>= 3) Imports: - dplyr, magrittr, shiny, shinyGovstyle, diff --git a/google-analytics.html b/google-analytics.html deleted file mode 100644 index 4ccb68e..0000000 --- a/google-analytics.html +++ /dev/null @@ -1,66 +0,0 @@ - - - - - - \ No newline at end of file From f1343d5f082870ec71f11f41c550e08d81a7ce0f Mon Sep 17 00:00:00 2001 From: Rich Bielby Date: Mon, 22 Jul 2024 10:56:56 +0100 Subject: [PATCH 21/34] Reconfigured analytics initialiser to pull template script from GitHub --- R/initialisers.R | 77 ++++----------------------------------- inst/google-analytics.hml | 66 +++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 70 deletions(-) create mode 100644 inst/google-analytics.hml diff --git a/R/initialisers.R b/R/initialisers.R index 6ccdea4..5cb36ce 100644 --- a/R/initialisers.R +++ b/R/initialisers.R @@ -1,12 +1,12 @@ -#' initialise_analytics +init_analytics #' #' @param ga_code The Google Analytics code #' @importFrom magrittr %>% #' @return TRUE if written, FALSE if not #' @export #' -#' @examples initialise_analytics(ga_code = "0123456789") -initialise_analytics <- function(ga_code) { +#' @examples init_analytics(ga_code = "0123456789") +init_analytics <- function(ga_code) { is_valid_ga4_code <- function(ga_code) { stringr::str_length(ga_code) == 10 & typeof(ga_code) == "character" } @@ -19,73 +19,10 @@ initialise_analytics <- function(ga_code) { ) } - html_script <- " - - - - - " %>% gsub("XXXXXXXXXX", ga_code, .) + webpage <- getURL("https://raw.githubusercontent.com/dfe-analytical-services/dfeshiny/analytsics-init/inst/google-analytics.hml") + html_script <- readLines(tc <- textConnection(webpage)) + %>% gsub("XXXXXXXXXX", ga_code, .) + close(tc) if (file.exists("google-analytics.html")) { message("Analytics file already exists.") message("If you have any customisations in that file, make sure you've backed diff --git a/inst/google-analytics.hml b/inst/google-analytics.hml new file mode 100644 index 0000000..f7343e8 --- /dev/null +++ b/inst/google-analytics.hml @@ -0,0 +1,66 @@ + + + + + From 0a754ab151278067977cb13067a2255790cff64a Mon Sep 17 00:00:00 2001 From: Rich Bielby Date: Mon, 22 Jul 2024 11:01:17 +0100 Subject: [PATCH 22/34] Rebuilt package after init_analytics updates --- NAMESPACE | 4 ++-- R/initialisers.R | 6 +++--- man/{initialise_analytics.Rd => init_analytics.Rd} | 12 ++++++------ 3 files changed, 11 insertions(+), 11 deletions(-) rename man/{initialise_analytics.Rd => init_analytics.Rd} (55%) diff --git a/NAMESPACE b/NAMESPACE index 557ad81..c1bfba7 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -6,10 +6,10 @@ export(cookies_panel_server) export(cookies_panel_ui) export(custom_disconnect_message) export(dfe_cookie_script) -export(initialise_analytics) +export(init_analytics) export(init_cookies) export(support_panel) export(tidy_code) -importFrom(magrittr,"%>%") importFrom(htmltools,tagList) importFrom(htmltools,tags) +importFrom(magrittr,"%>%") diff --git a/R/initialisers.R b/R/initialisers.R index 5cb36ce..b7aa402 100644 --- a/R/initialisers.R +++ b/R/initialisers.R @@ -1,4 +1,4 @@ -init_analytics +#' init_analytics #' #' @param ga_code The Google Analytics code #' @importFrom magrittr %>% @@ -20,8 +20,8 @@ init_analytics <- function(ga_code) { } webpage <- getURL("https://raw.githubusercontent.com/dfe-analytical-services/dfeshiny/analytsics-init/inst/google-analytics.hml") - html_script <- readLines(tc <- textConnection(webpage)) - %>% gsub("XXXXXXXXXX", ga_code, .) + html_script <- readLines(tc <- textConnection(webpage)) %>% + gsub("XXXXXXXXXX", ga_code, .) close(tc) if (file.exists("google-analytics.html")) { message("Analytics file already exists.") diff --git a/man/initialise_analytics.Rd b/man/init_analytics.Rd similarity index 55% rename from man/initialise_analytics.Rd rename to man/init_analytics.Rd index 54e6959..cebfe6c 100644 --- a/man/initialise_analytics.Rd +++ b/man/init_analytics.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/initialisers.R -\name{initialise_analytics} -\alias{initialise_analytics} -\title{initialise_analytics} +\name{init_analytics} +\alias{init_analytics} +\title{init_analytics} \usage{ -initialise_analytics(ga_code) +init_analytics(ga_code) } \arguments{ \item{ga_code}{The Google Analytics code} @@ -13,8 +13,8 @@ initialise_analytics(ga_code) TRUE if written, FALSE if not } \description{ -initialise_analytics +init_analytics } \examples{ -initialise_analytics(ga_code = "0123456789") +init_analytics(ga_code = "0123456789") } From 4ee5c8cc2aeb3f3cbaa524f616888a77745febb2 Mon Sep 17 00:00:00 2001 From: Rich Bielby Date: Mon, 22 Jul 2024 11:30:39 +0100 Subject: [PATCH 23/34] Few minor updates around analytics initialisation. --- R/cookies.R | 4 +++- R/initialisers.R | 24 +++++++++++++++++++----- google-analytics.html | 1 + man/init_analytics.Rd | 4 +++- 4 files changed, 26 insertions(+), 7 deletions(-) create mode 100644 google-analytics.html diff --git a/R/cookies.R b/R/cookies.R index dae66be..2fed642 100644 --- a/R/cookies.R +++ b/R/cookies.R @@ -235,7 +235,9 @@ init_cookies <- function() { } tryCatch( - download.file(url = "https://raw.githubusercontent.com/dfe-analytical-services/dfeshiny/main/inst/cookie-consent.js", destfile = "www/cookie-consent.js"), # nolint: [line_length_linter] + utils::download.file( + url = "https://raw.githubusercontent.com/dfe-analytical-services/dfeshiny/main/inst/cookie-consent.js", # nolint: [line_length_linter] + destfile = "www/cookie-consent.js"), error = function(e) { return("Download failed") }, diff --git a/R/initialisers.R b/R/initialisers.R index b7aa402..df18ac0 100644 --- a/R/initialisers.R +++ b/R/initialisers.R @@ -1,4 +1,8 @@ #' init_analytics +#' @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. #' #' @param ga_code The Google Analytics code #' @importFrom magrittr %>% @@ -19,14 +23,24 @@ init_analytics <- function(ga_code) { ) } - webpage <- getURL("https://raw.githubusercontent.com/dfe-analytical-services/dfeshiny/analytsics-init/inst/google-analytics.hml") - html_script <- readLines(tc <- textConnection(webpage)) %>% - gsub("XXXXXXXXXX", ga_code, .) + webpage <- RCurl::getURL("https://raw.githubusercontent.com/dfe-analytical-services/dfeshiny/analytsics-init/inst/google-analytics.hml") + tryCatch( + html_script <- gsub( + "XXXXXXXXXX", + ga_code, + readLines(tc <- textConnection(webpage)) + ), + error = function(e) { + return("Download failed") + }, + message("Downloaded analytics template script") + ) + close(tc) if (file.exists("google-analytics.html")) { message("Analytics file already exists.") - message("If you have any customisations in that file, make sure you've backed - those up before over-writing.") + message("If you have any customisations in that file, make sure you've + backed those up before over-writing.") user_input <- stringr::str_trim( readline( prompt = "Are you happy to overwrite the existing analytics script (y/N) " diff --git a/google-analytics.html b/google-analytics.html new file mode 100644 index 0000000..26699c1 --- /dev/null +++ b/google-analytics.html @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/man/init_analytics.Rd b/man/init_analytics.Rd index cebfe6c..37ed35d 100644 --- a/man/init_analytics.Rd +++ b/man/init_analytics.Rd @@ -13,7 +13,9 @@ init_analytics(ga_code) TRUE if written, FALSE if not } \description{ -init_analytics +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") From 117cf3576842452025abc24ba7c69e8c0feaebae Mon Sep 17 00:00:00 2001 From: Rich Bielby Date: Mon, 22 Jul 2024 12:44:57 +0100 Subject: [PATCH 24/34] Removed google-analytics.html --- google-analytics.html | 1 - 1 file changed, 1 deletion(-) delete mode 100644 google-analytics.html diff --git a/google-analytics.html b/google-analytics.html deleted file mode 100644 index 26699c1..0000000 --- a/google-analytics.html +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file From 39a7715702ac16b92ae65ce88b4cc3c151cbc97e Mon Sep 17 00:00:00 2001 From: Rich Bielby Date: Mon, 22 Jul 2024 12:46:00 +0100 Subject: [PATCH 25/34] Corrected reference to google-analytics.html in .gitignore --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 366d47f..234462f 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,7 @@ .Rhistory .RData .Ruserdata -google_analytics.html +google-analytics.html docs inst/doc www/ From 15479bc1c11d1925c4efeb1fe8fd099f012230cb Mon Sep 17 00:00:00 2001 From: Rich Bielby Date: Mon, 22 Jul 2024 12:55:05 +0100 Subject: [PATCH 26/34] Handful of PR change requests on analytics initiailser --- R/{initialisers.R => analytics.R} | 32 +-- R/cookies.R | 325 +++++++++++++++--------------- inst/google-analytics.hml | 66 ------ 3 files changed, 180 insertions(+), 243 deletions(-) rename R/{initialisers.R => analytics.R} (72%) delete mode 100644 inst/google-analytics.hml diff --git a/R/initialisers.R b/R/analytics.R similarity index 72% rename from R/initialisers.R rename to R/analytics.R index df18ac0..ed498f8 100644 --- a/R/initialisers.R +++ b/R/analytics.R @@ -23,17 +23,23 @@ init_analytics <- function(ga_code) { ) } - webpage <- RCurl::getURL("https://raw.githubusercontent.com/dfe-analytical-services/dfeshiny/analytsics-init/inst/google-analytics.hml") + github_area <- "https://raw.githubusercontent.com/dfe-analytical-services/" + webpage <- RCurl::getURL( + paste0( + github_area, + "dfeshiny/analytsics-init/inst/google-analytics.hml" + ) + ) tryCatch( - html_script <- gsub( - "XXXXXXXXXX", - ga_code, - readLines(tc <- textConnection(webpage)) - ), - error = function(e) { - return("Download failed") - }, - message("Downloaded analytics template script") + html_script <- gsub( + "XXXXXXXXXX", + ga_code, + readLines(tc <- textConnection(webpage)) + ), + error = function(e) { + return("Download failed") + }, + message("Downloaded analytics template script") ) close(tc) @@ -42,8 +48,8 @@ init_analytics <- function(ga_code) { message("If you have any customisations in that file, make sure you've backed those up before over-writing.") user_input <- stringr::str_trim( - readline( - prompt = "Are you happy to overwrite the existing analytics script (y/N) " + readline( + prompt = "Are you happy to overwrite the existing analytics script (y/N) " ) ) if (user_input %in% c("y", "Y")) { @@ -55,7 +61,7 @@ init_analytics <- function(ga_code) { write_out <- TRUE } if (write_out) { - cat(html_script, file = "google-analytics.html") + cat(html_script, file = "google-analytics.html", sep = '\n') message("") message("Google analytics script created in google-analytics.html.") message("You'll need to add the following line to your ui.R script to start diff --git a/R/cookies.R b/R/cookies.R index 2fed642..261da09 100644 --- a/R/cookies.R +++ b/R/cookies.R @@ -236,8 +236,9 @@ init_cookies <- function() { tryCatch( utils::download.file( - url = "https://raw.githubusercontent.com/dfe-analytical-services/dfeshiny/main/inst/cookie-consent.js", # nolint: [line_length_linter] - destfile = "www/cookie-consent.js"), + url = "https://raw.githubusercontent.com/dfe-analytical-services/dfeshiny/main/inst/cookie-consent.js", # nolint: [line_length_linter] + destfile = "www/cookie-consent.js" + ), error = function(e) { return("Download failed") }, @@ -245,196 +246,192 @@ init_cookies <- function() { ) } - #' cookies_panel_ui - #' - #' @description - #' Create the standard DfE R-Shiny cookies dashboard panel in the ui. The server - #' side functionality is provided by cookies_panel_server() - #' - #' @param id ID shared with cookies_panel_server() - #' @param google_analytics_key Provide the GA 10 digit key of the form - #' "ABCDE12345" - #' - #' - #' @return a standardised panel for a public R Shiny dashboard in DfE - #' @export - #' - #' @examples - #' \dontrun{ - #' cookies_panel_ui( - #' id = "cookies_panel", - #' google_analytics_key = "ABCDE12345" - #' ) - #' } - #' - #' - cookies_panel_ui <- function( - id, google_analytics_key = NULL) { - # Build the support page ---------------------------------------------------- - shiny::tabPanel( - id = shiny::NS(id, "cookies_panel"), - value = "cookies_panel_ui", - "Cookies", - shinyGovstyle::gov_main_layout( - shinyGovstyle::gov_row( - shiny::column( - width = 12, - shiny::tags$h1("Cookies"), - shiny::tags$p("Cookies are small files saved on your phone, tablet or +#' cookies_panel_ui +#' +#' @description +#' Create the standard DfE R-Shiny cookies dashboard panel in the ui. The server +#' side functionality is provided by cookies_panel_server() +#' +#' @param id ID shared with cookies_panel_server() +#' @param google_analytics_key Provide the GA 10 digit key of the form +#' "ABCDE12345" +#' +#' +#' @return a standardised panel for a public R Shiny dashboard in DfE +#' @export +#' +#' @examples +#' \dontrun{ +#' cookies_panel_ui( +#' id = "cookies_panel", +#' google_analytics_key = "ABCDE12345" +#' ) +#' } +#' +cookies_panel_ui <- function(id, google_analytics_key = NULL) { + # Build the support page ---------------------------------------------------- + shiny::tabPanel( + id = shiny::NS(id, "cookies_panel"), + value = "cookies_panel_ui", + "Cookies", + shinyGovstyle::gov_main_layout( + shinyGovstyle::gov_row( + shiny::column( + width = 12, + shiny::tags$h1("Cookies"), + shiny::tags$p("Cookies are small files saved on your phone, tablet or computer when you visit a website."), - shiny::tags$p("We use cookies to collect information about how you + shiny::tags$p("We use cookies to collect information about how you use our service."), - shiny::tags$h2("Essential cookies"), - shinyGovstyle::govTable( - inputId = "essential_cookies_table", - df = data.frame( - Name = "dfe_analytics", - Purpose = "Saves your cookie consent settings", - Expires = "365 days" - ), - caption = "", - caption_size = "s", - num_col = NULL, - width_overwrite = c("one-quarter", "one-quarter", "one-quarter") + shiny::tags$h2("Essential cookies"), + shinyGovstyle::govTable( + inputId = "essential_cookies_table", + df = data.frame( + Name = "dfe_analytics", + Purpose = "Saves your cookie consent settings", + Expires = "365 days" ), - shiny::tags$h2("Analytics cookies"), - shiny::tags$p("With your permission, we use Google Analytics to + caption = "", + caption_size = "s", + num_col = NULL, + width_overwrite = c("one-quarter", "one-quarter", "one-quarter") + ), + shiny::tags$h2("Analytics cookies"), + shiny::tags$p("With your permission, we use Google Analytics to collect data about how you use this service. This information helps us improve our service"), - shiny::tags$p("Google is not allowed to share our analytics data with + shiny::tags$p("Google is not allowed to share our analytics data with anyone."), - shiny::tags$p("Google Analytics stores anonymised information + shiny::tags$p("Google Analytics stores anonymised information about:"), - shiny::tags$li("How you got to this service"), - shiny::tags$li("The pages you visit on this service and how long you + shiny::tags$li("How you got to this service"), + shiny::tags$li("The pages you visit on this service and how long you spend on them"), - shiny::tags$li("How you interact with these pages"), - shinyGovstyle::govTable( - inputId = "ga_cookies_table", - df = data.frame( - Name = c("_ga", paste0("_ga_", google_analytics_key)), - Purpose = c("Used to distinguish users", "Used to persist + shiny::tags$li("How you interact with these pages"), + shinyGovstyle::govTable( + inputId = "ga_cookies_table", + df = data.frame( + Name = c("_ga", paste0("_ga_", google_analytics_key)), + Purpose = c("Used to distinguish users", "Used to persist session state"), - Expires = c("13 months", "13 months") - ), - caption = "", - caption_size = "s", - num_col = NULL, - width_overwrite = c("one-quarter", "one-quarter", "one-quarter") + Expires = c("13 months", "13 months") ), - shiny::br(), + caption = "", + caption_size = "s", + num_col = NULL, + width_overwrite = c("one-quarter", "one-quarter", "one-quarter") + ), + shiny::br(), + shiny::tags$div( + class = "govuk-grid-row", shiny::tags$div( - class = "govuk-grid-row", + class = "govuk-grid-column-two-thirds", + shiny::tags$h2( + class = "govuk-heading-l", + "Change your cookie settings" + ), + shiny::tags$div( + class = "govuk-form-group", + ), shiny::tags$div( - class = "govuk-grid-column-two-thirds", - shiny::tags$h2( - class = "govuk-heading-l", - "Change your cookie settings" - ), - shiny::tags$div( - class = "govuk-form-group", - ), - shiny::tags$div( - class = "govuk-form-group", - tags$fieldset( - class = "govuk-fieldset", - tags$legend( - class = "govuk-fieldset__legend govuk-fieldset__legend--s", - "Do you want to accept analytics cookies?" - ), + class = "govuk-form-group", + tags$fieldset( + class = "govuk-fieldset", + tags$legend( + class = "govuk-fieldset__legend govuk-fieldset__legend--s", + "Do you want to accept analytics cookies?" + ), + shiny::tags$div( + class = "govuk-radios", + `data-module` = "govuk-radios", shiny::tags$div( - class = "govuk-radios", - `data-module` = "govuk-radios", - shiny::tags$div( - class = "govuk-radios__item", - shiny::radioButtons(shiny::NS(id, "cookies_analytics"), - label = NULL, - choices = list("Yes" = "yes", "No" = "no"), - selected = "no", - inline = TRUE - ) + class = "govuk-radios__item", + shiny::radioButtons(shiny::NS(id, "cookies_analytics"), + label = NULL, + choices = list("Yes" = "yes", "No" = "no"), + selected = "no", + inline = TRUE ) ) ) - ), - shiny::actionButton(shiny::NS(id, "submit_btn"), - "Save cookie settings", - class = "govuk-button" ) + ), + shiny::actionButton(shiny::NS(id, "submit_btn"), + "Save cookie settings", + class = "govuk-button" ) ) ) ) ) ) - } + ) +} - #' cookies_panel_server - #' - #' @description - #' Create the server module of DfE R-Shiny cookies dashboard panel to be used - #' alongside cookies_panel_ui(). - #' - #' @param id ID shared with cookies_panel_ui() - #' @param input_cookies The cookie input passed from cookies.js (should always - #' be reactive(input$cookies)) - #' @param google_analytics_key Provide the GA 10 digit key of the form - #' "ABCDE12345" - #' - #' @export - #' - #' @examples - #' \dontrun{ - #' cookies_panel_server( - #' id = "cookies_panel", - #' input_cookies = reactive(input$cookies), - #' google_analytics_key = "ABCDE12345" - #' ) - #' } - cookies_panel_server <- function( - id, - input_cookies, - google_analytics_key = NULL) { - shiny::moduleServer(id, module = function(input, output, session) { - shiny::observeEvent(input_cookies(), { - if (!is.null(input_cookies())) { - if (!("dfe_analytics" %in% names(input_cookies()))) { +#' cookies_panel_server +#' +#' @description +#' Create the server module of DfE R-Shiny cookies dashboard panel to be used +#' alongside cookies_panel_ui(). +#' +#' @param id ID shared with cookies_panel_ui() +#' @param input_cookies The cookie input passed from cookies.js (should always +#' be reactive(input$cookies)) +#' @param google_analytics_key Provide the GA 10 digit key of the form +#' "ABCDE12345" +#' +#' @export +#' +#' @examples +#' \dontrun{ +#' cookies_panel_server( +#' id = "cookies_panel", +#' input_cookies = reactive(input$cookies), +#' google_analytics_key = "ABCDE12345" +#' ) +#' } +cookies_panel_server <- function(id, + input_cookies, + google_analytics_key = NULL) { + shiny::moduleServer(id, module = function(input, output, session) { + shiny::observeEvent(input_cookies(), { + if (!is.null(input_cookies())) { + if (!("dfe_analytics" %in% names(input_cookies()))) { + shiny::updateRadioButtons(session, "cookies_analytics", + selected = "no" + ) + } else { + if (input_cookies()$dfe_analytics == "denied") { shiny::updateRadioButtons(session, "cookies_analytics", selected = "no" ) - } else { - if (input_cookies()$dfe_analytics == "denied") { - shiny::updateRadioButtons(session, "cookies_analytics", - selected = "no" - ) - } else if (input_cookies()$dfe_analytics == "granted") { - shiny::updateRadioButtons(session, "cookies_analytics", - selected = "yes" - ) - } + } else if (input_cookies()$dfe_analytics == "granted") { + shiny::updateRadioButtons(session, "cookies_analytics", + selected = "yes" + ) } } - }) - - # Observe form submission button - shiny::observeEvent(input$submit_btn, { - # Update reactive values based on the selected radio buttons - if (input$cookies_analytics == "yes") { - msg <- list( - name = "dfe_analytics", - value = "granted" - ) - } else if (input$cookies_analytics == "no") { - msg <- list( - name = "dfe_analytics", - value = "denied" - ) - ga_msg <- list(name = paste0("_ga_", google_analytics_key)) - session$sendCustomMessage("cookie-clear", ga_msg) - } - session$sendCustomMessage("cookie-set", msg) - session$sendCustomMessage("analytics-consent", msg) - }) + } }) - } + # Observe form submission button + shiny::observeEvent(input$submit_btn, { + # Update reactive values based on the selected radio buttons + if (input$cookies_analytics == "yes") { + msg <- list( + name = "dfe_analytics", + value = "granted" + ) + } else if (input$cookies_analytics == "no") { + msg <- list( + name = "dfe_analytics", + value = "denied" + ) + ga_msg <- list(name = paste0("_ga_", google_analytics_key)) + session$sendCustomMessage("cookie-clear", ga_msg) + } + session$sendCustomMessage("cookie-set", msg) + session$sendCustomMessage("analytics-consent", msg) + }) + }) +} diff --git a/inst/google-analytics.hml b/inst/google-analytics.hml deleted file mode 100644 index f7343e8..0000000 --- a/inst/google-analytics.hml +++ /dev/null @@ -1,66 +0,0 @@ - - - - - From 41179f5107ce4fa353e26e4dcda7a563445f2142 Mon Sep 17 00:00:00 2001 From: Rich Bielby Date: Mon, 22 Jul 2024 12:59:08 +0100 Subject: [PATCH 27/34] Recommitting google analytics template sc --- inst/google-analytics.html | 66 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 inst/google-analytics.html diff --git a/inst/google-analytics.html b/inst/google-analytics.html new file mode 100644 index 0000000..f7343e8 --- /dev/null +++ b/inst/google-analytics.html @@ -0,0 +1,66 @@ + + + + + From 6fc3f9ac1eb1eef83820440e7a625d14d5b3a26e Mon Sep 17 00:00:00 2001 From: Rich Bielby Date: Mon, 22 Jul 2024 13:00:19 +0100 Subject: [PATCH 28/34] Updating .gitignore to only ignore analytics html if found in project root --- .gitignore | 2 +- google-analytics.html | 67 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 google-analytics.html diff --git a/.gitignore b/.gitignore index 234462f..20c460f 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,7 @@ .Rhistory .RData .Ruserdata -google-analytics.html +./google-analytics.html docs inst/doc www/ diff --git a/google-analytics.html b/google-analytics.html new file mode 100644 index 0000000..ee9b03f --- /dev/null +++ b/google-analytics.html @@ -0,0 +1,67 @@ + + + + + + From af6d8d9d38b4f3a44bb470b96b8990b2f404182d Mon Sep 17 00:00:00 2001 From: Rich Bielby Date: Mon, 22 Jul 2024 13:29:26 +0100 Subject: [PATCH 29/34] Few updates from analytics PR --- R/analytics.R | 11 +++---- google-analytics.html | 67 ----------------------------------------- man/cookies_panel_ui.Rd | 1 - man/init_analytics.Rd | 2 +- 4 files changed, 6 insertions(+), 75 deletions(-) delete mode 100644 google-analytics.html diff --git a/R/analytics.R b/R/analytics.R index ed498f8..29d6ede 100644 --- a/R/analytics.R +++ b/R/analytics.R @@ -47,11 +47,10 @@ init_analytics <- function(ga_code) { message("Analytics file already exists.") message("If you have any customisations in that file, make sure you've backed those up before over-writing.") - user_input <- stringr::str_trim( - readline( - prompt = "Are you happy to overwrite the existing analytics script (y/N) " - ) - ) + user_input <- readline( + prompt = "Are you happy to overwrite the existing analytics script (y/N) " + ) |> + stringr::str_trim() if (user_input %in% c("y", "Y")) { write_out <- TRUE } else { @@ -61,7 +60,7 @@ init_analytics <- function(ga_code) { write_out <- TRUE } if (write_out) { - cat(html_script, file = "google-analytics.html", sep = '\n') + cat(html_script, file = "google-analytics.html", sep = "\n") message("") message("Google analytics script created in google-analytics.html.") message("You'll need to add the following line to your ui.R script to start diff --git a/google-analytics.html b/google-analytics.html deleted file mode 100644 index ee9b03f..0000000 --- a/google-analytics.html +++ /dev/null @@ -1,67 +0,0 @@ - - - - - - diff --git a/man/cookies_panel_ui.Rd b/man/cookies_panel_ui.Rd index aabcb71..e609394 100644 --- a/man/cookies_panel_ui.Rd +++ b/man/cookies_panel_ui.Rd @@ -27,5 +27,4 @@ cookies_panel_ui( ) } - } diff --git a/man/init_analytics.Rd b/man/init_analytics.Rd index 37ed35d..fd38c97 100644 --- a/man/init_analytics.Rd +++ b/man/init_analytics.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/initialisers.R +% Please edit documentation in R/analytics.R \name{init_analytics} \alias{init_analytics} \title{init_analytics} From 5d253e3a0ca5d39e585b9e4d7569f67c93d6aefd Mon Sep 17 00:00:00 2001 From: Rich Bielby Date: Mon, 22 Jul 2024 14:25:48 +0100 Subject: [PATCH 30/34] Updting references to init_analytics(). --- README.md | 4 ++-- tests/testthat/google-analytics.html | 1 + tests/testthat/test-initialise_analytics.R | 8 ++++---- 3 files changed, 7 insertions(+), 6 deletions(-) create mode 100644 tests/testthat/google-analytics.html diff --git a/README.md b/README.md index fbbe80e..d042bfa 100644 --- a/README.md +++ b/README.md @@ -40,10 +40,10 @@ For analytics to function on your dashboard, you will need to: - create a html file with the javascript required for your dashboard to connect to Google Analytics - add the line: `tags$head(includeHTML(("google-analytics.html"))),` to the ui.R file. -To create the latter, we provide the function `dfeshiny::initialise_analytics()`. You should run this code from the R console providing your Google Analytics code as follows (replacing `ABCDE12345` with the code obtained from the Statistics Development team): +To create the latter, we provide the function `dfeshiny::init_analytics()`. You should run this code from the R console providing your Google Analytics code as follows (replacing `ABCDE12345` with the code obtained from the Statistics Development team): ``` -initialise_analytics("ABCDE12345") +init_analytics("ABCDE12345") ``` This will create the file [google-analytics.html](google-analytics.html) within the home directory of your R project. This html file can be edited to add customised analytics recorders for different shiny elements in your dashboard. Feel free to contact our team if you need support in adding additional functionality. diff --git a/tests/testthat/google-analytics.html b/tests/testthat/google-analytics.html new file mode 100644 index 0000000..929c05d --- /dev/null +++ b/tests/testthat/google-analytics.html @@ -0,0 +1 @@ +404: Not Found diff --git a/tests/testthat/test-initialise_analytics.R b/tests/testthat/test-initialise_analytics.R index 1fd61c3..52874b4 100644 --- a/tests/testthat/test-initialise_analytics.R +++ b/tests/testthat/test-initialise_analytics.R @@ -1,15 +1,15 @@ test_that("Blank code throws error", { - expect_error(initialise_analytics()) + expect_error(init_analytics()) }) test_that("Long code throws error", { - expect_error(initialise_analytics("G-qwertyuiop")) + expect_error(init_analytics("G-qwertyuiop")) }) test_that("Short code throws error", { - expect_error(initialise_analytics("qwerty")) + expect_error(init_analytics("qwerty")) }) test_that("Numeric throws error", { - expect_error(initialise_analytics(1234567890)) + expect_error(init_analytics(1234567890)) }) From 2d8ea4c0c982fb4c2ea643ee74845cdac10993eb Mon Sep 17 00:00:00 2001 From: Rich Bielby Date: Mon, 22 Jul 2024 14:31:38 +0100 Subject: [PATCH 31/34] Updating references from Stats Dev team to EESp team --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d042bfa..1ad1851 100644 --- a/README.md +++ b/README.md @@ -36,11 +36,11 @@ If this works, then you will need to look for where that "GITHUB_PAT" variable i For analytics to function on your dashboard, you will need to: -- request a Google Analytics key from the [Statistics Development Team](mailto:statistics.development@education.gov.uk) +- request a Google Analytics key from the [Explore Educaion Statisics platforms team](mailto:explore.statistics@education.gov.uk) - create a html file with the javascript required for your dashboard to connect to Google Analytics - add the line: `tags$head(includeHTML(("google-analytics.html"))),` to the ui.R file. -To create the latter, we provide the function `dfeshiny::init_analytics()`. You should run this code from the R console providing your Google Analytics code as follows (replacing `ABCDE12345` with the code obtained from the Statistics Development team): +To create the latter, we provide the function `dfeshiny::init_analytics()`. You should run this code from the R console providing your Google Analytics code as follows (replacing `ABCDE12345` with the code obtained from the [Explore Education Statistics platforms](explore.statistics@education.gov.uk) team): ``` init_analytics("ABCDE12345") From 00694a2916a5d2d74a1f96b715ce7c19b7e7b311 Mon Sep 17 00:00:00 2001 From: Rich Bielby Date: Mon, 22 Jul 2024 15:08:52 +0100 Subject: [PATCH 32/34] Updating reference filename for analytics --- R/analytics.R | 2 +- google-analytics.html | 67 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 google-analytics.html diff --git a/R/analytics.R b/R/analytics.R index 29d6ede..5dfff29 100644 --- a/R/analytics.R +++ b/R/analytics.R @@ -27,7 +27,7 @@ init_analytics <- function(ga_code) { webpage <- RCurl::getURL( paste0( github_area, - "dfeshiny/analytsics-init/inst/google-analytics.hml" + "dfeshiny/main/inst/google-analytics.html" ) ) tryCatch( diff --git a/google-analytics.html b/google-analytics.html new file mode 100644 index 0000000..fcbc98e --- /dev/null +++ b/google-analytics.html @@ -0,0 +1,67 @@ + + + + + + From 219e4bdb911a50d410c444739011976f22c0c224 Mon Sep 17 00:00:00 2001 From: Rich Bielby Date: Mon, 22 Jul 2024 15:14:33 +0100 Subject: [PATCH 33/34] removing google-analytics.html --- google-analytics.html | 67 ------------------------------------------- 1 file changed, 67 deletions(-) delete mode 100644 google-analytics.html diff --git a/google-analytics.html b/google-analytics.html deleted file mode 100644 index fcbc98e..0000000 --- a/google-analytics.html +++ /dev/null @@ -1,67 +0,0 @@ - - - - - - From 9cd99feb491c93f88d6191bef2fe2f66ef23bd18 Mon Sep 17 00:00:00 2001 From: Rich Bielby Date: Mon, 22 Jul 2024 15:17:53 +0100 Subject: [PATCH 34/34] updating analytics references in .gitignore --- .gitignore | 3 ++- tests/testthat/google-analytics.html | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) delete mode 100644 tests/testthat/google-analytics.html diff --git a/.gitignore b/.gitignore index 20c460f..7784746 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,8 @@ .Rhistory .RData .Ruserdata -./google-analytics.html +google-analytics.html +tests/testthat/google-analytics.html docs inst/doc www/ diff --git a/tests/testthat/google-analytics.html b/tests/testthat/google-analytics.html deleted file mode 100644 index 929c05d..0000000 --- a/tests/testthat/google-analytics.html +++ /dev/null @@ -1 +0,0 @@ -404: Not Found