Skip to content

Commit

Permalink
Here we go
Browse files Browse the repository at this point in the history
  • Loading branch information
cansavvy committed Nov 15, 2023
1 parent 03a24e1 commit 8d82ba3
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 24 deletions.
6 changes: 6 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
# Generated by roxygen2: do not edit by hand

export(all_ga_metrics)
export(auth_from_secret)
export(authorize)
export(delete_creds)
export(get_calendly_user)
export(get_ga_metadata)
export(get_ga_properties)
export(get_ga_stats)
export(get_ga_user)
export(get_github_metrics)
export(get_github_repo)
export(get_github_user)
export(get_google_files)
export(get_youtube_stats)
export(request_ga)
importFrom(assertthat,assert_that)
importFrom(assertthat,is.string)
importFrom(gh,gh)
Expand All @@ -20,6 +25,7 @@ importFrom(httr,oauth2.0_token)
importFrom(httr,oauth_app)
importFrom(httr,oauth_endpoints)
importFrom(jsonlite,fromJSON)
importFrom(lubridate,today)
importFrom(utils,browseURL)
importFrom(utils,installed.packages)
importFrom(utils,menu)
74 changes: 50 additions & 24 deletions R/google-analytics.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#' @importFrom jsonlite fromJSON
#' @importFrom assertthat assert_that is.string
#' @export
request_ga <- function(token, url, query = NULL, body = NULL, type) {
request_ga <- function(token, url, query = NULL, body_params = NULL, type) {
if (is.null(token)) {
# Get auth token
token <- get_token(app_name = "google")
Expand All @@ -25,21 +25,23 @@ request_ga <- function(token, url, query = NULL, body = NULL, type) {
body = body,
query = query,
config = config,
httr::accept_json()
httr::accept_json(),
encode = "json"
)
}

if (type == "POST") {
result <- httr::POST(url,
body = body,
body = body_params,
query = query,
config = config,
httr::accept_json()
httr::accept_json(),
encode = "json"
)
}

if (httr::status_code(result) != 200) {
httr::stop_for_status(result)
return(httr::content(result, "text"))
}

# Process and return results
Expand Down Expand Up @@ -74,6 +76,7 @@ get_ga_user <- function() {

#' 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
#' @param account_id the account id of the properties you are trying to retrieve
#' @importFrom httr config accept_json content
#' @importFrom jsonlite fromJSON
#' @importFrom assertthat assert_that is.string
Expand Down Expand Up @@ -169,22 +172,16 @@ get_ga_stats <- function(property_id, start_date = "2015-08-14", end_date = NULL
if (type == "metrics") {
body_params <- list(
dateRanges = list(
list(
"startDate" = start_date,
"endDate" = end_date
)
),
"endDate" = end_date),
metrics = metrics_list()
)
}
if (type == "dimensions") {
body_params <- list(
dateRanges = list(
list(
"startDate" = start_date,
"endDate" = end_date
)
),
"endDate" = end_date),
dimensions = dimensions_list()
)
}
Expand All @@ -196,7 +193,7 @@ get_ga_stats <- function(property_id, start_date = "2015-08-14", end_date = NULL
type = "POST"
)

return(result_list)
return(results)
}


Expand All @@ -218,16 +215,45 @@ metrics_list <- function() {

dimensions_list <- function() {
dimensions <- list(
list("name" = "activeUsers"),
list("name" = "newUsers"),
list("name" = "totalUsers"),
list("name" = "eventCountPerUser"),
list("name" = "screenPageViewsPerUser"),
list("name" = "sessions"),
list("name" = "averageSessionDuration"),
list("name" = "screenPageViews"),
list("name" = "engagementRate")
list("name" = "day"),
list("name" = "month"),
list("name" = "year"),
list("name" = "country"),
list("name" = "linkUrl"),
list("name" = "fullPageUrl")
)

return(metrics)
return(dimensions)
}

#' 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
#' @export
#' @examples \dontrun{
#'
#' authorize("google")
#' accounts <- get_ga_user()
#'
#' stats_list <- all_ga_metrics(account_id = accounts$id[5])
#' }
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)

all_google_analytics_data <- lapply(property_names, function(property_id) {

metrics <- get_ga_stats(property_id, type = "metrics")
dimensions <- get_ga_stats(property_id, type = "dimensions")

return(list(metrics = metrics, dimensions = dimensions))
})

names(all_google_analytics_data) <- properties_list$properties$displayName

return(all_google_analytics_data)
}

23 changes: 23 additions & 0 deletions man/all_ga_metrics.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions man/get_ga_metadata.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions man/get_ga_properties.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions man/get_ga_stats.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions man/request_ga.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8d82ba3

Please sign in to comment.