From 2bd518f3c54a34fe95a4e96ff96d9b8b3bae2837 Mon Sep 17 00:00:00 2001 From: cansavvy Date: Mon, 26 Feb 2024 14:54:33 -0500 Subject: [PATCH 01/13] Updating google analytics metrics getter --- NAMESPACE | 4 +- R/auth.R | 30 +++- R/github.R | 51 +++--- R/google-analytics.R | 149 ++++++++++++------ R/token-handlers.R | 2 - R/utils.R | 1 - R/write-data.R | 14 +- R/youtube.R | 17 +- inst/extdata/docker/Dockerfile | 3 +- man/authorize.Rd | 2 +- man/get_ga_property.Rd | 33 ++++ man/get_ga_property_info.Rd | 29 ++++ ..._metrics.Rd => get_multiple_ga_metrics.Rd} | 17 +- man/list_example_data.Rd | 1 - man/write_playlist_details.Rd | 17 +- man/write_to_gsheet.Rd | 12 +- tests/testthat/test-google-analytics.R | 6 +- vignettes/getting-started.Rmd | 5 +- 18 files changed, 280 insertions(+), 113 deletions(-) create mode 100644 man/get_ga_property.Rd create mode 100644 man/get_ga_property_info.Rd rename man/{get_all_ga_metrics.Rd => get_multiple_ga_metrics.Rd} (76%) diff --git a/NAMESPACE b/NAMESPACE index 709c918..c86b412 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -9,11 +9,12 @@ export(clean_repo_metrics) export(delete_creds) export(example_data_folder) export(extract_answers) -export(get_all_ga_metrics) export(get_calendly_user) export(get_example_data) export(get_ga_metadata) export(get_ga_properties) +export(get_ga_property) +export(get_ga_property_info) export(get_ga_stats) export(get_ga_user) export(get_github) @@ -23,6 +24,7 @@ export(get_github_repo_timecourse) export(get_github_user) export(get_google_form) export(get_multiple_forms) +export(get_multiple_ga_metrics) export(get_multiple_repos_metrics) export(get_org_repo_list) export(get_question_metadata) diff --git a/R/auth.R b/R/auth.R index c1ab65f..41e165a 100644 --- a/R/auth.R +++ b/R/auth.R @@ -137,16 +137,34 @@ delete_creds <- function(app_name = "all") { message("No cached creds to delete (from metricminer anyway). Done") } else { if (app_name == "all" | app_name == "calendly") { - if (calendly_creds_exist) {remove_token("calendly"); message("Calendly creds deleted from environment")} - if (calendly_cache_exist) {remove_cache("calendly"); message("Calendly creds deleted from cache")} + if (calendly_creds_exist) { + remove_token("calendly") + message("Calendly creds deleted from environment") + } + if (calendly_cache_exist) { + remove_cache("calendly") + message("Calendly creds deleted from cache") + } } if (app_name == "all" | app_name == "github") { - if (github_creds_exist) {remove_token("github"); message("GitHub creds deleted from environment")} - if (github_cache_exist) {remove_cache("github"); message("GitHub creds deleted from cache")} + if (github_creds_exist) { + remove_token("github") + message("GitHub creds deleted from environment") + } + if (github_cache_exist) { + remove_cache("github") + message("GitHub creds deleted from cache") + } } if (app_name == "all" | app_name == "google") { - if (google_creds_exist) {remove_token("google"); message("Cached Google token removed from environment")} - if (google_cache_exist) {remove_cache("google"); message("Cached Google creds removed from cache")} + if (google_creds_exist) { + remove_token("google") + message("Cached Google token removed from environment") + } + if (google_cache_exist) { + remove_cache("google") + message("Cached Google creds removed from cache") + } } } } diff --git a/R/github.R b/R/github.R index f143900..f367029 100644 --- a/R/github.R +++ b/R/github.R @@ -161,7 +161,6 @@ get_user_repo_list <- function(owner, count = "all", data_format = "dataframe", #' timecourse_metrics <- get_github_repo_timecourse(repo = "fhdsl/metricminer") #' } get_github_metrics <- function(repo, token = NULL, count = "all", data_format = "dataframe", time_course = FALSE) { - if (count == "all") count <- Inf if (is.null(token)) { @@ -177,8 +176,8 @@ get_github_metrics <- function(repo, token = NULL, count = "all", data_format = if (time_course) { api_calls <- list( - clones = "GET /repos/{owner}/{repo}/traffic/clones", - views = "GET /repos/{owner}/{repo}/traffic/views" + clones = "GET /repos/{owner}/{repo}/traffic/clones", + views = "GET /repos/{owner}/{repo}/traffic/views" ) } else { api_calls <- list( @@ -197,7 +196,7 @@ get_github_metrics <- function(repo, token = NULL, count = "all", data_format = repo = repo, token = token, count = count - ) + ) } # Run gh_repo_wrapper_fn() on api_calls # when error occurs, set value to "Not Found" @@ -206,7 +205,6 @@ get_github_metrics <- function(repo, token = NULL, count = "all", data_format = names(results) <- names(api_calls) if (data_format == "dataframe") { - if (time_course) { clones_test <- try(results$clones$clones[[1]]$timestamp, silent = TRUE) views_test <- try(results$views$views[[1]]$timestamp, silent = TRUE) @@ -223,12 +221,15 @@ get_github_metrics <- function(repo, token = NULL, count = "all", data_format = } results <- - dplyr::full_join(clones_data, views_data, by = "timestamp", - suffix = c("_clones", "_views")) %>% - dplyr::mutate(repo = paste0(c(owner, repo), collapse = "/"), - .before = dplyr::everything()) + dplyr::full_join(clones_data, views_data, + by = "timestamp", + suffix = c("_clones", "_views") + ) %>% + dplyr::mutate( + repo = paste0(c(owner, repo), collapse = "/"), + .before = dplyr::everything() + ) } else { - results <- clean_repo_metrics( repo_name = paste0(c(owner, repo), collapse = "/"), repo_metric_list = results @@ -253,12 +254,13 @@ get_github_metrics <- function(repo, token = NULL, count = "all", data_format = #' timecourse_metrics <- get_github_repo_timecourse(repo = "fhdsl/metricminer") #' } get_github_repo_timecourse <- function(repo, token = NULL, count = "all", data_format = "dataframe") { - - result <- get_github_metrics(repo = repo, - token = token, - count = count, - data_format = data_format, - time_course = TRUE) + result <- get_github_metrics( + repo = repo, + token = token, + count = count, + data_format = data_format, + time_course = TRUE + ) return(result) } @@ -278,12 +280,13 @@ get_github_repo_timecourse <- function(repo, token = NULL, count = "all", data_f #' summary_metrics <- get_github_repo_summary(repo = "fhdsl/metricminer") #' } get_github_repo_summary <- function(repo, token = NULL, count = "all", data_format = "dataframe") { - - result <- get_github_metrics(repo = repo, - token = token, - count = count, - data_format = data_format, - time_course = FALSE) + result <- get_github_metrics( + repo = repo, + token = token, + count = count, + data_format = data_format, + time_course = FALSE + ) return(result) } @@ -388,7 +391,6 @@ gh_repo_wrapper <- function(api_call, owner, repo, token = NULL, count = Inf) { #' @export #' clean_repo_metrics <- function(repo_name, repo_metric_list) { - ### Summarize the rest if (repo_metric_list$contributors[1] != "No results") { contributors <- @@ -437,14 +439,13 @@ clean_repo_metrics <- function(repo_name, repo_metric_list) { #' @export #' get_timestamp_repo_metrics <- function(results, column) { - data <- results[[column]][[column]] data <- dplyr::bind_rows(data) %>% dplyr::mutate( timestamp = lubridate::as_date(timestamp), count = as.numeric(count), uniques = as.numeric(uniques) - ) + ) return(data) } diff --git a/R/google-analytics.R b/R/google-analytics.R index 8e0d6ef..7b1bd20 100644 --- a/R/google-analytics.R +++ b/R/google-analytics.R @@ -127,6 +127,41 @@ get_ga_properties <- function(account_id, token = NULL) { return(results$properties) } +#' Get all property information for a particular property id +#' @description This is a function to get the Google Analytics accounts that this user has access to. +#' The scope it uses is the `See and download your Google Analytics data.` +#' If you don't check this box on the OAuth screen, this function will not work. +#' @param property_id the property id you want information about. +#' @param token credentials for access to Google using OAuth. `authorize("google")` +#' @importFrom httr config accept_json content +#' @importFrom jsonlite fromJSON +#' @importFrom assertthat assert_that is.string +#' @return All the property ids and information about them for a Google Analytics account. +#' @export +#' @examples \dontrun{ +#' +#' authorize("google") +#' +#' property_info <- get_ga_property_info(property_id = "422558989") +#' } +get_ga_property_info <- function(property_id, token = NULL) { + + results <- request_ga( + token = token, + url = paste0("https://analyticsadmin.googleapis.com/v1alpha/properties/", property_id), + request_type = "GET" + ) + + if (length(results) == 0) { + message(paste0( + "No properties of that id: ", property_id, "\n", + "Are you sure you have permissions to view this property_id?" + )) + } + + return(results) +} + #' Get metadata associated Google Analytics property #' @description This is a function to get the Google Analytics accounts that this user has access to. #' The scope it uses is the `See and download your Google Analytics data.` @@ -167,6 +202,46 @@ get_ga_metadata <- function(property_id, token = NULL) { return(results) } +#' Get metadata associated Google Analytics property +#' @description This is a function to get the Google Analytics accounts that this user has access to. +#' The scope it uses is the `See and download your Google Analytics data.` +#' If you don't check this box on the OAuth screen, this function will not work. +#' @param property_id a GA property. Looks like '123456789' Can be obtained from running `get_ga_properties()` +#' @param token credentials for access to Google using OAuth. `authorize("google")` +#' @importFrom httr config accept_json content +#' @importFrom jsonlite fromJSON +#' @importFrom assertthat assert_that is.string +#' @return A list showing the metadata types available for the Google Analytics property. This can be used to craft an API request. +#' @export +#' @examples \dontrun{ +#' +#' authorize("google") +#' accounts <- get_ga_user() +#' +#' properties_list <- get_ga_properties(account_id = accounts$id[1]) +#' +#' property_id <- gsub("properties/", "", properties_list$name[1]) +#' property_metadata <- get_ga_property(property_id = property_id) +#' } +get_ga_property <- function(property_id, token = NULL) { + # Declare URL + url <- "https://analyticsdata.googleapis.com/v1beta/properties/property_id" + url <- gsub("property_id", property_id, url) + + if (is.null(token)) { + # Get auth token + token <- get_token(app_name = "google") + } + + results <- request_ga( + token = token, + url = url, + request_type = "GET" + ) + + return(results) +} + #' Get stats for an associated Google Analytics property #' @description This is a function to get the Google Analytics accounts that this user has access to. #' The scope it uses is the `See and download your Google Analytics data.` @@ -287,6 +362,7 @@ link_clicks <- function() { #' @param account_id the account id that you'd like to retrieve stats for all properties associated with it. #' @param token credentials for access to Google using OAuth. `authorize("google")` #' @param dataformat How would you like the data returned to you? Default is a "dataframe" but if you'd like to see the original API list result, put "raw". +#' @param stats_type Do you want to retrieve metrics or dimensions? List all you want to collect as a vector #' @returns Either a list of dataframes where `metrics`, `dimensions` and `link clicks` are reported. But if `format` is set to "raw" then the original raw API results will be returned #' @export #' @return A list of metrics, dimensions, and link clicks for a for all properties underneath a Google Analytics account. It can be returned as a curated data.frame or the raw version which is the API response as a list @@ -295,14 +371,24 @@ link_clicks <- function() { #' authorize("google") #' accounts <- get_ga_user() #' -#' some_stats_list <- get_all_ga_metrics(property_ids = property_ids) +#' property_ids <- c("422671031", "422558989") +#' #' } -get_all_ga_metrics <- function(account_id = NULL, token = NULL, dataformat = "dataframe") { +get_multiple_ga_metrics <- function(account_id = NULL, property_ids = NULL, token = NULL, dataformat = "dataframe", + stats_type = c("metrics", "dimensions", "link_clicks")) { if (is.null(token)) { # Get auth token token <- get_token(app_name = "google") } + # Grab display names + if (!is.null(property_ids)) { + display_names <- sapply(property_ids, function(property_id) { + property_info <- get_ga_property_info(property_id, token = token) + return(property_info$displayName) + }) + } + if (!is.null(account_id)) { message("Retrieving all properties underneath this account") @@ -314,58 +400,33 @@ get_all_ga_metrics <- function(account_id = NULL, token = NULL, dataformat = "da } property_ids <- gsub("properties/", "", properties_list$name) display_names <- properties_list$displayName - } else { - stop("Must provide an account id for the property ids you wish to retrieve") } - # Now loop through all the properties - all_ga_metrics <- lapply(property_ids, function(property_id) { - # Be vocal about it - message(paste("Retrieving", property_id, "metrics")) - # Get the stats - metrics <- get_ga_stats(token = token, property_id, stats_type = "metrics", dataformat = "raw") - return(metrics) - }) - # Now loop through all the properties - all_ga_dimensions <- sapply(property_ids, function(property_id) { - # Be vocal about it - message(paste("Retrieving", property_id, "dimensions")) - # Get the stats - dimensions <- get_ga_stats(token = token, property_id, stats_type = "dimensions", dataformat = "raw") + all_metrics <- lapply(stats_type, function(a_stats_type) { - return(dimensions) - }) + # Now loop through all the properties + per_type <- lapply(property_ids, function(property_id) { + # Be vocal about it + message(paste("Retrieving", property_id, a_stats_type)) - # Now loop through all the properties - all_ga_links <- lapply(property_ids, function(property_id) { - # Be vocal about it - message(paste("Retrieving", property_id, "link clicks")) - # Get the stats - links <- get_ga_stats(token = token, property_id, stats_type = "link_clicks", dataformat = "raw") + # Get the stats + metrics <- get_ga_stats(token = token, property_id, stats_type = a_stats_type, dataformat = "raw") - return(links) - }) + return(metrics) + }) + names(per_type) <- display_names - if (length(display_names) > 1) { - # Save the names - names(all_ga_metrics) <- display_names - names(all_ga_dimensions) <- display_names - names(all_ga_links) <- display_names - } + if (a_stats_type == "metrics") per_type <- clean_ga_metrics(per_type) + if (a_stats_type == "dimensions") per_type <- clean_ga_dimensions(per_type) + if (a_stats_type == "link_clicks") per_type <- clean_ga_dimensions(per_type) - if (dataformat == "dataframe") { - all_ga_metrics <- clean_ga_metrics(all_ga_metrics) - all_ga_dimensions <- clean_ga_dimensions(all_ga_dimensions) - all_ga_links <- clean_ga_dimensions(all_ga_links) - } + return(per_type) + }) + names(all_metrics ) <- stats_type - return(list( - metrics = all_ga_metrics, - dimensions = all_ga_dimensions, - link_clicks = all_ga_links - )) + return(all_ga_metrics) } #' Handle Google Analytics Lists diff --git a/R/token-handlers.R b/R/token-handlers.R index c390d96..059d523 100644 --- a/R/token-handlers.R +++ b/R/token-handlers.R @@ -54,7 +54,6 @@ remove_cache <- function(app_name) { # Default is to try to retrieve credentials but if credentials are not necessary # and you just want to attempt to grab credentials and see if you can then set try = TRUE get_token <- function(app_name, try = FALSE, silent = FALSE) { - # If there's none in the current environment, attempt to grab a stored credential if (is.null(.Env$metricminer_tokens[[app_name]])) { # Attempt to get stored token @@ -88,7 +87,6 @@ get_token <- function(app_name, try = FALSE, silent = FALSE) { # Check if token already exists check_for_tokens <- function(app_name = NULL) { - if (is.null(app_name)) { app_name <- c("github", "google", "calendly") } diff --git a/R/utils.R b/R/utils.R index f5b9a63..5feee3b 100644 --- a/R/utils.R +++ b/R/utils.R @@ -14,7 +14,6 @@ utils::globalVariables(c( #' # Now you could use any of these example datasets that are printed out #' #' get_example_data("calendly_events") -#' #' } list_example_data <- function() { data_list <- diff --git a/R/write-data.R b/R/write-data.R index 64bbc27..3e54da2 100644 --- a/R/write-data.R +++ b/R/write-data.R @@ -17,19 +17,23 @@ #' #' authorize("github") #' repo_list <- get_user_repo_list(owner = "metricminer") -#' gsheet <- paste0("https://docs.google.com/spreadsheets/d/", -#' "166MV4_1pfATB3Hes2HbdZCpkMc8JTT3u3eJes6Wu7Rk/edit#gid=0") +#' gsheet <- paste0( +#' "https://docs.google.com/spreadsheets/d/", +#' "166MV4_1pfATB3Hes2HbdZCpkMc8JTT3u3eJes6Wu7Rk/edit#gid=0" +#' ) #' write_to_gsheet(repo_list) #' #' datasheet <- write_to_gsheet( #' gsheet = gsheet, #' input = repo_list, append_rows = TRUE, -#' sheet = 1) +#' sheet = 1 +#' ) #' #' datasheet <- write_to_gsheet( #' gsheet = gsheet, #' input = repo_list, -#' new_sheet = "github_data") +#' new_sheet = "github_data" +#' ) #' } #' write_to_gsheet <- function(input, token = NULL, gsheet = NULL, overwrite = FALSE, append_rows = FALSE, sheet = 1, new_sheet = FALSE, ...) { @@ -44,7 +48,6 @@ write_to_gsheet <- function(input, token = NULL, gsheet = NULL, overwrite = FALS ## If no gsheet provided do they want us to make one? if (is.null(gsheet)) { - if (interactive()) { message("No Googlesheet ID/url was provided do you want us to make one for you?") gsheet_new <- menu(c("Yes create a new Googlesheet for me", "No do not create a new Googlesheet, I'll try this again but give you one to write to with the `gsheet` argument.")) @@ -63,7 +66,6 @@ write_to_gsheet <- function(input, token = NULL, gsheet = NULL, overwrite = FALS if (class(gsheet_test)[1] == "try-error") { stop("Can't find the provided gsheet") - } if (all(c(nrow(gsheet_test) > 0, overwrite == FALSE, append_rows == FALSE, new_sheet == FALSE))) { diff --git a/R/youtube.R b/R/youtube.R index 40699b6..3869d70 100644 --- a/R/youtube.R +++ b/R/youtube.R @@ -125,13 +125,16 @@ get_youtube_video_stats <- function(video_id, token = NULL, dataformat = "datafr #' @return writes a file containing the dataframe of cleaned results #' @export #' -#' @examples \dontrun{ -#' # Not run -#' write_playlist_details(playlist_id = shorts_playlist_id, -#' outfile = "youtube_shorts_data.tsv") -#' write_playlist_details( -#' playlist_id = "PL6aYJ_0zJ4uCABkMngSYjPo_3c-nUUmio", -#' outfile = "youtube_shorts_data.tsv") +#' @examples \dontrun{ +#' # Not run +#' write_playlist_details( +#' playlist_id = shorts_playlist_id, +#' outfile = "youtube_shorts_data.tsv" +#' ) +#' write_playlist_details( +#' playlist_id = "PL6aYJ_0zJ4uCABkMngSYjPo_3c-nUUmio", +#' outfile = "youtube_shorts_data.tsv" +#' ) #' } write_playlist_details <- function(playlist_id, token = NULL, outfile = NULL) { if (is.null(token)) { diff --git a/inst/extdata/docker/Dockerfile b/inst/extdata/docker/Dockerfile index 848c855..d87f1f4 100644 --- a/inst/extdata/docker/Dockerfile +++ b/inst/extdata/docker/Dockerfile @@ -6,11 +6,12 @@ COPY install_github.R . COPY git_token.txt . COPY github_package_list.tsv . - # Install packages from github RUN Rscript install_github.R \ --packages github_package_list.tsv \ --token git_token.txt +RUN R -q -e 'install.packages("cranlogs", repos="https://cran.rstudio.com/")' + # Set final workdir for commands WORKDIR /home/rstudio diff --git a/man/authorize.Rd b/man/authorize.Rd index 8bf6a9b..2ef8553 100644 --- a/man/authorize.Rd +++ b/man/authorize.Rd @@ -7,7 +7,7 @@ authorize(app_name = NULL, cache = FALSE, ...) } \arguments{ -\item{app_name}{App would you like to authorize? Supported apps are 'google' 'calendly' and 'github'} +\item{app_name}{app would you like to authorize? Supported apps are 'google' 'calendly' and 'github'} \item{cache}{Should the token be cached as an .httr-oauth file or API keys stored as global options?} diff --git a/man/get_ga_property.Rd b/man/get_ga_property.Rd new file mode 100644 index 0000000..cef51fb --- /dev/null +++ b/man/get_ga_property.Rd @@ -0,0 +1,33 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/google-analytics.R +\name{get_ga_property} +\alias{get_ga_property} +\title{Get metadata associated Google Analytics property} +\usage{ +get_ga_property(property_id, token = NULL) +} +\arguments{ +\item{property_id}{a GA property. Looks like '123456789' Can be obtained from running `get_ga_properties()`} + +\item{token}{credentials for access to Google using OAuth. `authorize("google")`} +} +\value{ +A list showing the metadata types available for the Google Analytics property. This can be used to craft an API request. +} +\description{ +This is a function to get the Google Analytics accounts that this user has access to. +The scope it uses is the `See and download your Google Analytics data.` +If you don't check this box on the OAuth screen, this function will not work. +} +\examples{ +\dontrun{ + +authorize("google") +accounts <- get_ga_user() + +properties_list <- get_ga_properties(account_id = accounts$id[1]) + +property_id <- gsub("properties/", "", properties_list$name[1]) +property_metadata <- get_ga_property(property_id = property_id) +} +} diff --git a/man/get_ga_property_info.Rd b/man/get_ga_property_info.Rd new file mode 100644 index 0000000..edc8b7a --- /dev/null +++ b/man/get_ga_property_info.Rd @@ -0,0 +1,29 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/google-analytics.R +\name{get_ga_property_info} +\alias{get_ga_property_info} +\title{Get all property information for a particular property id} +\usage{ +get_ga_property_info(property_id, token = NULL) +} +\arguments{ +\item{property_id}{the property id you want information about.} + +\item{token}{credentials for access to Google using OAuth. `authorize("google")`} +} +\value{ +All the property ids and information about them for a Google Analytics account. +} +\description{ +This is a function to get the Google Analytics accounts that this user has access to. +The scope it uses is the `See and download your Google Analytics data.` +If you don't check this box on the OAuth screen, this function will not work. +} +\examples{ +\dontrun{ + +authorize("google") + +property_info <- get_ga_property_info(property_id = "422558989") +} +} diff --git a/man/get_all_ga_metrics.Rd b/man/get_multiple_ga_metrics.Rd similarity index 76% rename from man/get_all_ga_metrics.Rd rename to man/get_multiple_ga_metrics.Rd index 6668036..7c6a20e 100644 --- a/man/get_all_ga_metrics.Rd +++ b/man/get_multiple_ga_metrics.Rd @@ -1,10 +1,16 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/google-analytics.R -\name{get_all_ga_metrics} -\alias{get_all_ga_metrics} +\name{get_multiple_ga_metrics} +\alias{get_multiple_ga_metrics} \title{Get all metrics for all properties associated with an account} \usage{ -get_all_ga_metrics(account_id = NULL, token = NULL, dataformat = "dataframe") +get_multiple_ga_metrics( + account_id = NULL, + property_ids = NULL, + token = NULL, + dataformat = "dataframe", + stats_type = c("metrics", "dimensions", "link_clicks") +) } \arguments{ \item{account_id}{the account id that you'd like to retrieve stats for all properties associated with it.} @@ -12,6 +18,8 @@ get_all_ga_metrics(account_id = NULL, token = NULL, dataformat = "dataframe") \item{token}{credentials for access to Google using OAuth. `authorize("google")`} \item{dataformat}{How would you like the data returned to you? Default is a "dataframe" but if you'd like to see the original API list result, put "raw".} + +\item{stats_type}{Do you want to retrieve metrics or dimensions? List all you want to collect as a vector} } \value{ Either a list of dataframes where `metrics`, `dimensions` and `link clicks` are reported. But if `format` is set to "raw" then the original raw API results will be returned @@ -27,6 +35,7 @@ This is a function to gets metrics and dimensions for all properties associated authorize("google") accounts <- get_ga_user() -some_stats_list <- get_all_ga_metrics(property_ids = property_ids) +property_ids <- c("422671031", "422558989") + } } diff --git a/man/list_example_data.Rd b/man/list_example_data.Rd index 4f33093..4083333 100644 --- a/man/list_example_data.Rd +++ b/man/list_example_data.Rd @@ -20,6 +20,5 @@ list_example_data() # Now you could use any of these example datasets that are printed out get_example_data("calendly_events") - } } diff --git a/man/write_playlist_details.Rd b/man/write_playlist_details.Rd index 42147d6..298bfd1 100644 --- a/man/write_playlist_details.Rd +++ b/man/write_playlist_details.Rd @@ -20,12 +20,15 @@ writes a file containing the dataframe of cleaned results Write playlist details from YouTube } \examples{ - \dontrun{ - # Not run - write_playlist_details(playlist_id = shorts_playlist_id, - outfile = "youtube_shorts_data.tsv") - write_playlist_details( - playlist_id = "PL6aYJ_0zJ4uCABkMngSYjPo_3c-nUUmio", - outfile = "youtube_shorts_data.tsv") +\dontrun{ +# Not run +write_playlist_details( + playlist_id = shorts_playlist_id, + outfile = "youtube_shorts_data.tsv" +) +write_playlist_details( + playlist_id = "PL6aYJ_0zJ4uCABkMngSYjPo_3c-nUUmio", + outfile = "youtube_shorts_data.tsv" +) } } diff --git a/man/write_to_gsheet.Rd b/man/write_to_gsheet.Rd index e1a8e96..0c1b9cf 100644 --- a/man/write_to_gsheet.Rd +++ b/man/write_to_gsheet.Rd @@ -45,19 +45,23 @@ When you get to the OAuth consent screen. If you do not check this box, this fun authorize("github") repo_list <- get_user_repo_list(owner = "metricminer") -gsheet <- paste0("https://docs.google.com/spreadsheets/d/", - "166MV4_1pfATB3Hes2HbdZCpkMc8JTT3u3eJes6Wu7Rk/edit#gid=0") +gsheet <- paste0( + "https://docs.google.com/spreadsheets/d/", + "166MV4_1pfATB3Hes2HbdZCpkMc8JTT3u3eJes6Wu7Rk/edit#gid=0" +) write_to_gsheet(repo_list) datasheet <- write_to_gsheet( gsheet = gsheet, input = repo_list, append_rows = TRUE, - sheet = 1) + sheet = 1 +) datasheet <- write_to_gsheet( gsheet = gsheet, input = repo_list, - new_sheet = "github_data") + new_sheet = "github_data" +) } } diff --git a/tests/testthat/test-google-analytics.R b/tests/testthat/test-google-analytics.R index 5e97046..c93c5ac 100644 --- a/tests/testthat/test-google-analytics.R +++ b/tests/testthat/test-google-analytics.R @@ -61,7 +61,11 @@ if (all(!(auth_tokens == ""))) { in_test = TRUE ) - stats_list <- get_all_ga_metrics(account_id = 209776907) + stats_list <- get_multiple_ga_metrics(account_id = 209776907) + + expect_named(stats_list, c("metrics", "dimensions", "link_clicks")) + + stats_list <- get_multiple_ga_metrics(property_ids = c(422671031, 422558989)) expect_named(stats_list, c("metrics", "dimensions", "link_clicks")) }) diff --git a/vignettes/getting-started.Rmd b/vignettes/getting-started.Rmd index 302f267..998f6dc 100644 --- a/vignettes/getting-started.Rmd +++ b/vignettes/getting-started.Rmd @@ -196,10 +196,11 @@ authorize("google") accounts <- get_ga_user() ``` -Then we can provide the account id to `all_ga_metrics` and it will attempt to grab all stats for all website properties underneath the provided account. +Then we can provide the account id to `get_multiple_ga_metrics` and it will attempt to grab all stats for all website properties underneath the provided account. ``` -stats_list <- all_ga_metrics(account_id = accounts$id[5]) +account_stats_list <- get_multiple_ga_metrics(account_id = 209776907) +stats_list <- stats_list <- get_multiple_ga_metrics(property_ids = c(422671031, 422558989)) ``` From 317acee466365cafb9ee63302721467bb0a836a3 Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Mon, 26 Feb 2024 15:25:31 -0500 Subject: [PATCH 02/13] Add test for get_ga_property_info --- tests/testthat/test-google-analytics.R | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/testthat/test-google-analytics.R b/tests/testthat/test-google-analytics.R index c93c5ac..4d3001e 100644 --- a/tests/testthat/test-google-analytics.R +++ b/tests/testthat/test-google-analytics.R @@ -41,6 +41,9 @@ if (all(!(auth_tokens == ""))) { expect_named(property_metadata, c("dimensions", "metrics", "name")) + property_info <- get_ga_property_info(property_id) + expect_length(property_info, 11) + metrics <- get_ga_stats(property_id, stats_type = "metrics") expect_named(metrics, c( "website", "activeUsers", "newUsers", "totalUsers", From 25accd8f6bca51e490574f258043dc5ab58adde9 Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Mon, 26 Feb 2024 15:37:34 -0500 Subject: [PATCH 03/13] Fix code --- R/google-analytics.R | 5 +++-- man/get_multiple_ga_metrics.Rd | 2 ++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/R/google-analytics.R b/R/google-analytics.R index 7b1bd20..f7bb25a 100644 --- a/R/google-analytics.R +++ b/R/google-analytics.R @@ -360,6 +360,7 @@ link_clicks <- function() { #' Get all metrics for all properties associated with an account #' @description This is a function to gets metrics and dimensions for all properties associated with an account. The scope it uses is the `See and download your Google Analytics data.` If you don't check this box on the OAuth screen. #' @param account_id the account id that you'd like to retrieve stats for all properties associated with it. +#' @param property_id A vector of property ids you'd like to retrieve metrics for. #' @param token credentials for access to Google using OAuth. `authorize("google")` #' @param dataformat How would you like the data returned to you? Default is a "dataframe" but if you'd like to see the original API list result, put "raw". #' @param stats_type Do you want to retrieve metrics or dimensions? List all you want to collect as a vector @@ -424,9 +425,9 @@ get_multiple_ga_metrics <- function(account_id = NULL, property_ids = NULL, toke return(per_type) }) - names(all_metrics ) <- stats_type + names(all_metrics) <- stats_type - return(all_ga_metrics) + return(all_metrics) } #' Handle Google Analytics Lists diff --git a/man/get_multiple_ga_metrics.Rd b/man/get_multiple_ga_metrics.Rd index 7c6a20e..4ec2b2f 100644 --- a/man/get_multiple_ga_metrics.Rd +++ b/man/get_multiple_ga_metrics.Rd @@ -20,6 +20,8 @@ get_multiple_ga_metrics( \item{dataformat}{How would you like the data returned to you? Default is a "dataframe" but if you'd like to see the original API list result, put "raw".} \item{stats_type}{Do you want to retrieve metrics or dimensions? List all you want to collect as a vector} + +\item{property_id}{A vector of property ids you'd like to retrieve metrics for.} } \value{ Either a list of dataframes where `metrics`, `dimensions` and `link clicks` are reported. But if `format` is set to "raw" then the original raw API results will be returned From bac4da6e3b66f85261eac83418134f9d7f72ed73 Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Mon, 26 Feb 2024 15:52:36 -0500 Subject: [PATCH 04/13] Update --- R/google-analytics.R | 2 +- R/utils.R | 4 ++-- man/cache_secrets_folder.Rd | 2 +- man/get_multiple_ga_metrics.Rd | 4 ++-- tests/testthat/test-google-analytics.R | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/R/google-analytics.R b/R/google-analytics.R index f7bb25a..899c2ac 100644 --- a/R/google-analytics.R +++ b/R/google-analytics.R @@ -360,7 +360,7 @@ link_clicks <- function() { #' Get all metrics for all properties associated with an account #' @description This is a function to gets metrics and dimensions for all properties associated with an account. The scope it uses is the `See and download your Google Analytics data.` If you don't check this box on the OAuth screen. #' @param account_id the account id that you'd like to retrieve stats for all properties associated with it. -#' @param property_id A vector of property ids you'd like to retrieve metrics for. +#' @param property_ids A vector of property ids you'd like to retrieve metrics for. #' @param token credentials for access to Google using OAuth. `authorize("google")` #' @param dataformat How would you like the data returned to you? Default is a "dataframe" but if you'd like to see the original API list result, put "raw". #' @param stats_type Do you want to retrieve metrics or dimensions? List all you want to collect as a vector diff --git a/R/utils.R b/R/utils.R index 5feee3b..b8ee325 100644 --- a/R/utils.R +++ b/R/utils.R @@ -1,6 +1,6 @@ utils::globalVariables(c( "result", "num", "test_name", "scopes", "set_token", "browseURL", "remove_token", "get_token", "get_github", "get_calendly", "%>%", - "token", "query_params", "file_name", "accounts", "get_repo_list", "timestamp", "uniques", "req" + "token", "query_params", "file_name", "accounts", "get_repo_list", "timestamp", "uniques", "req", "cache_secrets_folder" )) #' Get list of example datasets @@ -110,7 +110,7 @@ key_encrypt_creds_path <- function() { ) } -#' see where your cached secrets are being stored +#' See where your cached secrets are being stored #' @description This is a function to retrieve the file path of where your cached secrets are stored #' @return an file path that shows where your cached secrets are stored #' @export diff --git a/man/cache_secrets_folder.Rd b/man/cache_secrets_folder.Rd index a27a217..9b0062e 100644 --- a/man/cache_secrets_folder.Rd +++ b/man/cache_secrets_folder.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/utils.R \name{cache_secrets_folder} \alias{cache_secrets_folder} -\title{see where your cached secrets are being stored} +\title{See where your cached secrets are being stored} \usage{ cache_secrets_folder() } diff --git a/man/get_multiple_ga_metrics.Rd b/man/get_multiple_ga_metrics.Rd index 4ec2b2f..4b668ef 100644 --- a/man/get_multiple_ga_metrics.Rd +++ b/man/get_multiple_ga_metrics.Rd @@ -15,13 +15,13 @@ get_multiple_ga_metrics( \arguments{ \item{account_id}{the account id that you'd like to retrieve stats for all properties associated with it.} +\item{property_ids}{A vector of property ids you'd like to retrieve metrics for.} + \item{token}{credentials for access to Google using OAuth. `authorize("google")`} \item{dataformat}{How would you like the data returned to you? Default is a "dataframe" but if you'd like to see the original API list result, put "raw".} \item{stats_type}{Do you want to retrieve metrics or dimensions? List all you want to collect as a vector} - -\item{property_id}{A vector of property ids you'd like to retrieve metrics for.} } \value{ Either a list of dataframes where `metrics`, `dimensions` and `link clicks` are reported. But if `format` is set to "raw" then the original raw API results will be returned diff --git a/tests/testthat/test-google-analytics.R b/tests/testthat/test-google-analytics.R index 4d3001e..38f8881 100644 --- a/tests/testthat/test-google-analytics.R +++ b/tests/testthat/test-google-analytics.R @@ -42,7 +42,7 @@ if (all(!(auth_tokens == ""))) { expect_named(property_metadata, c("dimensions", "metrics", "name")) property_info <- get_ga_property_info(property_id) - expect_length(property_info, 11) + expect_length(property_info, 10) metrics <- get_ga_stats(property_id, stats_type = "metrics") expect_named(metrics, c( From 23b3f380e790e43f1ff6312ad9722c7b56076218 Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Mon, 26 Feb 2024 15:58:22 -0500 Subject: [PATCH 05/13] Update docs --- R/google-analytics.R | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/R/google-analytics.R b/R/google-analytics.R index 899c2ac..9dfe8e9 100644 --- a/R/google-analytics.R +++ b/R/google-analytics.R @@ -208,9 +208,6 @@ get_ga_metadata <- function(property_id, token = NULL) { #' If you don't check this box on the OAuth screen, this function will not work. #' @param property_id a GA property. Looks like '123456789' Can be obtained from running `get_ga_properties()` #' @param token credentials for access to Google using OAuth. `authorize("google")` -#' @importFrom httr config accept_json content -#' @importFrom jsonlite fromJSON -#' @importFrom assertthat assert_that is.string #' @return A list showing the metadata types available for the Google Analytics property. This can be used to craft an API request. #' @export #' @examples \dontrun{ @@ -372,7 +369,9 @@ link_clicks <- function() { #' authorize("google") #' accounts <- get_ga_user() #' -#' property_ids <- c("422671031", "422558989") +#' all_properties <- get_multiple_ga_metrics(account_id = accounts$id) +#' +#' some_properties <- get_multiple_ga_metrics(property_ids = c("422671031", "422558989")) #' #' } get_multiple_ga_metrics <- function(account_id = NULL, property_ids = NULL, token = NULL, dataformat = "dataframe", From 877d1786cbc07e8f4cf0cb11e868d9016b238c45 Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Mon, 26 Feb 2024 16:03:41 -0500 Subject: [PATCH 06/13] Update docs --- NAMESPACE | 1 - R/google-analytics.R | 37 ---------------------------------- man/get_ga_property.Rd | 33 ------------------------------ man/get_multiple_ga_metrics.Rd | 4 +++- 4 files changed, 3 insertions(+), 72 deletions(-) delete mode 100644 man/get_ga_property.Rd diff --git a/NAMESPACE b/NAMESPACE index c86b412..9c3f223 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -13,7 +13,6 @@ export(get_calendly_user) export(get_example_data) export(get_ga_metadata) export(get_ga_properties) -export(get_ga_property) export(get_ga_property_info) export(get_ga_stats) export(get_ga_user) diff --git a/R/google-analytics.R b/R/google-analytics.R index 9dfe8e9..8520312 100644 --- a/R/google-analytics.R +++ b/R/google-analytics.R @@ -202,43 +202,6 @@ get_ga_metadata <- function(property_id, token = NULL) { return(results) } -#' Get metadata associated Google Analytics property -#' @description This is a function to get the Google Analytics accounts that this user has access to. -#' The scope it uses is the `See and download your Google Analytics data.` -#' If you don't check this box on the OAuth screen, this function will not work. -#' @param property_id a GA property. Looks like '123456789' Can be obtained from running `get_ga_properties()` -#' @param token credentials for access to Google using OAuth. `authorize("google")` -#' @return A list showing the metadata types available for the Google Analytics property. This can be used to craft an API request. -#' @export -#' @examples \dontrun{ -#' -#' authorize("google") -#' accounts <- get_ga_user() -#' -#' properties_list <- get_ga_properties(account_id = accounts$id[1]) -#' -#' property_id <- gsub("properties/", "", properties_list$name[1]) -#' property_metadata <- get_ga_property(property_id = property_id) -#' } -get_ga_property <- function(property_id, token = NULL) { - # Declare URL - url <- "https://analyticsdata.googleapis.com/v1beta/properties/property_id" - url <- gsub("property_id", property_id, url) - - if (is.null(token)) { - # Get auth token - token <- get_token(app_name = "google") - } - - results <- request_ga( - token = token, - url = url, - request_type = "GET" - ) - - return(results) -} - #' Get stats for an associated Google Analytics property #' @description This is a function to get the Google Analytics accounts that this user has access to. #' The scope it uses is the `See and download your Google Analytics data.` diff --git a/man/get_ga_property.Rd b/man/get_ga_property.Rd deleted file mode 100644 index cef51fb..0000000 --- a/man/get_ga_property.Rd +++ /dev/null @@ -1,33 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/google-analytics.R -\name{get_ga_property} -\alias{get_ga_property} -\title{Get metadata associated Google Analytics property} -\usage{ -get_ga_property(property_id, token = NULL) -} -\arguments{ -\item{property_id}{a GA property. Looks like '123456789' Can be obtained from running `get_ga_properties()`} - -\item{token}{credentials for access to Google using OAuth. `authorize("google")`} -} -\value{ -A list showing the metadata types available for the Google Analytics property. This can be used to craft an API request. -} -\description{ -This is a function to get the Google Analytics accounts that this user has access to. -The scope it uses is the `See and download your Google Analytics data.` -If you don't check this box on the OAuth screen, this function will not work. -} -\examples{ -\dontrun{ - -authorize("google") -accounts <- get_ga_user() - -properties_list <- get_ga_properties(account_id = accounts$id[1]) - -property_id <- gsub("properties/", "", properties_list$name[1]) -property_metadata <- get_ga_property(property_id = property_id) -} -} diff --git a/man/get_multiple_ga_metrics.Rd b/man/get_multiple_ga_metrics.Rd index 4b668ef..679ccc5 100644 --- a/man/get_multiple_ga_metrics.Rd +++ b/man/get_multiple_ga_metrics.Rd @@ -37,7 +37,9 @@ This is a function to gets metrics and dimensions for all properties associated authorize("google") accounts <- get_ga_user() -property_ids <- c("422671031", "422558989") +all_properties <- get_multiple_ga_metrics(account_id = accounts$id) + +some_properties <- get_multiple_ga_metrics(property_ids = c("422671031", "422558989")) } } From 8cd8e4eebf473e52fa3d904db72dbb55e317abeb Mon Sep 17 00:00:00 2001 From: Howard Baek <50791792+howardbaek@users.noreply.github.com> Date: Fri, 1 Mar 2024 11:15:04 -0800 Subject: [PATCH 07/13] Does setup necessary to use magrittr's pipe operator with `usethis::use_pipe()` --- DESCRIPTION | 13 +++++++------ NAMESPACE | 2 ++ R/utils-pipe.R | 14 ++++++++++++++ man/pipe.Rd | 20 ++++++++++++++++++++ 4 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 R/utils-pipe.R create mode 100644 man/pipe.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 935ecae..734dc97 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -19,20 +19,21 @@ URL: https://github.com/fhdsl/metricminer BugReports: https://github.com/fhdsl/metricminer/issues Imports: httr, - jsonlite, - assertthat, - openssl, - gh (>= 1.3.0), + jsonlite, + assertthat, + openssl, + gh (>= 1.3.0), getPass, dplyr, - lubridate, - purrr, + lubridate, + purrr, tidyr, googledrive, googlesheets4, janitor, stringr, methods, + magrittr Suggests: knitr, rmarkdown, diff --git a/NAMESPACE b/NAMESPACE index 9c3f223..3724250 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,6 @@ # Generated by roxygen2: do not edit by hand +export("%>%") export(auth_from_secret) export(authorize) export(cache_secrets_folder) @@ -63,6 +64,7 @@ importFrom(httr,oauth_endpoints) importFrom(janitor,make_clean_names) importFrom(jsonlite,fromJSON) importFrom(lubridate,today) +importFrom(magrittr,"%>%") importFrom(methods,is) importFrom(purrr,map) importFrom(stringr,str_to_title) diff --git a/R/utils-pipe.R b/R/utils-pipe.R new file mode 100644 index 0000000..fd0b1d1 --- /dev/null +++ b/R/utils-pipe.R @@ -0,0 +1,14 @@ +#' Pipe operator +#' +#' See \code{magrittr::\link[magrittr:pipe]{\%>\%}} for details. +#' +#' @name %>% +#' @rdname pipe +#' @keywords internal +#' @export +#' @importFrom magrittr %>% +#' @usage lhs \%>\% rhs +#' @param lhs A value or the magrittr placeholder. +#' @param rhs A function call using the magrittr semantics. +#' @return The result of calling `rhs(lhs)`. +NULL diff --git a/man/pipe.Rd b/man/pipe.Rd new file mode 100644 index 0000000..1f8f237 --- /dev/null +++ b/man/pipe.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils-pipe.R +\name{\%>\%} +\alias{\%>\%} +\title{Pipe operator} +\usage{ +lhs \%>\% rhs +} +\arguments{ +\item{lhs}{A value or the magrittr placeholder.} + +\item{rhs}{A function call using the magrittr semantics.} +} +\value{ +The result of calling `rhs(lhs)`. +} +\description{ +See \code{magrittr::\link[magrittr:pipe]{\%>\%}} for details. +} +\keyword{internal} From 6777187879223fd47b94a1d30f503a6c05f14a80 Mon Sep 17 00:00:00 2001 From: Howard Baek <50791792+howardbaek@users.noreply.github.com> Date: Fri, 1 Mar 2024 11:15:25 -0800 Subject: [PATCH 08/13] Delete `library(magrittr)` in R code --- R/google-analytics.R | 2 -- 1 file changed, 2 deletions(-) diff --git a/R/google-analytics.R b/R/google-analytics.R index 8520312..2f214c0 100644 --- a/R/google-analytics.R +++ b/R/google-analytics.R @@ -1,7 +1,5 @@ # Extracting data from Google Analytics -library(magrittr) - #' Handler for API requests from Google Analytics #' @description This is a function that handles requests from Google Analytics. #' The scope it uses is the `See and download your Google Analytics data.` From b56a6e85b5bb7c9476f0c1265d9bd4e6000b1d36 Mon Sep 17 00:00:00 2001 From: Howard Baek <50791792+howardbaek@users.noreply.github.com> Date: Fri, 1 Mar 2024 11:22:56 -0800 Subject: [PATCH 09/13] Fix user facing message --- R/token-handlers.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/token-handlers.R b/R/token-handlers.R index 059d523..f167ced 100644 --- a/R/token-handlers.R +++ b/R/token-handlers.R @@ -68,7 +68,7 @@ get_token <- function(app_name, try = FALSE, silent = FALSE) { } # only print this message if we are successful if (!is.null(.Env$metricminer_tokens[[app_name]])) { - if (!silent) message("Using user-supplied cached token using authorize(\"", app_name, "\")") + if (!silent) message("Using user-supplied cached tokens stored using authorize(\"", app_name, "\")") if (app_name == "google") { googledrive::drive_auth(token = .Env$metricminer_tokens[[app_name]]) googlesheets4::gs4_auth(token = .Env$metricminer_tokens[[app_name]]) From ac971119f3339ed65870055c37c00d35589506f3 Mon Sep 17 00:00:00 2001 From: cansavvy Date: Tue, 5 Mar 2024 11:37:12 -0500 Subject: [PATCH 10/13] Add back scope --- R/scopes.R | 1 + 1 file changed, 1 insertion(+) diff --git a/R/scopes.R b/R/scopes.R index c9b77a4..72b4399 100644 --- a/R/scopes.R +++ b/R/scopes.R @@ -9,6 +9,7 @@ find_scopes <- function(app_name) { "https://www.googleapis.com/auth/drive.metadata.readonly", "https://www.googleapis.com/auth/forms.body.readonly", "https://www.googleapis.com/auth/forms.responses.readonly", + "https://www.googleapis.com/auth/analytics", "https://www.googleapis.com/auth/analytics.readonly", "https://www.googleapis.com/auth/spreadsheets", "https://www.googleapis.com/auth/youtube.readonly" From 0ae724d9dd7fd6e9ceac576a040b80c66a95d461 Mon Sep 17 00:00:00 2001 From: cansavvy Date: Tue, 5 Mar 2024 12:03:43 -0500 Subject: [PATCH 11/13] Clear up examples --- R/google-analytics.R | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/R/google-analytics.R b/R/google-analytics.R index 2f214c0..1d7f0df 100644 --- a/R/google-analytics.R +++ b/R/google-analytics.R @@ -2,8 +2,7 @@ #' Handler for API requests from Google Analytics #' @description This is a function that handles requests from Google Analytics. -#' The scope it uses is the `See and download your Google Analytics data.` -#' If you don't check this box on the OAuth screen, this function will not work. +#' The scopes it uses is the `See and download your Google Analytics data` and `View and manage your Google Analytics data` If you don't check these boxes on the OAuth screen this won't work. #' @param url The endpoint URL for the request #' @param token credentials for access to Google using OAuth. `authorize("google")` #' @param body_params The body parameters for the request @@ -56,8 +55,7 @@ request_ga <- function(token, url, query = NULL, body_params = NULL, request_typ #' Get Google Analytics Accounts #' @description This is a function to get the Google Analytics accounts that -#' this user has access to. The scope it uses is the `See and download your Google Analytics data.` -#' If you don't check this box on the OAuth screen, this function will not work. +#' this user has access to. The scopes it uses is the `See and download your Google Analytics data` and `View and manage your Google Analytics data` If you don't check these boxes on the OAuth screen this won't work. #' @param request_type Is this a GET or a POST? #' @param token credentials for access to Google using OAuth. `authorize("google")` #' @importFrom httr config accept_json content @@ -90,9 +88,7 @@ get_ga_user <- function(token = NULL, request_type = "GET") { } #' Get all property ids for all Google Analytics associated with an account id -#' @description This is a function to get the Google Analytics accounts that this user has access to. -#' The scope it uses is the `See and download your Google Analytics data.` -#' If you don't check this box on the OAuth screen, this function will not work. +#' @description The scopes it uses is the `See and download your Google Analytics data` and `View and manage your Google Analytics data` If you don't check these boxes on the OAuth screen this won't work. #' @param account_id the account id of the properties you are trying to retrieve #' @param token credentials for access to Google using OAuth. `authorize("google")` #' @importFrom httr config accept_json content @@ -127,8 +123,7 @@ get_ga_properties <- function(account_id, token = NULL) { #' Get all property information for a particular property id #' @description This is a function to get the Google Analytics accounts that this user has access to. -#' The scope it uses is the `See and download your Google Analytics data.` -#' If you don't check this box on the OAuth screen, this function will not work. +#' The scopes it uses is the `See and download your Google Analytics data` and `View and manage your Google Analytics data` If you don't check these boxes on the OAuth screen this won't work. #' @param property_id the property id you want information about. #' @param token credentials for access to Google using OAuth. `authorize("google")` #' @importFrom httr config accept_json content @@ -139,8 +134,11 @@ get_ga_properties <- function(account_id, token = NULL) { #' @examples \dontrun{ #' #' authorize("google") +#' accounts <- get_ga_user() +#' properties_list <- get_ga_properties(account_id = accounts$id[1]) +#' property_id <- gsub("properties\\/", "", properties_list$name[1]) #' -#' property_info <- get_ga_property_info(property_id = "422558989") +#' property_info <- get_ga_property_info(property_id = property_id) #' } get_ga_property_info <- function(property_id, token = NULL) { @@ -162,8 +160,7 @@ get_ga_property_info <- function(property_id, token = NULL) { #' Get metadata associated Google Analytics property #' @description This is a function to get the Google Analytics accounts that this user has access to. -#' The scope it uses is the `See and download your Google Analytics data.` -#' If you don't check this box on the OAuth screen, this function will not work. +#' The scopes it uses is the `See and download your Google Analytics data` and `View and manage your Google Analytics data` If you don't check these boxes on the OAuth screen this won't work. #' @param property_id a GA property. Looks like '123456789' Can be obtained from running `get_ga_properties()` #' @param token credentials for access to Google using OAuth. `authorize("google")` #' @importFrom httr config accept_json content @@ -202,8 +199,7 @@ get_ga_metadata <- function(property_id, token = NULL) { #' Get stats for an associated Google Analytics property #' @description This is a function to get the Google Analytics accounts that this user has access to. -#' The scope it uses is the `See and download your Google Analytics data.` -#' If you don't check this box on the OAuth screen, this function will not work. +#' The scopes it uses is the `See and download your Google Analytics data` and `View and manage your Google Analytics data` If you don't check these boxes on the OAuth screen this won't work. #' @param token credentials for access to Google using OAuth. `authorize("google")` #' @param property_id a GA property. Looks like '123456789' Can be obtained from running `get_ga_properties()` #' @param token credentials for access to Google using OAuth. `authorize("google")` @@ -316,7 +312,7 @@ link_clicks <- function() { } #' Get all metrics for all properties associated with an account -#' @description This is a function to gets metrics and dimensions for all properties associated with an account. The scope it uses is the `See and download your Google Analytics data.` If you don't check this box on the OAuth screen. +#' @description This is a function to gets metrics and dimensions for all properties associated with an account. The scopes it uses is the `See and download your Google Analytics data` and `View and manage your Google Analytics data` If you don't check these boxes on the OAuth screen this won't work. #' @param account_id the account id that you'd like to retrieve stats for all properties associated with it. #' @param property_ids A vector of property ids you'd like to retrieve metrics for. #' @param token credentials for access to Google using OAuth. `authorize("google")` @@ -330,9 +326,12 @@ link_clicks <- function() { #' authorize("google") #' accounts <- get_ga_user() #' +#' properties_list <- get_ga_properties(account_id = accounts$id[1]) +#' property_ids <- gsub("properties/", "", properties_list$name[1:3]) +#' #' all_properties <- get_multiple_ga_metrics(account_id = accounts$id) #' -#' some_properties <- get_multiple_ga_metrics(property_ids = c("422671031", "422558989")) +#' some_properties <- get_multiple_ga_metrics(property_ids = property_ids) #' #' } get_multiple_ga_metrics <- function(account_id = NULL, property_ids = NULL, token = NULL, dataformat = "dataframe", From 7794cec70003bfd159a65a97924078d592f71365 Mon Sep 17 00:00:00 2001 From: cansavvy Date: Tue, 5 Mar 2024 13:02:33 -0500 Subject: [PATCH 12/13] Update scope --- R/google-analytics.R | 15 ++++++++------- R/scopes.R | 1 - 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/R/google-analytics.R b/R/google-analytics.R index 1d7f0df..5782376 100644 --- a/R/google-analytics.R +++ b/R/google-analytics.R @@ -2,7 +2,7 @@ #' Handler for API requests from Google Analytics #' @description This is a function that handles requests from Google Analytics. -#' The scopes it uses is the `See and download your Google Analytics data` and `View and manage your Google Analytics data` If you don't check these boxes on the OAuth screen this won't work. +#' The scope it uses is the `See and download your Google Analytics data` If you don't this check this box on the OAuth screen this won't work. #' @param url The endpoint URL for the request #' @param token credentials for access to Google using OAuth. `authorize("google")` #' @param body_params The body parameters for the request @@ -55,7 +55,7 @@ request_ga <- function(token, url, query = NULL, body_params = NULL, request_typ #' Get Google Analytics Accounts #' @description This is a function to get the Google Analytics accounts that -#' this user has access to. The scopes it uses is the `See and download your Google Analytics data` and `View and manage your Google Analytics data` If you don't check these boxes on the OAuth screen this won't work. +#' this user has access to. The scope it uses is the `See and download your Google Analytics data` If you don't this check this box on the OAuth screen this won't work. #' @param request_type Is this a GET or a POST? #' @param token credentials for access to Google using OAuth. `authorize("google")` #' @importFrom httr config accept_json content @@ -88,7 +88,7 @@ get_ga_user <- function(token = NULL, request_type = "GET") { } #' Get all property ids for all Google Analytics associated with an account id -#' @description The scopes it uses is the `See and download your Google Analytics data` and `View and manage your Google Analytics data` If you don't check these boxes on the OAuth screen this won't work. +#' @description This retrieves all the property ids associated with a Google Analytics Account. The scope it uses is the `See and download your Google Analytics data` If you don't this check this box on the OAuth screen this won't work. #' @param account_id the account id of the properties you are trying to retrieve #' @param token credentials for access to Google using OAuth. `authorize("google")` #' @importFrom httr config accept_json content @@ -123,7 +123,7 @@ get_ga_properties <- function(account_id, token = NULL) { #' Get all property information for a particular property id #' @description This is a function to get the Google Analytics accounts that this user has access to. -#' The scopes it uses is the `See and download your Google Analytics data` and `View and manage your Google Analytics data` If you don't check these boxes on the OAuth screen this won't work. +#' The scope it uses is the `See and download your Google Analytics data` If you don't this check this box on the OAuth screen this won't work. #' @param property_id the property id you want information about. #' @param token credentials for access to Google using OAuth. `authorize("google")` #' @importFrom httr config accept_json content @@ -160,7 +160,7 @@ get_ga_property_info <- function(property_id, token = NULL) { #' Get metadata associated Google Analytics property #' @description This is a function to get the Google Analytics accounts that this user has access to. -#' The scopes it uses is the `See and download your Google Analytics data` and `View and manage your Google Analytics data` If you don't check these boxes on the OAuth screen this won't work. +#' The scope it uses is the `See and download your Google Analytics data` If you don't this check this box on the OAuth screen this won't work. #' @param property_id a GA property. Looks like '123456789' Can be obtained from running `get_ga_properties()` #' @param token credentials for access to Google using OAuth. `authorize("google")` #' @importFrom httr config accept_json content @@ -199,7 +199,7 @@ get_ga_metadata <- function(property_id, token = NULL) { #' Get stats for an associated Google Analytics property #' @description This is a function to get the Google Analytics accounts that this user has access to. -#' The scopes it uses is the `See and download your Google Analytics data` and `View and manage your Google Analytics data` If you don't check these boxes on the OAuth screen this won't work. +#' The scope it uses is the `See and download your Google Analytics data` If you don't this check this box on the OAuth screen this won't work. #' @param token credentials for access to Google using OAuth. `authorize("google")` #' @param property_id a GA property. Looks like '123456789' Can be obtained from running `get_ga_properties()` #' @param token credentials for access to Google using OAuth. `authorize("google")` @@ -312,7 +312,8 @@ link_clicks <- function() { } #' Get all metrics for all properties associated with an account -#' @description This is a function to gets metrics and dimensions for all properties associated with an account. The scopes it uses is the `See and download your Google Analytics data` and `View and manage your Google Analytics data` If you don't check these boxes on the OAuth screen this won't work. +#' @description This is a function to gets metrics and dimensions for all properties associated with an account. +#' The scope it uses is the `See and download your Google Analytics data` If you don't this check this box on the OAuth screen this won't work. #' @param account_id the account id that you'd like to retrieve stats for all properties associated with it. #' @param property_ids A vector of property ids you'd like to retrieve metrics for. #' @param token credentials for access to Google using OAuth. `authorize("google")` diff --git a/R/scopes.R b/R/scopes.R index 72b4399..c9b77a4 100644 --- a/R/scopes.R +++ b/R/scopes.R @@ -9,7 +9,6 @@ find_scopes <- function(app_name) { "https://www.googleapis.com/auth/drive.metadata.readonly", "https://www.googleapis.com/auth/forms.body.readonly", "https://www.googleapis.com/auth/forms.responses.readonly", - "https://www.googleapis.com/auth/analytics", "https://www.googleapis.com/auth/analytics.readonly", "https://www.googleapis.com/auth/spreadsheets", "https://www.googleapis.com/auth/youtube.readonly" From b4e751dd7331ab8d9443a23faba3c81feccc9323 Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Tue, 5 Mar 2024 13:19:32 -0500 Subject: [PATCH 13/13] Fix accounts ref --- R/google-analytics.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/google-analytics.R b/R/google-analytics.R index 5782376..8816f47 100644 --- a/R/google-analytics.R +++ b/R/google-analytics.R @@ -328,9 +328,9 @@ link_clicks <- function() { #' accounts <- get_ga_user() #' #' properties_list <- get_ga_properties(account_id = accounts$id[1]) -#' property_ids <- gsub("properties/", "", properties_list$name[1:3]) +#' property_ids <- gsub("properties/", "", properties_list$name[1:2]) #' -#' all_properties <- get_multiple_ga_metrics(account_id = accounts$id) +#' all_properties <- get_multiple_ga_metrics(account_id = accounts$id[1]) #' #' some_properties <- get_multiple_ga_metrics(property_ids = property_ids) #'