Skip to content

Commit

Permalink
Merge pull request #4 from dfe-analytical-services/analytsics-init
Browse files Browse the repository at this point in the history
Function to initialise google analytics html script
  • Loading branch information
rmbielby authored Jul 22, 2024
2 parents b19f36f + 9cd99fe commit 688c866
Show file tree
Hide file tree
Showing 12 changed files with 377 additions and 168 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
.Rhistory
.RData
.Ruserdata
google-analytics.html
tests/testthat/google-analytics.html
docs
inst/doc
www/
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Description: R package containing preferred methods for creating official
DfE R-Shiny dashboards.
License: GPL (>= 3)
Imports:
magrittr,
checkmate,
glue,
htmltools,
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ export(cookies_panel_server)
export(cookies_panel_ui)
export(custom_disconnect_message)
export(dfe_cookie_script)
export(init_analytics)
export(init_cookies)
export(support_panel)
export(tidy_code)
importFrom(htmltools,tagList)
importFrom(htmltools,tags)
importFrom(magrittr,"%>%")
72 changes: 72 additions & 0 deletions R/analytics.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#' 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 %>%
#' @return TRUE if written, FALSE if not
#' @export
#'
#' @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"
}

if (is_valid_ga4_code(ga_code) == FALSE) {
stop(
'You have entered an invalid GA4 code in the ga_code argument.
Please enter a 10 digit code as a character string.
e.g. "0123QWERTY"'
)
}

github_area <- "https://raw.githubusercontent.com/dfe-analytical-services/"
webpage <- RCurl::getURL(
paste0(
github_area,
"dfeshiny/main/inst/google-analytics.html"
)
)
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.")
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 {
write_out <- FALSE
}
} else {
write_out <- TRUE
}
if (write_out) {
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
recording analytics:")
message('tags$head(includeHTML(("google-analytics.html"))),')
} else {
message("Original Google analytics html script left in place.")
}
}
Loading

0 comments on commit 688c866

Please sign in to comment.