Skip to content

Commit

Permalink
replace old ui and server elements for cookies with standardized dfes…
Browse files Browse the repository at this point in the history
…hiny cookies module
  • Loading branch information
oadetayo committed Dec 18, 2024
1 parent 0923377 commit 0e2b3f7
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 108 deletions.
4 changes: 2 additions & 2 deletions google-analytics.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
</script>

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src='https://www.googletagmanager.com/gtag/js?id=G-Z967JJVQQX'></script>
<script async src='https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX'></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}

gtag('js', new Date());

gtag('config', 'G-Z967JJVQQX');
gtag('config', 'G-XXXXXXXXXX');

/*
The custom trackers below can be tailored to match the inputs used in your
Expand Down
103 changes: 10 additions & 93 deletions server.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,99 +68,16 @@ server <- function(input, output, session) {
})

# Cookies logic -------------------------------------------------------------
observeEvent(input$cookies, {
if (!is.null(input$cookies)) {
if (!("dfe_analytics" %in% names(input$cookies))) {
shinyjs::show(id = "cookieMain")
} else {
shinyjs::hide(id = "cookieMain")
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-remove", ga_msg)
}
}
}
}
} else {
shinyjs::hide(id = "cookieMain")
}
})

# Need these set of observeEvent to create a path through the cookie banner
observeEvent(input$cookieAccept, {
msg <- list(
name = "dfe_analytics",
value = "granted"
)
session$sendCustomMessage("cookie-set", msg)
session$sendCustomMessage("analytics-consent", msg)
shinyjs::show(id = "cookieAcceptDiv")
shinyjs::hide(id = "cookieMain")
})

observeEvent(input$cookieReject, {
msg <- list(
name = "dfe_analytics",
value = "denied"
)
session$sendCustomMessage("cookie-set", msg)
session$sendCustomMessage("analytics-consent", msg)
shinyjs::show(id = "cookieRejectDiv")
shinyjs::hide(id = "cookieMain")
})

observeEvent(input$hideAccept, {
shinyjs::toggle(id = "cookieDiv")
})

observeEvent(input$hideReject, {
shinyjs::toggle(id = "cookieDiv")
})

observeEvent(input$remove, {
shinyjs::toggle(id = "cookieMain")
msg <- list(name = "dfe_analytics", value = "denied")
session$sendCustomMessage("cookie-remove", msg)
session$sendCustomMessage("analytics-consent", msg)
print(input$cookies)
})

cookies_data <- reactive({
input$cookies
})

output$cookie_status <- renderText({
cookie_text_stem <- "To better understand the reach of our dashboard tools,
this site uses cookies to identify numbers of unique users as part of Google
Analytics. You have chosen to"
cookie_text_tail <- "the use of cookies on this website."
if ("cookies" %in% names(input)) {
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."
}
})

observeEvent(input$cookieLink, {
# Need to link here to where further info is located. You can
# updateTabsetPanel to have a cookie page for instance
updateTabsetPanel(session, "navlistPanel",
selected = "Support and feedback"
)
})
output$cookies_status <- dfeshiny::cookies_banner_server(
input_cookies = shiny::reactive(input$cookies),
parent_session = session,
google_analytics_key = google_analytics_key
)

Check notice

Code scanning / lintr

Trailing whitespace is superfluous. Note

Trailing whitespace is superfluous.
dfeshiny::cookies_panel_server(
input_cookies = shiny::reactive(input$cookies),
google_analytics_key = google_analytics_key
)

# Dataset with timeseries data ----------------------------------------------
reactive_rev_bal <- reactive({
Expand Down
18 changes: 8 additions & 10 deletions ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,10 @@ ui <- function(input, output, session) {

# Cookies -----------------------------------------------------------------
# Setting up cookie consent based on a cookie recording the consent:
# https://book.javascript-for-r.com/shiny-cookies.html
tags$head(
tags$script(
src = paste0(
"https://cdn.jsdelivr.net/npm/js-cookie@rc/",
"dist/js.cookie.min.js"
)
),
tags$script(src = "cookie-consent.js")
dfeshiny::dfe_cookies_script(),
dfeshiny::cookies_banner_ui(
name = "Department for Education (DfE) Shiny Template"
),
shinyGovstyle::cookieBanner("Department for Education (DfE) R-Shiny dashboard template"),

# Google analytics --------------------------------------------------------
tags$head(includeHTML(("google-analytics.html"))),
Expand Down Expand Up @@ -108,6 +101,11 @@ ui <- function(input, output, session) {
example_tab_1_panel(),
user_guide_panel(),
a11y_panel(),
shiny::tabPanel(
value = "cookies_panel_ui",
"Cookies",
cookies_panel_ui(google_analytics_key = google_analytics_key)
),
support_panel(
team_email = "[email protected]",
repo_name = "https://github.com/dfe-analytical-services/shiny-template",
Expand Down
4 changes: 1 addition & 3 deletions www/cookie-consent.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Shiny.addCustomMessageHandler('cookie-set', function(msg){
getCookies();
})

Shiny.addCustomMessageHandler('cookie-remove', function(msg){
Shiny.addCustomMessageHandler('cookie-clear', function(msg){
Cookies.remove(msg.name);
getCookies();
})
Expand All @@ -22,5 +22,3 @@ Shiny.addCustomMessageHandler('analytics-consent', function(msg){
'analytics_storage': msg.value
});
})


0 comments on commit 0e2b3f7

Please sign in to comment.