Skip to content

Commit

Permalink
update cookie to cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
cjrace committed Jul 30, 2024
1 parent c7ee853 commit e8d87a5
Show file tree
Hide file tree
Showing 26 changed files with 135 additions and 137 deletions.
6 changes: 3 additions & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Generated by roxygen2: do not edit by hand

export(cookie_banner_server)
export(cookie_banner_ui)
export(cookies_banner_server)
export(cookies_banner_ui)
export(cookies_panel_server)
export(cookies_panel_ui)
export(custom_disconnect_message)
export(dfe_cookie_script)
export(dfe_cookies_script)
export(init_analytics)
export(init_cookies)
export(support_panel)
Expand Down
4 changes: 2 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ app.

* A module is now provided to produce a standardised cookie consent banner and
implement the associated functionality. The server part is
`cookie_banner_server()` and the ui part is `cookie_banner_ui()`. In addition,
`dfe_cookie_script()` is provided to implement the necessary javascript.
`cookies_banner_server()` and the ui part is `cookies_banner_ui()`. In addition,
`dfe_cookies_script()` is provided to implement the necessary javascript.

## Improvements

Expand Down
78 changes: 39 additions & 39 deletions R/cookies.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' dfe_cookie_script
#' dfe_cookies_script
#'
#' Calls in JavaScript dependencies to the shiny app used to set and unset the
#' cookies. Function should be placed in the ui.R script.
Expand All @@ -10,7 +10,7 @@
#' @family cookies
#' @examples
#' if (interactive()) {
#' # This example shows how to use the full family of cookie functions together
#' # This example shows how to use the full family of cookies functions together
#' # This will be in your global.R script =====================================
#'
#' library(shiny)
Expand All @@ -23,8 +23,8 @@
#' ui <- fluidPage(
#' # Place these lines above your header ------------------------------------
#' useShinyjs(),
#' dfe_cookie_script(),
#' cookie_banner_ui(name = "My DfE R-Shiny data dashboard"),
#' dfe_cookies_script(),
#' cookies_banner_ui(name = "My DfE R-Shiny data dashboard"),
#'
#' # Place the cookies panel under the header but in the main content -------
#' cookies_panel_ui(google_analytics_key = google_analytics_key)
Expand All @@ -34,7 +34,7 @@
#'
#' server <- function(input, output, session) {
#' # Server logic for the pop up banner, can be placed anywhere in server.R -
#' output$cookie_status <- dfeshiny::cookie_banner_server(
#' output$cookies_status <- dfeshiny::cookies_banner_server(
#' input_cookies = reactive(input$cookies),
#' google_analytics_key = google_analytics_key,
#' parent_session = session
Expand All @@ -50,7 +50,7 @@
#' # How to run the minimal app given in this example =========================
#' shinyApp(ui, server)
#' }
dfe_cookie_script <- function() {
dfe_cookies_script <- function() {
shiny::tags$head(
shiny::tags$script(
src = paste0(
Expand All @@ -63,31 +63,31 @@ dfe_cookie_script <- function() {
)
}

#' cookie_banner_ui
#' cookies_banner_ui
#'
#' @description
#' This function provides a cookie authorisation banner on DfE R-Shiny
#' dashboards for users to be able to accept or reject cookies. The server side
#' functionality is provided by cookie_banner_server(), whilst users will also
#' need to include the dfe_cookie_script() function in their ui.R file.
#' functionality is provided by cookies_banner_server(), whilst users will also
#' need to include the dfe_cookies_script() function in their ui.R file.
#'
#' @param id Shiny tag shared with cookie_banner_server(), can be any string set
#' by the user as long as it matches the id in the cookie_banner_server()
#' @param id Shiny tag shared with cookies_banner_server(), can be any string set
#' by the user as long as it matches the id in the cookies_banner_server()
#' @param name Name of the dashboard on which the cookie authorisation is being
#' applied
#'
#' @family cookies
#' @return shiny::tags$div()
#' @export
#' @inherit cookies examples
cookie_banner_ui <- function(id = "cookies_banner", name = "DfE R-Shiny dashboard template") {
cookies_banner_ui <- function(id = "cookies_banner", name = "DfE R-Shiny dashboard template") {
shiny::tags$div(
id = shiny::NS(id, "cookie_div"),
id = shiny::NS(id, "cookies_div"),
class = "govuk-cookie-banner",
`data-nosnippet role` = "region",
`aria-label` = "Cookies on name",
shiny::tags$div(
id = shiny::NS(id, "cookie_main"),
id = shiny::NS(id, "cookies_main"),
class = "govuk-cookie-banner__message govuk-width-container",
shiny::tags$div(
class = "govuk-grid-row",
Expand All @@ -114,63 +114,63 @@ cookie_banner_ui <- function(id = "cookies_banner", name = "DfE R-Shiny dashboar
shiny::tags$div(
class = "govuk-button-group",
shinyGovstyle::button_Input(
shiny::NS(id, "cookie_accept"),
shiny::NS(id, "cookies_accept"),
"Accept analytics cookies"
),
shinyGovstyle::button_Input(
shiny::NS(id, "cookie_reject"),
shiny::NS(id, "cookies_reject"),
"Reject analytics cookies"
),
shiny::actionLink(
shiny::NS(id, "cookie_link"),
shiny::NS(id, "cookies_link"),
"View cookie information"
)
)
)
)
}

#' cookie_banner_server
#' cookies_banner_server
#'
#' @description
#' cookie_banner_server() provides the server module to be used alongside
#' cookie_banner_ui(). Place cookie_banner_server() as a call in your server.R
#' cookies_banner_server() provides the server module to be used alongside
#' cookies_banner_ui(). Place cookies_banner_server() as a call in your server.R
#' file to provide the server functions to control users being able to accept or
#' reject cookie consent for the provision of Google Analytics tracking on DfE
#' R-Shiny dashboards.
#'
#' @param id Shiny tag shared with cookie_banner_ui(), can be any string set by
#' the user as long as it matches the id in the cookie_banner_ui()
#' @param id Shiny tag shared with cookies_banner_ui(), can be any string set by
#' the user as long as it matches the id in the cookies_banner_ui()
#' @param input_cookies The cookie input passed from cookies.js (should always
#' be `reactive(input$cookies)`)
#' @param parent_session This should be the R Shiny app session, expect it to
#' always be `parent_session = session`
#' @param google_analytics_key Provide the GA 10 digit key of the form
#' "ABCDE12345"
#' @param cookie_link_panel name of the navlistPanel that the cookie banner
#' @param cookies_link_panel name of the navlistPanel that the cookie banner
#' provides a link to, usually "cookies_panel_ui"
#'
#' @family cookies
#' @return NULL
#' @export
#'
#' @inherit cookies examples
cookie_banner_server <- function(
cookies_banner_server <- function(
id = "cookies_banner",
input_cookies,
parent_session,
google_analytics_key = NULL,
cookie_link_panel = "cookies_panel_ui") {
cookies_link_panel = "cookies_panel_ui") {
shiny::moduleServer(id, function(input, output, session) {
if (is.null(google_analytics_key)) {
warning("Please provide a valid Google Analytics key")
}
shiny::observeEvent(input_cookies(), {
if (!is.null(input_cookies())) {
if (!("dfe_analytics" %in% names(input_cookies()))) {
shinyjs::show(id = "cookie_main")
shinyjs::show(id = "cookies_main")
} else {
shinyjs::hide(id = "cookie_main")
shinyjs::hide(id = "cookies_main")
msg <- list(
name = "dfe_analytics",
value = input_cookies()$dfe_analytics
Expand All @@ -186,52 +186,52 @@ cookie_banner_server <- function(
}
}
} else {
shinyjs::hide(id = "cookie_main", asis = TRUE)
shinyjs::toggle(id = "cookie_div", asis = TRUE)
shinyjs::hide(id = "cookies_main", asis = TRUE)
shinyjs::toggle(id = "cookies_div", asis = TRUE)
}
})

# Check for the cookies being authorised
shiny::observeEvent(input$cookie_accept, {
shiny::observeEvent(input$cookies_accept, {
msg <- list(
name = "dfe_analytics",
value = "granted"
)
session$sendCustomMessage("cookie-set", msg)
session$sendCustomMessage("analytics-consent", msg)
shinyjs::hide(id = "cookie_main", asis = TRUE)
shinyjs::hide(id = "cookies_main", asis = TRUE)
})

# Check for the cookies being rejected
shiny::observeEvent(input$cookie_reject, {
shiny::observeEvent(input$cookies_reject, {
msg <- list(
name = "dfe_analytics",
value = "denied"
)
session$sendCustomMessage("cookie-set", msg)
session$sendCustomMessage("analytics-consent", msg)
shinyjs::hide(id = "cookie_main", asis = TRUE)
shinyjs::hide(id = "cookies_main", asis = TRUE)
})

shiny::observeEvent(input$cookie_link, {
shiny::observeEvent(input$cookies_link, {
# Need to link here to where further info is located. You can
# updateTabsetPanel to have a cookie page for instance
shiny::updateTabsetPanel(
session = parent_session,
"navlistPanel",
selected = cookie_link_panel
selected = cookies_link_panel
)
})

return(shiny::renderText({
cookie_text_stem <- "You have chosen to"
cookie_text_tail <- "the use of cookies on this website."
cookies_text_stem <- "You have chosen to"
cookies_text_tail <- "the use of cookies on this website."
if (!is.null(input_cookies())) {
if ("dfe_analytics" %in% names(input_cookies())) {
if (input_cookies()$dfe_analytics == "granted") {
paste(cookie_text_stem, "accept", cookie_text_tail)
paste(cookies_text_stem, "accept", cookies_text_tail)
} else {
paste(cookie_text_stem, "reject", cookie_text_tail)
paste(cookies_text_stem, "reject", cookies_text_tail)
}
}
} else {
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,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 [Explore Educaion Statisics platforms team](mailto:[email protected])
- request a Google Analytics key from the [explore education statisics platforms team](mailto:[email protected])
- 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 [Explore Education Statistics platforms]([email protected]) 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]([email protected]) team):

```
init_analytics("ABCDE12345")
Expand Down
4 changes: 1 addition & 3 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ reference:
- tidy_code
- title: Cookies
contents:
- starts_with("cookie")
- dfe_cookie_script
- has_concept("cookies")
- title: Standard panels
contents:
- support_panel
Expand All @@ -23,4 +22,3 @@ reference:
desc: One time functions used to set up or update standardised scripts and workflows needed for your dashboard
contents:
- starts_with("init")

18 changes: 9 additions & 9 deletions man/cookies.Rd

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

Loading

0 comments on commit e8d87a5

Please sign in to comment.