From 51f26e8542c5ed817026b22db68f668bd88e66e1 Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Thu, 16 Nov 2023 11:00:53 -0500 Subject: [PATCH 1/7] LISTS -> dF for google analytics --- R/auth.R | 1 - R/calendly.R | 3 -- R/github.R | 5 +- R/google-analytics.R | 126 +++++++++++++++++++++++++++++++++++++------ 4 files changed, 112 insertions(+), 23 deletions(-) diff --git a/R/auth.R b/R/auth.R index f850796..d9e8180 100644 --- a/R/auth.R +++ b/R/auth.R @@ -138,7 +138,6 @@ delete_creds <- function(app_name = "all") { #' } #' auth_from_secret <- function(app_name, token, access_token, refresh_token, cache = FALSE) { - if (app_name %in% c("github", "calendly") && is.null(token)) { stop("For GitHub and Calendly, token cannot be NULL") } diff --git a/R/calendly.R b/R/calendly.R index 7a40311..571534d 100644 --- a/R/calendly.R +++ b/R/calendly.R @@ -55,7 +55,6 @@ calendly_get <- function(url, token = NULL, user = NULL, count = NULL, page_toke #' get_calendly_user() #' } get_calendly_user <- function(token = NULL) { - if (is.null(token)) { # Get auth token token <- get_token(app_name = "calendly") @@ -84,11 +83,9 @@ get_calendly_user <- function(token = NULL) { #' authorize("calendly") #' user <- get_calendly_user() #' list_calendly_events(user = user$resource$uri) -#' #' } #' list_calendly_events <- function(token = NULL, user, count = 100) { - if (is.null(token)) { # Get auth token token <- get_token(app_name = "calendly") diff --git a/R/github.R b/R/github.R index 54b9fdf..8b4a196 100644 --- a/R/github.R +++ b/R/github.R @@ -49,7 +49,6 @@ get_github_user <- function(token) { #' get_github_user() #' } get_github_user <- function(token) { - if (is.null(token)) { # Get auth token token <- get_token(app_name = "github") @@ -77,8 +76,7 @@ get_github_user <- function(token) { #' authorize("github") #' get_github_repo() #' } -get_github_repo <- function(token, owner, repo) { - +get_github_repo <- function(token, owner, repo) { if (is.null(token)) { # Get auth token token <- get_token(app_name = "github") @@ -129,7 +127,6 @@ get_github_repo <- function(token, owner, repo) { #' get_github_user() #' } get_github_metrics <- function(token, owner, repo) { - if (is.null(token)) { # Get auth token token <- get_token(app_name = "github") diff --git a/R/google-analytics.R b/R/google-analytics.R index f5bc761..2e8ed59 100644 --- a/R/google-analytics.R +++ b/R/google-analytics.R @@ -14,7 +14,6 @@ #' @importFrom assertthat assert_that is.string #' @export request_ga <- function(token, url, query = NULL, body_params = NULL, type) { - if (is.null(token)) { # Get auth token token <- get_token(app_name = "google") @@ -137,12 +136,12 @@ get_ga_metadata <- function(property_id) { return(results) } -#' Get metrics for an associated google analytics property +#' 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 #' @param property_id a GA property. Looks like '123456789' Can be obtained from running `get_ga_properties()` #' @param start_date YYYY-MM-DD format of what metric you'd like to collect metrics from to start. Default is the earliest date Google Analytics were collected. #' @param end_date YYYY-MM-DD format of what metric you'd like to collect metrics from to end. Default is today. -#' +#' @param type What type of stats would you like to collect? Options are "metrics", "dimensions" or "link_clicks". #' @importFrom httr config accept_json content #' @importFrom jsonlite fromJSON #' @importFrom assertthat assert_that is.string @@ -173,19 +172,30 @@ get_ga_stats <- function(property_id, start_date = "2015-08-14", end_date = NULL if (type == "metrics") { body_params <- list( dateRanges = list( - "startDate" = start_date, - "endDate" = end_date), + "startDate" = start_date, + "endDate" = end_date + ), metrics = metrics_list() ) } if (type == "dimensions") { body_params <- list( dateRanges = list( - "startDate" = start_date, - "endDate" = end_date), + "startDate" = start_date, + "endDate" = end_date + ), dimensions = dimensions_list() ) } + if (type == "link_clicks") { + body_params <- list( + dateRanges = list( + "startDate" = start_date, + "endDate" = end_date + ), + dimensions = link_clicks() + ) + } results <- request_ga( token = token, @@ -219,16 +229,21 @@ dimensions_list <- function() { list("name" = "month"), list("name" = "year"), list("name" = "country"), - list("name" = "linkUrl"), list("name" = "fullPageUrl") ) return(dimensions) } +link_clicks <- function() { + list("name" = "linkUrl") +} + #' 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 #' @param account_id the account id of the properties you are trying to retrieve +#' @param format 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". +#' @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 #' @examples \dontrun{ #' @@ -237,25 +252,106 @@ dimensions_list <- function() { #' #' stats_list <- all_ga_metrics(account_id = accounts$id[5]) #' } -all_ga_metrics <- function(account_id) { - +all_ga_metrics <- function(account_id, format = "dataframe") { properties_list <- get_ga_properties(account_id = account_id) # This is the code for one website/property property_names <- gsub("properties/", "", properties_list$properties$name) # Now loop through all the properties - all_google_analytics_data <- lapply(property_names, function(property_id) { - + all_google_analytics_metrics <- lapply(property_names, function(property_id) { + # Be vocal about it + message(paste("Retrieving", property_id, "metrics")) + # Get the stats metrics <- get_ga_stats(property_id, type = "metrics") + return(metrics) + }) + + # Save the names + names(all_google_analytics_metrics) <- properties_list$properties$displayName + + # Now loop through all the properties + all_google_analytics_dimensions <- lapply(property_names, function(property_id) { + # Be vocal about it + message(paste("Retrieving", property_id, "dimensions")) + # Get the stats dimensions <- get_ga_stats(property_id, type = "dimensions") - return(list(metrics = metrics, dimensions = dimensions)) + return(dimensions) }) # Save the names - names(all_google_analytics_data) <- properties_list$properties$displayName + names(all_google_analytics_dimensions) <- properties_list$properties$displayName + + # Now loop through all the properties + all_google_analytics_links <- lapply(property_names, function(property_id) { + # Be vocal about it + message(paste("Retrieving", property_id, "link clicks")) + # Get the stats + links <- get_ga_stats(property_id, type = "link_clicks") + + return(links) + }) + + # Save the names + names(all_google_analytics_links) <- properties_list$properties$displayName + + if (format == "dataframe") { + all_google_analytics_metrics <- clean_metric_data(all_google_analytics_metrics) + all_google_analytics_dimensions <- clean_dimension_data(all_google_analytics_dimensions) + all_google_analytics_links <- clean_link_data(all_google_analytics_links) + } + + + return(list( + metrics = all_google_analytics_metrics, + dimensions = all_google_analytics_dimensions, + link_clicks = all_google_analytics_links + )) +} + +#' Handle Google Analytics Lists +#' @description This is a function to gets metrics and dimensions for all properties associated with an account +#' @param account_id the account id of the properties you are trying to retrieve +#' @export + +clean_metric_data <- function(metrics) { + stat_names <- metrics[[1]]$metricHeaders$name + + clean_df <- purrr::map(metrics, "rows") %>% + dplyr::bind_rows(.id = "website") %>% + tidyr::separate(col = "metricValues", sep = ",", into = stat_names) %>% + dplyr::mutate_all(~ gsub("list\\(value = c\\(|\\)\\)|\"|", "", .)) %>% + dplyr::mutate_at(stat_names, as.numeric) + + return(clean_df) +} + +clean_dimension_data <- function(dimensions) { + all_website_dims <- lapply(dimensions, wrangle_dimensions) %>% + dplyr::bind_rows(.id = "website") + + return(all_website_dims) +} + +clean_link_data <- function(link_clicks) { + + all_website_links <- lapply(link_clicks, wrangle_dimensions) %>% + dplyr::bind_rows(.id = "website") - return(all_google_analytics_data) + return(all_website_links) } +wrangle_dimensions <- function(dims_for_website) { + stat_names <- dims_for_website$dimensionHeaders + + values_list <- lapply(dims_for_website$rows$dimensionValues, t) + + clean_df <- lapply(values_list, as.data.frame) %>% + dplyr::bind_rows() + + colnames(clean_df) <- dims_for_website$dimensionHeaders$name + rownames(clean_df) <- NULL + + return(clean_df) +} From 12a04596034172ec9754cb6c182cbedc7867f082 Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Thu, 16 Nov 2023 11:07:27 -0500 Subject: [PATCH 2/7] Update docs --- NAMESPACE | 1 + man/all_ga_metrics.Rd | 7 ++++++- man/clean_metric_data.Rd | 14 ++++++++++++++ man/get_ga_stats.Rd | 4 +++- man/list_calendly_events.Rd | 1 - 5 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 man/clean_metric_data.Rd diff --git a/NAMESPACE b/NAMESPACE index 54f8cf1..8ba9bb0 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -4,6 +4,7 @@ export(all_ga_metrics) export(auth_from_secret) export(authorize) export(calendly_get) +export(clean_metric_data) export(delete_creds) export(get_calendly_user) export(get_ga_metadata) diff --git a/man/all_ga_metrics.Rd b/man/all_ga_metrics.Rd index 1f3c080..bca050f 100644 --- a/man/all_ga_metrics.Rd +++ b/man/all_ga_metrics.Rd @@ -4,10 +4,15 @@ \alias{all_ga_metrics} \title{Get all metrics for all properties associated with an account} \usage{ -all_ga_metrics(account_id) +all_ga_metrics(account_id, format = "dataframe") } \arguments{ \item{account_id}{the account id of the properties you are trying to retrieve} + +\item{format}{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".} +} +\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 } \description{ This is a function to gets metrics and dimensions for all properties associated with an account diff --git a/man/clean_metric_data.Rd b/man/clean_metric_data.Rd new file mode 100644 index 0000000..1604d9e --- /dev/null +++ b/man/clean_metric_data.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/google-analytics.R +\name{clean_metric_data} +\alias{clean_metric_data} +\title{Handle Google Analytics Lists} +\usage{ +clean_metric_data(metrics) +} +\arguments{ +\item{account_id}{the account id of the properties you are trying to retrieve} +} +\description{ +This is a function to gets metrics and dimensions for all properties associated with an account +} diff --git a/man/get_ga_stats.Rd b/man/get_ga_stats.Rd index 1392fc6..9735c58 100644 --- a/man/get_ga_stats.Rd +++ b/man/get_ga_stats.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/google-analytics.R \name{get_ga_stats} \alias{get_ga_stats} -\title{Get metrics for an associated google analytics property} +\title{Get stats for an associated google analytics property} \usage{ get_ga_stats( property_id, @@ -17,6 +17,8 @@ get_ga_stats( \item{start_date}{YYYY-MM-DD format of what metric you'd like to collect metrics from to start. Default is the earliest date Google Analytics were collected.} \item{end_date}{YYYY-MM-DD format of what metric you'd like to collect metrics from to end. Default is today.} + +\item{type}{What type of stats would you like to collect? Options are "metrics", "dimensions" or "link_clicks".} } \description{ This is a function to get the Google Analytics accounts that this user has access to diff --git a/man/list_calendly_events.Rd b/man/list_calendly_events.Rd index 16de433..1a5085b 100644 --- a/man/list_calendly_events.Rd +++ b/man/list_calendly_events.Rd @@ -25,7 +25,6 @@ This is a function to get a list of scheduled events from a calendly user. authorize("calendly") user <- get_calendly_user() list_calendly_events(user = user$resource$uri) - } } From 730055ba4a1de7d632167a9779c3e4b7a833df3b Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Tue, 21 Nov 2023 10:38:19 -0500 Subject: [PATCH 3/7] Add to gitignore --- .gitignore | 1 + R/google-analytics.R | 11 +++++++---- R/token-handlers.R | 29 ++++++++++++++++++++++++++++- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 9da072c..00dd8f5 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ .secrets/* .httr-oauth docs +inst/extdata/cached-secrets/* \ No newline at end of file diff --git a/R/google-analytics.R b/R/google-analytics.R index a03f788..9f93637 100644 --- a/R/google-analytics.R +++ b/R/google-analytics.R @@ -63,14 +63,17 @@ request_ga <- function(token, url, query = NULL, body_params = NULL, type) { #' authorize("google") #' get_ga_user() #' } -get_ga_user <- function() { - # Get auth token - token <- get_token(app_name = "google") +get_ga_user <- function(token = NULL, request_type = "GET") { + + if (is.null(token)) { + # Get auth token + token <- get_token(app_name = "google") + } results <- request_ga( token = token, url = "https://analytics.googleapis.com/analytics/v3/management/accountSummaries", - type = "GET" + request_type = request_type ) return(results$items) diff --git a/R/token-handlers.R b/R/token-handlers.R index 3d45ce9..695f448 100644 --- a/R/token-handlers.R +++ b/R/token-handlers.R @@ -23,6 +23,21 @@ get_token <- function(app_name) { if (is.null(.Env$metricminer_tokens[[app_name]])) { .Env$metricminer_tokens[[app_name]] <- get_cached_token(app_name) } + # Attempt to grab a cached credential + if (is.null(.Env$metricminer_tokens[[app_name]])) { + .Env$metricminer_tokens[[app_name]] <- get_cached_token(app_name) + + # only print this message if we are successful + if (!is.null(.Env$metricminer_tokens[[app_name]])) message("Using user-supplied cached token using authorize(\"", app_name, "\")") + } + + # If we don't get authorization, check if we said it was required or not + if (is.null(.Env$metricminer_tokens[[app_name]])) { + warning("No token found. Please run `authorize()` to supply token.") + if (!try) { + stop("Authorization required for the called function. Quitting.") + } + } return(invisible(.Env$metricminer_tokens[[app_name]])) } @@ -30,7 +45,19 @@ get_token <- function(app_name) { get_cached_token <- function(app_name) { if (app_name == "calendly") token <- getOption("calendly_api") if (app_name == "github") token <- getOption("github_api") - if (app_name == "google") token <- try(readRDS(".httr-oauth"), silent = TRUE) + if (app_name == "google") token <- try(readRDS(".httr-oauth")[[1]], silent = TRUE) + return(token) +} + +# A function that attempts to grab cached credentials +get_cached_token <- function(app_name) { + if (app_name == "calendly") token <- try(readRDS(file.path(cache_secrets_folder(), "calendly.RDS")), silent = TRUE) + if (app_name == "github") token <- try(readRDS(file.path(cache_secrets_folder(), "github.RDS")), silent = TRUE) + if (app_name == "google") token <- try(readRDS(".httr-oauth")[[1]], silent = TRUE) + + if (grepl("Error", token[1])) { + token <- NULL + } return(token) } From e934395b69fe099817deb1f3ff8e67d0a301cbb0 Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Tue, 21 Nov 2023 10:39:20 -0500 Subject: [PATCH 4/7] Updates --- R/google-analytics.R | 162 +++++++++---------------------------------- 1 file changed, 34 insertions(+), 128 deletions(-) diff --git a/R/google-analytics.R b/R/google-analytics.R index 9f93637..e8b1533 100644 --- a/R/google-analytics.R +++ b/R/google-analytics.R @@ -8,22 +8,23 @@ #' @param token credentials for access to Google using OAuth. `authorize("google")` #' @param body_params The body parameters for the request #' @param query A list to be passed to query -#' @param type Is this a GET or a POST? +#' @param request_type Is this a GET or a POST? #' @importFrom httr config accept_json content #' @importFrom jsonlite fromJSON #' @importFrom assertthat assert_that is.string #' @export -request_ga <- function(token, url, query = NULL, body_params = NULL, type) { +request_ga <- function(token, url, query = NULL, body_params = NULL, request_type) { + if (is.null(token)) { # Get auth token token <- get_token(app_name = "google") } config <- httr::config(token = token) - if (type == "GET") { + if (request_type == "GET") { result <- httr::GET( url = url, - body = body, + body = body_params, query = query, config = config, httr::accept_json(), @@ -31,7 +32,7 @@ request_ga <- function(token, url, query = NULL, body_params = NULL, type) { ) } - if (type == "POST") { + if (request_type == "POST") { result <- httr::POST( url = url, body = body_params, @@ -54,6 +55,7 @@ request_ga <- function(token, url, query = NULL, body_params = NULL, type) { #' Get Google Analytics Accounts #' @description This is a function to get the Google Analytics accounts that this user has access to +#' @param request_type Is this a GET or a POST? #' @importFrom httr config accept_json content #' @importFrom jsonlite fromJSON #' @importFrom assertthat assert_that is.string @@ -91,7 +93,7 @@ get_ga_user <- function(token = NULL, request_type = "GET") { #' authorize("google") #' accounts <- get_ga_user() #' -#' properties_list <- get_ga_properties(account_id = accounts$id[1]) +#' properties_list <- get_ga_properties(account_id = 209776907) #' } get_ga_properties <- function(account_id) { # Get auth token @@ -101,7 +103,7 @@ get_ga_properties <- function(account_id) { token = token, url = "https://analyticsadmin.googleapis.com/v1alpha/properties", query = list(filter = paste0("parent:accounts/", account_id)), - type = "GET" + request_type = "GET" ) return(results) @@ -135,18 +137,19 @@ get_ga_metadata <- function(property_id) { results <- request_ga( token = token, url = url, - type = "GET" + request_type = "GET" ) return(results) } -#' Get stats for an associated Google Analytics property +#' Get metrics for an associated Google Analytics property #' @description This is a function to get the Google Analytics accounts that this user has access to #' @param property_id a GA property. Looks like '123456789' Can be obtained from running `get_ga_properties()` #' @param start_date YYYY-MM-DD format of what metric you'd like to collect metrics from to start. Default is the earliest date Google Analytics were collected. #' @param end_date YYYY-MM-DD format of what metric you'd like to collect metrics from to end. Default is today. -#' @param type What type of stats would you like to collect? Options are "metrics", "dimensions" or "link_clicks". +#' @param body_params The body parameters for the request +#' @param stats_type Do you want to retrieve metrics or dimensions? #' @importFrom httr config accept_json content #' @importFrom jsonlite fromJSON #' @importFrom assertthat assert_that is.string @@ -160,10 +163,10 @@ get_ga_metadata <- function(property_id) { #' properties_list <- get_ga_properties(account_id = accounts$id[1]) #' #' property_id <- gsub("properties/", "", properties_list$properties$name[1]) -#' metrics <- get_ga_stats(property_id, type = "metrics") -#' dimensions <- get_ga_stats(property_id, type = "dimensions") +#' metrics <- get_ga_stats(property_id, stats_type = "metrics") +#' dimensions <- get_ga_stats(property_id, stats_type = "dimensions") #' } -get_ga_stats <- function(property_id, start_date = "2015-08-14", end_date = NULL, type = "metrics") { +get_ga_stats <- function(property_id, start_date = "2015-08-14", body_params = NULL, end_date = NULL, stats_type = "metrics") { # If no end_date is set, use today end_date <- ifelse(is.null(end_date), as.character(lubridate::today()), end_date) @@ -174,39 +177,28 @@ get_ga_stats <- function(property_id, start_date = "2015-08-14", end_date = NULL # Get auth token token <- get_token(app_name = "google") - if (type == "metrics") { + if (stats_type == "metrics") { body_params <- list( dateRanges = list( - "startDate" = start_date, - "endDate" = end_date - ), + "startDate" = start_date, + "endDate" = end_date), metrics = metrics_list() ) } - if (type == "dimensions") { + if (stats_type == "dimensions") { body_params <- list( dateRanges = list( - "startDate" = start_date, - "endDate" = end_date - ), + "startDate" = start_date, + "endDate" = end_date), dimensions = dimensions_list() ) } - if (type == "link_clicks") { - body_params <- list( - dateRanges = list( - "startDate" = start_date, - "endDate" = end_date - ), - dimensions = link_clicks() - ) - } results <- request_ga( token = token, url = url, - body = body_params, - type = "POST" + body_params = body_params, + request_type = "POST" ) return(results) @@ -234,129 +226,43 @@ dimensions_list <- function() { list("name" = "month"), list("name" = "year"), list("name" = "country"), + list("name" = "linkUrl"), list("name" = "fullPageUrl") ) return(dimensions) } -link_clicks <- function() { - list("name" = "linkUrl") -} - #' 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 #' @param account_id the account id of the properties you are trying to retrieve -#' @param format 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". -#' @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 #' @examples \dontrun{ #' #' authorize("google") #' accounts <- get_ga_user() #' -#' stats_list <- all_ga_metrics(account_id = accounts$id[5]) +#' stats_list <- all_ga_metrics(account_id = 209776907) #' } -all_ga_metrics <- function(account_id, format = "dataframe") { +all_ga_metrics <- function(account_id) { + properties_list <- get_ga_properties(account_id = account_id) # This is the code for one website/property property_names <- gsub("properties/", "", properties_list$properties$name) # Now loop through all the properties - all_google_analytics_metrics <- lapply(property_names, function(property_id) { - # Be vocal about it - message(paste("Retrieving", property_id, "metrics")) - # Get the stats - metrics <- get_ga_stats(property_id, type = "metrics") - return(metrics) - }) - - # Save the names - names(all_google_analytics_metrics) <- properties_list$properties$displayName + all_google_analytics_data <- lapply(property_names, function(property_id) { - # Now loop through all the properties - all_google_analytics_dimensions <- lapply(property_names, function(property_id) { - # Be vocal about it - message(paste("Retrieving", property_id, "dimensions")) - # Get the stats - dimensions <- get_ga_stats(property_id, type = "dimensions") + metrics <- get_ga_stats(property_id, stats_type = "metrics") + dimensions <- get_ga_stats(property_id, stats_type = "dimensions") - return(dimensions) + return(list(metrics = metrics, dimensions = dimensions)) }) # Save the names - names(all_google_analytics_dimensions) <- properties_list$properties$displayName - - # Now loop through all the properties - all_google_analytics_links <- lapply(property_names, function(property_id) { - # Be vocal about it - message(paste("Retrieving", property_id, "link clicks")) - # Get the stats - links <- get_ga_stats(property_id, type = "link_clicks") - - return(links) - }) - - # Save the names - names(all_google_analytics_links) <- properties_list$properties$displayName - - if (format == "dataframe") { - all_google_analytics_metrics <- clean_metric_data(all_google_analytics_metrics) - all_google_analytics_dimensions <- clean_dimension_data(all_google_analytics_dimensions) - all_google_analytics_links <- clean_link_data(all_google_analytics_links) - } - - - return(list( - metrics = all_google_analytics_metrics, - dimensions = all_google_analytics_dimensions, - link_clicks = all_google_analytics_links - )) -} - -#' Handle Google Analytics Lists -#' @description This is a function to gets metrics and dimensions for all properties associated with an account -#' @param account_id the account id of the properties you are trying to retrieve -#' @export - -clean_metric_data <- function(metrics) { - stat_names <- metrics[[1]]$metricHeaders$name - - clean_df <- purrr::map(metrics, "rows") %>% - dplyr::bind_rows(.id = "website") %>% - tidyr::separate(col = "metricValues", sep = ",", into = stat_names) %>% - dplyr::mutate_all(~ gsub("list\\(value = c\\(|\\)\\)|\"|", "", .)) %>% - dplyr::mutate_at(stat_names, as.numeric) - - return(clean_df) -} - -clean_dimension_data <- function(dimensions) { - all_website_dims <- lapply(dimensions, wrangle_dimensions) %>% - dplyr::bind_rows(.id = "website") - - return(all_website_dims) -} - -clean_link_data <- function(link_clicks) { - - all_website_links <- lapply(link_clicks, wrangle_dimensions) %>% - dplyr::bind_rows(.id = "website") + names(all_google_analytics_data) <- properties_list$properties$displayName - return(all_website_links) + return(all_google_analytics_data) } -wrangle_dimensions <- function(dims_for_website) { - stat_names <- dims_for_website$dimensionHeaders - - values_list <- lapply(dims_for_website$rows$dimensionValues, t) - - clean_df <- lapply(values_list, as.data.frame) %>% - dplyr::bind_rows() - - colnames(clean_df) <- dims_for_website$dimensionHeaders$name - rownames(clean_df) <- NULL - - return(clean_df) -} From 5954351cfe0822e384ba910972e451e983b6a019 Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Tue, 21 Nov 2023 10:50:26 -0500 Subject: [PATCH 5/7] Fixes! --- R/google-analytics.R | 145 ++++++++++++++++++++++++++++++++++++------- R/token-handlers.R | 2 +- 2 files changed, 122 insertions(+), 25 deletions(-) diff --git a/R/google-analytics.R b/R/google-analytics.R index e8b1533..2172696 100644 --- a/R/google-analytics.R +++ b/R/google-analytics.R @@ -1,6 +1,6 @@ # 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 @@ -143,13 +143,12 @@ get_ga_metadata <- function(property_id) { return(results) } -#' Get metrics for an associated Google Analytics property +#' 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 #' @param property_id a GA property. Looks like '123456789' Can be obtained from running `get_ga_properties()` #' @param start_date YYYY-MM-DD format of what metric you'd like to collect metrics from to start. Default is the earliest date Google Analytics were collected. #' @param end_date YYYY-MM-DD format of what metric you'd like to collect metrics from to end. Default is today. -#' @param body_params The body parameters for the request -#' @param stats_type Do you want to retrieve metrics or dimensions? +#' @param type What type of stats would you like to collect? Options are "metrics", "dimensions" or "link_clicks". #' @importFrom httr config accept_json content #' @importFrom jsonlite fromJSON #' @importFrom assertthat assert_that is.string @@ -163,10 +162,10 @@ get_ga_metadata <- function(property_id) { #' properties_list <- get_ga_properties(account_id = accounts$id[1]) #' #' property_id <- gsub("properties/", "", properties_list$properties$name[1]) -#' metrics <- get_ga_stats(property_id, stats_type = "metrics") -#' dimensions <- get_ga_stats(property_id, stats_type = "dimensions") +#' metrics <- get_ga_stats(property_id, type = "metrics") +#' dimensions <- get_ga_stats(property_id, type = "dimensions") #' } -get_ga_stats <- function(property_id, start_date = "2015-08-14", body_params = NULL, end_date = NULL, stats_type = "metrics") { +get_ga_stats <- function(property_id, start_date = "2015-08-14", end_date = NULL, type = "metrics") { # If no end_date is set, use today end_date <- ifelse(is.null(end_date), as.character(lubridate::today()), end_date) @@ -177,27 +176,38 @@ get_ga_stats <- function(property_id, start_date = "2015-08-14", body_params = N # Get auth token token <- get_token(app_name = "google") - if (stats_type == "metrics") { + if (type == "metrics") { body_params <- list( dateRanges = list( - "startDate" = start_date, - "endDate" = end_date), + "startDate" = start_date, + "endDate" = end_date + ), metrics = metrics_list() ) } - if (stats_type == "dimensions") { + if (type == "dimensions") { body_params <- list( dateRanges = list( - "startDate" = start_date, - "endDate" = end_date), + "startDate" = start_date, + "endDate" = end_date + ), dimensions = dimensions_list() ) } + if (type == "link_clicks") { + body_params <- list( + dateRanges = list( + "startDate" = start_date, + "endDate" = end_date + ), + dimensions = link_clicks() + ) + } results <- request_ga( token = token, url = url, - body_params = body_params, + body = body_params, request_type = "POST" ) @@ -226,43 +236,130 @@ dimensions_list <- function() { list("name" = "month"), list("name" = "year"), list("name" = "country"), - list("name" = "linkUrl"), list("name" = "fullPageUrl") ) return(dimensions) } +link_clicks <- function() { + list("name" = "linkUrl") +} + #' 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 #' @param account_id the account id of the properties you are trying to retrieve +#' @param format 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". +#' @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 #' @examples \dontrun{ #' #' authorize("google") #' accounts <- get_ga_user() #' -#' stats_list <- all_ga_metrics(account_id = 209776907) +#' stats_list <- all_ga_metrics(account_id = accounts$id[5]) #' } -all_ga_metrics <- function(account_id) { - +all_ga_metrics <- function(account_id, format = "dataframe") { properties_list <- get_ga_properties(account_id = account_id) # This is the code for one website/property property_names <- gsub("properties/", "", properties_list$properties$name) # Now loop through all the properties - all_google_analytics_data <- lapply(property_names, function(property_id) { + all_google_analytics_metrics <- lapply(property_names, function(property_id) { + # Be vocal about it + message(paste("Retrieving", property_id, "metrics")) + # Get the stats + metrics <- get_ga_stats(property_id, type = "metrics") + return(metrics) + }) - metrics <- get_ga_stats(property_id, stats_type = "metrics") - dimensions <- get_ga_stats(property_id, stats_type = "dimensions") + # Save the names + names(all_google_analytics_metrics) <- properties_list$properties$displayName - return(list(metrics = metrics, dimensions = dimensions)) + # Now loop through all the properties + all_google_analytics_dimensions <- lapply(property_names, function(property_id) { + # Be vocal about it + message(paste("Retrieving", property_id, "dimensions")) + # Get the stats + dimensions <- get_ga_stats(property_id, type = "dimensions") + + return(dimensions) + }) + + # Save the names + names(all_google_analytics_dimensions) <- properties_list$properties$displayName + + # Now loop through all the properties + all_google_analytics_links <- lapply(property_names, function(property_id) { + # Be vocal about it + message(paste("Retrieving", property_id, "link clicks")) + # Get the stats + links <- get_ga_stats(property_id, type = "link_clicks") + + return(links) }) # Save the names - names(all_google_analytics_data) <- properties_list$properties$displayName + names(all_google_analytics_links) <- properties_list$properties$displayName + + if (format == "dataframe") { + all_google_analytics_metrics <- clean_metric_data(all_google_analytics_metrics) + all_google_analytics_dimensions <- clean_dimension_data(all_google_analytics_dimensions) + all_google_analytics_links <- clean_link_data(all_google_analytics_links) + } - return(all_google_analytics_data) + + return(list( + metrics = all_google_analytics_metrics, + dimensions = all_google_analytics_dimensions, + link_clicks = all_google_analytics_links + )) } +#' Handle Google Analytics Lists +#' @description This is a function to gets metrics and dimensions for all properties associated with an account +#' @param account_id the account id of the properties you are trying to retrieve +#' @importFrom dplyr %>% +#' @export + +clean_metric_data <- function(metrics) { + stat_names <- metrics[[1]]$metricHeaders$name + + clean_df <- purrr::map(metrics, "rows") %>% + dplyr::bind_rows(.id = "website") %>% + tidyr::separate(col = "metricValues", sep = ",", into = stat_names) %>% + dplyr::mutate_all(~ gsub("list\\(value = c\\(|\\)\\)|\"|", "", .)) %>% + dplyr::mutate_at(stat_names, as.numeric) + + return(clean_df) +} + +clean_dimension_data <- function(dimensions) { + all_website_dims <- lapply(dimensions, wrangle_dimensions) %>% + dplyr::bind_rows(.id = "website") + + return(all_website_dims) +} + +clean_link_data <- function(link_clicks) { + + all_website_links <- lapply(link_clicks, wrangle_dimensions) %>% + dplyr::bind_rows(.id = "website") + + return(all_website_links) +} + +wrangle_dimensions <- function(dims_for_website) { + stat_names <- dims_for_website$dimensionHeaders + + values_list <- lapply(dims_for_website$rows$dimensionValues, t) + + clean_df <- lapply(values_list, as.data.frame) %>% + dplyr::bind_rows() + + colnames(clean_df) <- dims_for_website$dimensionHeaders$name + rownames(clean_df) <- NULL + + return(clean_df) +} diff --git a/R/token-handlers.R b/R/token-handlers.R index 695f448..f99cc6f 100644 --- a/R/token-handlers.R +++ b/R/token-handlers.R @@ -55,7 +55,7 @@ get_cached_token <- function(app_name) { if (app_name == "github") token <- try(readRDS(file.path(cache_secrets_folder(), "github.RDS")), silent = TRUE) if (app_name == "google") token <- try(readRDS(".httr-oauth")[[1]], silent = TRUE) - if (grepl("Error", token[1])) { + if (class(token)[1] == "try-error") { token <- NULL } From a5f5208d59f587845677fe67ee8cdade81c98a7f Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Tue, 21 Nov 2023 10:51:06 -0500 Subject: [PATCH 6/7] Add saveRDS example --- R/google-analytics.R | 1 + 1 file changed, 1 insertion(+) diff --git a/R/google-analytics.R b/R/google-analytics.R index 2172696..534c75e 100644 --- a/R/google-analytics.R +++ b/R/google-analytics.R @@ -258,6 +258,7 @@ link_clicks <- function() { #' accounts <- get_ga_user() #' #' stats_list <- all_ga_metrics(account_id = accounts$id[5]) +#' saveRDS(stats_list, "itcr_website_data.rds") #' } all_ga_metrics <- function(account_id, format = "dataframe") { properties_list <- get_ga_properties(account_id = account_id) From 1e61d2b5c1d55499b46e989e8f1e2165ec25ec04 Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Tue, 21 Nov 2023 14:56:41 -0500 Subject: [PATCH 7/7] Style and update documentation --- DESCRIPTION | 3 +- NAMESPACE | 4 +++ R/github.R | 60 +++++++++++++++++++--------------------- R/google-analytics.R | 32 ++++++++++----------- R/token-handlers.R | 16 +++++------ R/utils.R | 6 ++-- man/all_ga_metrics.Rd | 1 + man/clean_metric_data.Rd | 6 ++-- man/get_ga_properties.Rd | 2 +- man/get_ga_user.Rd | 4 ++- man/get_repos_metrics.Rd | 2 +- 11 files changed, 71 insertions(+), 65 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index c25e47b..b9f289c 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -20,7 +20,8 @@ Imports: gh, dplyr, lubridate, - purrr + purrr, + tidyr Encoding: UTF-8 RoxygenNote: 7.2.3 LazyData: true diff --git a/NAMESPACE b/NAMESPACE index 8ec9234..bbfb010 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -4,6 +4,7 @@ export(all_ga_metrics) export(auth_from_secret) export(authorize) export(calendly_get) +export(clean_metric_data) export(clean_repo_metrics) export(delete_creds) export(get_calendly_user) @@ -26,6 +27,8 @@ importFrom(assertthat,is.string) importFrom(dplyr,"%>%") importFrom(dplyr,bind_rows) importFrom(dplyr,distinct) +importFrom(dplyr,mutate_all) +importFrom(dplyr,mutate_at) importFrom(gh,gh) importFrom(httr,accept_json) importFrom(httr,config) @@ -36,6 +39,7 @@ importFrom(httr,oauth_endpoints) importFrom(jsonlite,fromJSON) importFrom(lubridate,today) importFrom(purrr,map) +importFrom(tidyr,separate) importFrom(utils,browseURL) importFrom(utils,installed.packages) importFrom(utils,menu) diff --git a/R/github.R b/R/github.R index 518eb9d..ec19744 100644 --- a/R/github.R +++ b/R/github.R @@ -14,7 +14,7 @@ get_github <- function(token = NULL, url) { token <- get_token(app_name = "github") } - # Github api get + # Github api get result <- httr::GET( url, httr::add_headers(Authorization = paste0("Bearer ", token)), @@ -68,7 +68,6 @@ get_github_user <- function(token = NULL) { #' } #' get_repo_list <- function(owner, count = "all", token = NULL) { - if (count == "all") count <- "Inf" if (is.null(token)) { @@ -78,9 +77,9 @@ get_repo_list <- function(owner, count = "all", token = NULL) { } repo_list <- gh::gh("GET /orgs/{owner}/repos", - owner = owner, - .token = token, - .limit = count + owner = owner, + .token = token, + .limit = count ) return(repo_list) @@ -102,7 +101,6 @@ get_repo_list <- function(owner, count = "all", token = NULL) { #' metrics <- get_github_metrics(repo = "fhdsl/metricminer") #' } get_github_metrics <- function(repo, token = NULL, count = "all", data_format = "dataframe") { - if (count == "all") count <- Inf # Split it up @@ -121,13 +119,14 @@ get_github_metrics <- function(repo, token = NULL, count = "all", data_format = ) # Put gh_repo_wrapper inside function gh_repo_wrapper_fn <- function(api_call) { - gh_repo_wrapper(api_call = api_call, - owner = owner, - repo = repo, - token = token, - count = count, - data_format = data_format - ) + gh_repo_wrapper( + api_call = api_call, + owner = owner, + repo = repo, + token = token, + count = count, + data_format = data_format + ) } # Run gh_repo_wrapper_fn() on api_calls # when error occurs, set value to "Not Found" @@ -165,11 +164,9 @@ get_github_metrics <- function(repo, token = NULL, count = "all", data_format = #' #' repo_names <- c("fhdsl/metricminer", "jhudsl/OTTR_Template") #' some_repos_metrics <- get_repos_metrics(repo_names = repo_names) -#' #' } #' get_repos_metrics <- function(owner = NULL, repo_names = NULL, token = NULL, data_format = "dataframe") { - if (is.null(token)) { # Get auth token token <- get_token(app_name = "github", try = TRUE) @@ -182,15 +179,17 @@ get_repos_metrics <- function(owner = NULL, repo_names = NULL, token = NULL, dat count = "all" ) - # Extra repo names from the repo list - repo_names <- unlist(purrr::map(repo_list, "full_name")) + # Extra repo names from the repo list + repo_names <- unlist(purrr::map(repo_list, "full_name")) } # Now run get_github_metrics on all repos repo_metrics <- lapply(repo_names, function(repo) { - get_github_metrics(token = token, - repo = repo, - data_format = data_format) + get_github_metrics( + token = token, + repo = repo, + data_format = data_format + ) }) # Keep names @@ -216,7 +215,6 @@ get_repos_metrics <- function(owner = NULL, repo_names = NULL, token = NULL, dat #' @export #' gh_repo_wrapper <- function(api_call, owner, repo, token = NULL, count = Inf, data_format = "dataframe") { - message(paste("Trying", api_call, "for", repo)) if (is.null(token)) { @@ -234,9 +232,9 @@ gh_repo_wrapper <- function(api_call, owner, repo, token = NULL, count = Inf, da ), silent = TRUE) # Some handlers because not all repos have all stats - if (length(result) == 0) result <- "No results" - if (grepl("404", result[1])) result <- "No results" - if (grepl("Error", result[1])) result <- "No results" + if (length(result) == 0) result <- "No results" + if (grepl("404", result[1])) result <- "No results" + if (grepl("Error", result[1])) result <- "No results" return(result) } @@ -253,14 +251,14 @@ gh_repo_wrapper <- function(api_call, owner, repo, token = NULL, count = Inf, da #' @export #' clean_repo_metrics <- function(repo_name, repo_metric_list) { - if (repo_metric_list$contributors[1] != "No results") { contributors <- - lapply(repo_metric_list$contributors, function(contributor) { - data.frame( - contributor = contributor$login, - num_contributors = contributor$contributions) - }) %>% + lapply(repo_metric_list$contributors, function(contributor) { + data.frame( + contributor = contributor$login, + num_contributors = contributor$contributions + ) + }) %>% dplyr::bind_rows() %>% dplyr::distinct() @@ -280,7 +278,7 @@ clean_repo_metrics <- function(repo_name, repo_metric_list) { metrics <- data.frame( repo_name, num_forks = num_forks, - num_contributors = num_contributors, + num_contributors = num_contributors, total_contributions = total_contributors, num_stars = length(unlist(purrr::map(repo_metric_list$stars, "login"))), health_percentage = ifelse(repo_metric_list$community[1] != "No results", as.numeric(repo_metric_list$community$health_percentage), NA), diff --git a/R/google-analytics.R b/R/google-analytics.R index 366f3a6..ca57f65 100644 --- a/R/google-analytics.R +++ b/R/google-analytics.R @@ -14,7 +14,6 @@ library(magrittr) #' @importFrom assertthat assert_that is.string #' @export request_ga <- function(token, url, query = NULL, body_params = NULL, request_type) { - if (is.null(token)) { # Get auth token token <- get_token(app_name = "google") @@ -23,7 +22,7 @@ request_ga <- function(token, url, query = NULL, body_params = NULL, request_typ if (request_type == "GET") { result <- httr::GET( - url = url, + url = url, body = body_params, query = query, config = config, @@ -34,7 +33,7 @@ request_ga <- function(token, url, query = NULL, body_params = NULL, request_typ if (request_type == "POST") { result <- httr::POST( - url = url, + url = url, body = body_params, query = query, config = config, @@ -56,6 +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 #' @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 #' @importFrom jsonlite fromJSON #' @importFrom assertthat assert_that is.string @@ -66,7 +66,6 @@ request_ga <- function(token, url, query = NULL, body_params = NULL, request_typ #' get_ga_user() #' } get_ga_user <- function(token = NULL, request_type = "GET") { - if (is.null(token)) { # Get auth token token <- get_token(app_name = "google") @@ -143,7 +142,7 @@ get_ga_metadata <- function(property_id) { return(results) } -#' Get stats for an associated google analytics property +#' 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 #' @param property_id a GA property. Looks like '123456789' Can be obtained from running `get_ga_properties()` #' @param start_date YYYY-MM-DD format of what metric you'd like to collect metrics from to start. Default is the earliest date Google Analytics were collected. @@ -195,7 +194,7 @@ get_ga_stats <- function(property_id, start_date = "2015-08-14", body_params = N dimensions = dimensions_list() ) } - if (type == "link_clicks") { + if (stats_type == "link_clicks") { body_params <- list( dateRanges = list( "startDate" = start_date, @@ -272,7 +271,7 @@ all_ga_metrics <- function(account_id, format = "dataframe") { # Be vocal about it message(paste("Retrieving", property_id, "metrics")) # Get the stats - metrics <- get_ga_stats(property_id, type = "metrics") + metrics <- get_ga_stats(property_id, stats_type = "metrics") return(metrics) }) @@ -284,7 +283,7 @@ all_ga_metrics <- function(account_id, format = "dataframe") { # Be vocal about it message(paste("Retrieving", property_id, "dimensions")) # Get the stats - dimensions <- get_ga_stats(property_id, type = "dimensions") + dimensions <- get_ga_stats(property_id, stats_type = "dimensions") return(dimensions) }) @@ -297,7 +296,7 @@ all_ga_metrics <- function(account_id, format = "dataframe") { # Be vocal about it message(paste("Retrieving", property_id, "link clicks")) # Get the stats - links <- get_ga_stats(property_id, type = "link_clicks") + links <- get_ga_stats(property_id, stats_type = "link_clicks") return(links) }) @@ -320,12 +319,14 @@ all_ga_metrics <- function(account_id, format = "dataframe") { } #' Handle Google Analytics Lists -#' @description This is a function to gets metrics and dimensions for all properties associated with an account -#' @param account_id the account id of the properties you are trying to retrieve -#' @importFrom dplyr %>% +#' @description These functions are to clean metric and dimension data from Google Analytics `get_ga_stats()` function +#' @param metrics a metrics object from `get_ga_stats()` function +#' @importFrom dplyr %>% mutate_all mutate_at bind_rows +#' @importFrom purrr map +#' @importFrom tidyr separate #' @export -clean_metric_data <- function(metrics) { +clean_metric_data <- function(metrics = NULL) { stat_names <- metrics[[1]]$metricHeaders$name clean_df <- purrr::map(metrics, "rows") %>% @@ -337,15 +338,14 @@ clean_metric_data <- function(metrics) { return(clean_df) } -clean_dimension_data <- function(dimensions) { +clean_dimension_data <- function(dimensions = NULL) { all_website_dims <- lapply(dimensions, wrangle_dimensions) %>% dplyr::bind_rows(.id = "website") return(all_website_dims) } -clean_link_data <- function(link_clicks) { - +clean_link_data <- function(link_clicks = NULL) { all_website_links <- lapply(link_clicks, wrangle_dimensions) %>% dplyr::bind_rows(.id = "website") diff --git a/R/token-handlers.R b/R/token-handlers.R index 771b4c9..d68eae7 100644 --- a/R/token-handlers.R +++ b/R/token-handlers.R @@ -26,21 +26,19 @@ 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) { - # If there's none in the current environment, attempt to grab a stored credential if (is.null(.Env$metricminer_tokens[[app_name]])) { - - .Env$metricminer_tokens[[app_name]] <- get_stored_token(app_name) - # only print this message if we are successful - if (!is.null(.Env$metricminer_tokens[[app_name]])) message("Using user-supplied token stored using authorize(\"", app_name, "\")") + .Env$metricminer_tokens[[app_name]] <- get_stored_token(app_name) + # only print this message if we are successful + if (!is.null(.Env$metricminer_tokens[[app_name]])) message("Using user-supplied token stored using authorize(\"", app_name, "\")") } # Attempt to grab a cached credential if (is.null(.Env$metricminer_tokens[[app_name]])) { - .Env$metricminer_tokens[[app_name]] <- get_cached_token(app_name) + .Env$metricminer_tokens[[app_name]] <- get_cached_token(app_name) - # only print this message if we are successful - if (!is.null(.Env$metricminer_tokens[[app_name]])) message("Using user-supplied cached token using authorize(\"", app_name, "\")") - } + # only print this message if we are successful + if (!is.null(.Env$metricminer_tokens[[app_name]])) message("Using user-supplied cached token using authorize(\"", app_name, "\")") + } # If we don't get authorization, check if we said it was required or not if (is.null(.Env$metricminer_tokens[[app_name]])) { diff --git a/R/utils.R b/R/utils.R index 0e26531..8654e2c 100644 --- a/R/utils.R +++ b/R/utils.R @@ -51,8 +51,10 @@ cache_secrets_folder <- function() { ) if (length(file_path) == 0) { - dir.create(file.path(system.file("extdata", package = "metricminer"), - "cached-secrets"), recursive = TRUE, showWarnings = FALSE) + dir.create(file.path( + system.file("extdata", package = "metricminer"), + "cached-secrets" + ), recursive = TRUE, showWarnings = FALSE) } list.files( pattern = "cached-secrets", diff --git a/man/all_ga_metrics.Rd b/man/all_ga_metrics.Rd index bca050f..22b97f3 100644 --- a/man/all_ga_metrics.Rd +++ b/man/all_ga_metrics.Rd @@ -24,5 +24,6 @@ authorize("google") accounts <- get_ga_user() stats_list <- all_ga_metrics(account_id = accounts$id[5]) +saveRDS(stats_list, "itcr_website_data.rds") } } diff --git a/man/clean_metric_data.Rd b/man/clean_metric_data.Rd index 1604d9e..28a7693 100644 --- a/man/clean_metric_data.Rd +++ b/man/clean_metric_data.Rd @@ -4,11 +4,11 @@ \alias{clean_metric_data} \title{Handle Google Analytics Lists} \usage{ -clean_metric_data(metrics) +clean_metric_data(metrics = NULL) } \arguments{ -\item{account_id}{the account id of the properties you are trying to retrieve} +\item{metrics}{a metrics object from `get_ga_stats()` function} } \description{ -This is a function to gets metrics and dimensions for all properties associated with an account +These functions are to clean metric and dimension data from Google Analytics `get_ga_stats()` function } diff --git a/man/get_ga_properties.Rd b/man/get_ga_properties.Rd index e7a4e0d..6ed09c2 100644 --- a/man/get_ga_properties.Rd +++ b/man/get_ga_properties.Rd @@ -18,6 +18,6 @@ This is a function to get the Google Analytics accounts that this user has acces authorize("google") accounts <- get_ga_user() -properties_list <- get_ga_properties(account_id = accounts$id[1]) +properties_list <- get_ga_properties(account_id = 209776907) } } diff --git a/man/get_ga_user.Rd b/man/get_ga_user.Rd index 0310d5f..91cda14 100644 --- a/man/get_ga_user.Rd +++ b/man/get_ga_user.Rd @@ -4,9 +4,11 @@ \alias{get_ga_user} \title{Get Google Analytics Accounts} \usage{ -get_ga_user(request_type = "GET") +get_ga_user(token = NULL, request_type = "GET") } \arguments{ +\item{token}{credentials for access to Google using OAuth. `authorize("google")`} + \item{request_type}{Is this a GET or a POST?} } \description{ diff --git a/man/get_repos_metrics.Rd b/man/get_repos_metrics.Rd index c248183..e8ceb77 100644 --- a/man/get_repos_metrics.Rd +++ b/man/get_repos_metrics.Rd @@ -32,10 +32,10 @@ particular organization, or you can provide a character vector of repos like " authorize("github") all_repos_metrics <- get_repos_metrics(owner = "fhdsl") +readr::write_tsv(all_repos_metrics, "fhdsl_github_metrics.tsv") repo_names <- c("fhdsl/metricminer", "jhudsl/OTTR_Template") some_repos_metrics <- get_repos_metrics(repo_names = repo_names) - } }