-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cookie module #19
Merged
Merged
Cookie module #19
Changes from all commits
Commits
Show all changes
45 commits
Select commit
Hold shift + click to select a range
f573459
Switching in cookie module code developed within the template
rmbielby 2766a15
Documentation update with cookie module
rmbielby e2b5686
Clarifying some namespace issues
rmbielby ab2f6c6
Revert "Clarifying some namespace issues"
rmbielby 21d5b6d
Updated cookie documentation
rmbielby 70172f4
Adding cookie header script
rmbielby f70c63f
Adding cookie header script to docs
rmbielby 1650f74
Renaming cookie script function
rmbielby 4c30abe
Added cookie js script
rmbielby ea4b53d
Updated cookie script to point to remote consent script
rmbielby 1cdc9f7
Making the references to dfe_cookie_script consistent
rmbielby 2050c47
Rolling back to the local cookie-consent.js
rmbielby 2b7c422
Removing some print() statements
rmbielby 02c068d
Commenting in cookies.R
rmbielby b402b7b
Reverting to gov style button inputs.
rmbielby ed99f80
Updated styling of the cookies link in the banner
rmbielby fb05a29
Adjusting how the cookies link button is rendered
rmbielby d5f529d
Switched cookie link to actionLink instead of a button
rmbielby 93c7741
Updating support and feedback id
rmbielby 3ee22bb
Adjusing namespacing for updateTabsetPanel
rmbielby 5dc3f9a
Updated cookie server function to pull in parent session
rmbielby a0fdc5c
Updated cookie module server to return cookie status text
rmbielby 47bab1b
Updated references for input in cookie module
rmbielby d123654
Adjusting cookie status logic
rmbielby 02eca50
Adding in the brackets for the reactives!
rmbielby 2085897
Expanded description of how to use the cookie banner
rmbielby 1c65605
Testing out defining parent package for functions to stop lintr compl…
rmbielby 34f610c
Testing out defining parent package for functions to stop lintr compl…
rmbielby 458a96f
Adding in the google analytics key!
rmbielby b3bbce4
A few styling changes to the cookie code based on lintr
rmbielby b3bcde1
Code styling clean up
rmbielby e2583f6
Updated readme with instructions on using the cookie functionality
rmbielby af0301a
Updating title hierarchy in readme
rmbielby 0be14f9
Adding links to template examples
rmbielby d9c2016
Adding link to template global.R example
rmbielby 4dbbace
Added shinytest2 tests and changed the language on the remove consent…
rmbielby 35aa82e
Updated links to example dashboard code to the dfeshiny test dashboard
rmbielby 12c1d5a
Updated links to example ui code to the dfeshiny test ui.R
rmbielby c9d790b
Little bit of code styling
rmbielby 255c058
Adding shinytest2 and diffviewer to suggests
rmbielby 8efeaea
Missed some commas in the description file
rmbielby 255407e
Added NEWS.md with overview of updates intended for v0.2.0.
rmbielby 50edb7f
Merge branch 'main' into cookie-module
rmbielby 7de0416
Expanded NEWS.md to include testing improvements
rmbielby a3dc1c3
Merge branch 'cookie-module' of https://github.com/dfe-analytical-ser…
rmbielby File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
# Generated by roxygen2: do not edit by hand | ||
|
||
export(cookie_banner_server) | ||
export(cookie_banner_ui) | ||
export(dfe_cookie_script) | ||
export(support_panel) | ||
export(tidy_code) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# dfeshiny 0.2.0 | ||
|
||
## New features | ||
|
||
* 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. | ||
|
||
## Improvements | ||
|
||
* Implemented basic unit testing (currently just on e-mail validation) and UI | ||
testing using a test dashboard; | ||
|
||
* Instructions within the package README.md on how to install `dfeshiny` have | ||
been updated. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,236 @@ | ||
#' dfe_cookie_script | ||
#' | ||
#' @return shiny::tags$head() | ||
#' @export | ||
#' | ||
#' @examples | ||
#' dfe_cookie_script() | ||
dfe_cookie_script <- function() { | ||
shiny::tags$head( | ||
shiny::tags$script( | ||
src = paste0( | ||
"https://cdn.jsdelivr.net/npm/js-cookie@rc/", | ||
"dist/js.cookie.min.js" | ||
) | ||
), | ||
shiny::tags$script(src = "cookie-consent.js") # Tried using a copy in the | ||
# repo here, but it all stopped working at that point. | ||
) | ||
} | ||
|
||
#' cookie_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. | ||
#' | ||
#' @param id Shiny tag shared with cookie_banner_server() | ||
#' @param name Name of the dashboard on which the cookie authorisation is being | ||
#' applied | ||
#' | ||
#' @return shiny::tags$div() | ||
#' @export | ||
#' | ||
#' @examples | ||
#' To use the banner, first copy the file cookie-consent.js from the js/ folder | ||
#' of this package to the www/ folder of your R-Shiny app. Then add the | ||
#' following lines to your ui.R: | ||
#' dfe_cookie_script(), | ||
#' cookie_banner_ui("cookies", name = "My DfE R-Shiny data dashboard"), | ||
#' | ||
#' And add the following in server.R (after updating the google analytics key): | ||
#' output$cookie_status <- dfeshiny::cookie_banner_server( | ||
#' "cookies", | ||
#' input_cookies = reactive(input$cookies), | ||
#' input_clear = reactive(input$cookie_consent_clear), | ||
#' parent_session = session, | ||
#' google_analytics_key = "ABCDE12345" | ||
#' ) | ||
#' | ||
#' Note that you should also include dfeshiny::support_panel() in your | ||
#' navListPanel() | ||
#' | ||
cookie_banner_ui <- function(id, name = "DfE R-Shiny dashboard template") { | ||
shiny::tags$div( | ||
id = shiny::NS(id, "cookie_div"), | ||
class = "govuk-cookie-banner", | ||
`data-nosnippet role` = "region", | ||
`aria-label` = "Cookies on name", | ||
shiny::tags$div( | ||
id = shiny::NS(id, "cookie_main"), | ||
class = "govuk-cookie-banner__message govuk-width-container", | ||
shiny::tags$div( | ||
class = "govuk-grid-row", | ||
shiny::tags$div( | ||
class = "govuk-grid-column-two-thirds", | ||
shiny::tags$h2( | ||
class = "govuk-cookie-banner__heading govuk-heading-m", | ||
name | ||
), | ||
shiny::tags$div( | ||
class = "govuk-cookie-banner__content", | ||
shiny::tags$p( | ||
class = "govuk-body", | ||
"We use some essential cookies to make this service work." | ||
), | ||
shiny::tags$p( | ||
class = "govuk-body", | ||
"We'd also like to use analytics cookies so we can understand | ||
how you use the service and make improvements." | ||
) | ||
) | ||
) | ||
), | ||
shiny::tags$div( | ||
class = "govuk-button-group", | ||
shinyGovstyle::button_Input( | ||
shiny::NS(id, "cookie_accept"), | ||
"Accept analytics cookies" | ||
), | ||
shinyGovstyle::button_Input( | ||
shiny::NS(id, "cookie_reject"), | ||
"Reject analytics cookies" | ||
), | ||
shiny::actionLink( | ||
shiny::NS(id, "cookie_link"), | ||
"View cookie information" | ||
) | ||
) | ||
) | ||
) | ||
} | ||
|
||
#' cookie_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 | ||
#' 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() | ||
#' @param input_cookies The cookie input passed from cookies.js (should always | ||
#' be reactive(input$cookies)) | ||
#' @param input_clear The state of the cookie reset button provided by | ||
#' dfeshiny::support_panel(). Should always be set to reactive(input$cookie_consent_clear). | ||
Check notice Code scanning / lintr Lines should not be more than 80 characters. This line is 91 characters. Note
Lines should not be more than 80 characters. This line is 91 characters.
|
||
#' @param parent_session This should be the R Shiny app session | ||
#' @param google_analytics_key Provide the GA 10 digit key of the form | ||
#' "ABCDE12345" | ||
#' | ||
#' @return NULL | ||
#' @export | ||
#' | ||
#' @examples | ||
#' To use the banner, first copy the file cookie-consent.js from the js/ folder | ||
#' of this package to the www/ folder of your R-Shiny app. Then add the | ||
#' following lines to your ui.R: | ||
#' dfe_cookie_script(), | ||
#' cookie_banner_ui("cookies", name = "My DfE R-Shiny data dashboard"), | ||
#' | ||
#' And add the following in server.R (after updating the google analytics key): | ||
#' output$cookie_status <- dfeshiny::cookie_banner_server( | ||
#' "cookies", | ||
#' input_cookies = reactive(input$cookies), | ||
#' input_clear = reactive(input$cookie_consent_clear), | ||
#' parent_session = session, | ||
#' google_analytics_key = "ABCDE12345" | ||
#' ) | ||
#' | ||
#' Note that you should also include dfeshiny::support_panel() in your | ||
#' navListPanel() | ||
#' | ||
cookie_banner_server <- function( | ||
id, | ||
input_cookies, | ||
input_clear, | ||
parent_session, | ||
google_analytics_key = NULL) { | ||
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") | ||
} else { | ||
shinyjs::hide(id = "cookie_main") | ||
msg <- list( | ||
name = "dfe_analytics", | ||
value = input_cookies()$dfe_analytics | ||
) | ||
session$sendCustomMessage("analytics-consent", msg) | ||
if ("cookies" %in% names(input)) { | ||
if ("dfe_analytics" %in% names(input_cookies())) { | ||
if (input_cookies()$dfe_analytics == "denied") { | ||
ga_msg <- list(name = paste0("_ga_", google_analytics_key)) | ||
session$sendCustomMessage("cookie-clear", ga_msg) | ||
} | ||
} | ||
} | ||
} | ||
} else { | ||
shinyjs::hide(id = "cookie_main", asis = TRUE) | ||
shinyjs::toggle(id = "cookie_div", asis = TRUE) | ||
} | ||
}) | ||
|
||
# Check for the cookies being authorised | ||
shiny::observeEvent(input$cookie_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) | ||
}) | ||
|
||
# Check for the cookies being rejected | ||
shiny::observeEvent(input$cookie_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) | ||
}) | ||
|
||
shiny::observeEvent(input$cookie_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 = "support_panel" | ||
) | ||
}) | ||
|
||
shiny::observeEvent(input_clear(), { | ||
shinyjs::toggle(id = "cookie_main") | ||
msg <- list(name = "dfe_analytics", value = "denied") | ||
session$sendCustomMessage("cookie-clear", msg) | ||
session$sendCustomMessage("analytics-consent", msg) | ||
}) | ||
|
||
return(shiny::renderText({ | ||
cookie_text_stem <- "You have chosen to" | ||
cookie_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) | ||
} else { | ||
paste(cookie_text_stem, "reject", cookie_text_tail) | ||
} | ||
} | ||
} else { | ||
"Cookies consent has not been confirmed." | ||
} | ||
})) | ||
}) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This may need changing as I've added a couple of other bits of validation, but can do in a separate mop up before 0.2.0 is formally released.