Skip to contents

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.

Usage

cookies_banner_server(
  id = "cookies_banner",
  input_cookies,
  parent_session,
  google_analytics_key = NULL,
  cookies_link_panel = "cookies_panel_ui"
)

Arguments

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()

input_cookies

The cookie input passed from cookies.js (should always be reactive(input$cookies))

parent_session

This should be the R Shiny app session, expect it to always be parent_session = session

google_analytics_key

Provide the GA 10 digit key of the form "ABCDE12345"

name of the navlistPanel that the cookie banner provides a link to, usually "cookies_panel_ui"

Examples

if (interactive()) {
  # This example shows how to use the full family of cookies functions together
  # This will be in your global.R script ===================================

  library(shiny)
  library(shinyjs)
  library(dfeshiny)
  google_analytics_key <- "ABCDE12345"

  # This will be what is in your ui.R script ===============================

  ui <- fluidPage(
    # Place these lines above your header ----------------------------------
    useShinyjs(),
    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 -----
    # Example of placing as a panel within navlistPanel
    shiny::navlistPanel(
      "",
      id = "navlistPanel",
      widths = c(2, 8),
      well = FALSE,
      ## Cookies panel -----------------------------------------------------
      shiny::tabPanel(
        value = "cookies_panel_ui",
        "Cookies",
        cookies_panel_ui(google_analytics_key = google_analytics_key)
      )
    )
  )

  # This will be in your server.R file =====================================

  server <- function(input, output, session) {
    # Server logic for the pop up banner, can be placed anywhere in server.R -
    output$cookies_status <- dfeshiny::cookies_banner_server(
      input_cookies = reactive(input$cookies),
      google_analytics_key = google_analytics_key,
      parent_session = session
    )

    # Server logic for the panel, can be placed anywhere in server.R -------
    cookies_panel_server(
      input_cookies = reactive(input$cookies),
      google_analytics_key = google_analytics_key
    )
  }

  # How to run the minimal app given in this example =======================
  shinyApp(ui, server)
}