-
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
Function to initialise google analytics html script #4
Merged
Merged
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 23568ef
Added extra documentation info
rmbielby 771a116
Resolve merge conflicts from main
rmbielby f64f95c
Added validation on the ga_code variable for the google analytics scr…
rmbielby cebb5bd
Added tests for initialise_analytics
rmbielby a9790e1
Updated documentation
rmbielby 5bfede7
Added magrittr pipe load
rmbielby 8dc456d
Added magrittr DESCRIPTION
rmbielby 7cd6557
Added source package for str_trim
rmbielby bd7d0c4
Removed yml code that had crept in from another branch
rmbielby 4906b65
Cleaning up documentation
rmbielby 4467b91
Adding test r script to tidy code exclude list
rmbielby 63ecfa0
Adding info into README on analytics initialiser
rmbielby b09328d
Cleaning up line lengths in initialisers
rmbielby f46e7e6
Cleaning up line lengths in tidy code
rmbielby 5a45bf8
Updated installation guidance in readme
rmbielby a7c4d5b
Updated installation guidance in readme
rmbielby 1bb8e70
Adjusting line lengths in analyics function
rmbielby 84a3721
Adjusted messaging spacing in initialiser script
rmbielby 07d1bb2
Tweaks to initialisers code
rmbielby e9e0d8f
Removed google analyics html
rmbielby 1450aec
Merge branch 'main' into analytsics-init
rmbielby a212e57
Merge branch 'main' into analytsics-init
rmbielby f1343d5
Reconfigured analytics initialiser to pull template script from GitHub
rmbielby 0a754ab
Rebuilt package after init_analytics updates
rmbielby 4ee5c8c
Few minor updates around analytics initialisation.
rmbielby 117cf35
Removed google-analytics.html
rmbielby 39a7715
Corrected reference to google-analytics.html in .gitignore
rmbielby 15479bc
Handful of PR change requests on analytics initiailser
rmbielby 41179f5
Recommitting google analytics template sc
rmbielby 6fc3f9a
Updating .gitignore to only ignore analytics html if found in project…
rmbielby af6d8d9
Few updates from analytics PR
rmbielby 5d253e3
Updting references to init_analytics().
rmbielby 2d8ea4c
Updating references from Stats Dev team to EESp team
rmbielby 00694a2
Updating reference filename for analytics
rmbielby 219e4bd
removing google-analytics.html
rmbielby 9cd99fe
updating analytics references in .gitignore
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
.Rhistory | ||
.RData | ||
.Ruserdata | ||
./google-analytics.html | ||
docs | ||
inst/doc | ||
www/ |
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
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,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.") | ||
} | ||
} |
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.
Check notice
Code scanning / lintr
Indentation should be 4 spaces but is 6 spaces. Note