Skip to content
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

Function to initialise google analytics html script #4

Merged
merged 37 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
6260d78
Added initialise google analytics function.
rmbielby Nov 8, 2023
23568ef
Added extra documentation info
rmbielby Nov 8, 2023
771a116
Resolve merge conflicts from main
rmbielby Mar 13, 2024
f64f95c
Added validation on the ga_code variable for the google analytics scr…
rmbielby Mar 13, 2024
cebb5bd
Added tests for initialise_analytics
rmbielby Mar 13, 2024
a9790e1
Updated documentation
rmbielby Mar 13, 2024
5bfede7
Added magrittr pipe load
rmbielby Mar 13, 2024
8dc456d
Added magrittr DESCRIPTION
rmbielby Mar 13, 2024
7cd6557
Added source package for str_trim
rmbielby Mar 13, 2024
bd7d0c4
Removed yml code that had crept in from another branch
rmbielby Mar 13, 2024
4906b65
Cleaning up documentation
rmbielby Mar 13, 2024
4467b91
Adding test r script to tidy code exclude list
rmbielby Mar 13, 2024
63ecfa0
Adding info into README on analytics initialiser
rmbielby Mar 27, 2024
b09328d
Cleaning up line lengths in initialisers
rmbielby Mar 27, 2024
f46e7e6
Cleaning up line lengths in tidy code
rmbielby Mar 27, 2024
5a45bf8
Updated installation guidance in readme
rmbielby Mar 27, 2024
a7c4d5b
Updated installation guidance in readme
rmbielby Mar 27, 2024
1bb8e70
Adjusting line lengths in analyics function
rmbielby Mar 27, 2024
84a3721
Adjusted messaging spacing in initialiser script
rmbielby Mar 27, 2024
07d1bb2
Tweaks to initialisers code
rmbielby Mar 27, 2024
e9e0d8f
Removed google analyics html
rmbielby Mar 27, 2024
1450aec
Merge branch 'main' into analytsics-init
rmbielby Mar 27, 2024
a212e57
Merge branch 'main' into analytsics-init
rmbielby Jul 22, 2024
f1343d5
Reconfigured analytics initialiser to pull template script from GitHub
rmbielby Jul 22, 2024
0a754ab
Rebuilt package after init_analytics updates
rmbielby Jul 22, 2024
4ee5c8c
Few minor updates around analytics initialisation.
rmbielby Jul 22, 2024
117cf35
Removed google-analytics.html
rmbielby Jul 22, 2024
39a7715
Corrected reference to google-analytics.html in .gitignore
rmbielby Jul 22, 2024
15479bc
Handful of PR change requests on analytics initiailser
rmbielby Jul 22, 2024
41179f5
Recommitting google analytics template sc
rmbielby Jul 22, 2024
6fc3f9a
Updating .gitignore to only ignore analytics html if found in project…
rmbielby Jul 22, 2024
af6d8d9
Few updates from analytics PR
rmbielby Jul 22, 2024
5d253e3
Updting references to init_analytics().
rmbielby Jul 22, 2024
2d8ea4c
Updating references from Stats Dev team to EESp team
rmbielby Jul 22, 2024
00694a2
Updating reference filename for analytics
rmbielby Jul 22, 2024
219e4bd
removing google-analytics.html
rmbielby Jul 22, 2024
9cd99fe
updating analytics references in .gitignore
rmbielby Jul 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.Rhistory
.RData
.Ruserdata
./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) "
) |>

Check notice

Code scanning / lintr

Indentation should be 4 spaces but is 6 spaces. Note

Indentation should be 4 spaces but is 6 spaces.
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
Loading