Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating google analytics metrics getter #73

Merged
merged 13 commits into from
Mar 5, 2024
13 changes: 7 additions & 6 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

export("%>%")
export(auth_from_secret)
export(authorize)
export(cache_secrets_folder)
Expand All @@ -9,11 +10,11 @@ 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_info)
export(get_ga_stats)
export(get_ga_user)
export(get_github)
Expand All @@ -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)
Expand Down Expand Up @@ -62,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)
Expand Down
30 changes: 24 additions & 6 deletions R/auth.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
}
}
Expand Down
51 changes: 26 additions & 25 deletions R/github.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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(
Expand All @@ -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"
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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)
}

Expand All @@ -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)
}
Expand Down Expand Up @@ -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 <-
Expand Down Expand Up @@ -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)
}
Expand Down
Loading
Loading