Skip to content

Commit

Permalink
Google analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
cansavvy committed Nov 15, 2023
1 parent 67690ed commit 03a24e1
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 63 deletions.
60 changes: 31 additions & 29 deletions R/github.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,14 @@ get_github_user <- function(api_key) {
#' get_github_user()
#' }
get_github_user <- function(api_key) {

if (is.null(api_key)) {
# Get auth token
token <- get_token(app_name = "github")
} else {
token <- api_key
}

get_github(
get_github(
url = "https://api.github.com/user",
token = token,
)
Expand All @@ -75,38 +74,40 @@ get_github_user <- function(api_key) {
#' authorize("github")
#' get_github_repo()
#' }
get_github_repo <- function(api_key, owner, repo) {

get_github_repo <- function(api_key, owner, repo) {
if (is.null(api_key)) {
# Get auth token
token <- get_token(app_name = "github")
} else {
token <- api_key
}
repo_activity <- gh::gh("GET /repos/{owner}/{repo}/activity",
owner = owner,
repo = repo,
.token = token)
owner = owner,
repo = repo,
.token = token
)



stars <- gh::gh("GET /repos/{owner}/{repo}/stargazers",
owner = owner,
repo = repo,
.token = token)
owner = owner,
repo = repo,
.token = token
)

forks <- gh::gh("GET /repos/{owner}/{repo}/forks",
owner = owner,
repo = repo,
.token = token)
owner = owner,
repo = repo,
.token = token
)

contributors <- gh::gh("GET /repos/{owner}/{repo}/contributors",
owner = owner,
repo = repo,
.token = token)
owner = owner,
repo = repo,
.token = token
)

return(list(repo_activity, stars, forks, contributors))

}


Expand All @@ -124,7 +125,6 @@ get_github_repo <- function(api_key, owner, repo) {
#' get_github_user()
#' }
get_github_metrics <- function(api_key, owner, repo) {

if (is.null(api_key)) {
# Get auth token
token <- get_token(app_name = "github")
Expand All @@ -133,19 +133,21 @@ get_github_metrics <- function(api_key, owner, repo) {
}

community <- gh::gh("GET /repos/{owner}/{repo}/community/profile",
owner = owner,
repo = repo,
.token = token)
owner = owner,
repo = repo,
.token = token
)

clones <- gh::gh("GET /repos/{owner}/{repo}/traffic/clones",
owner = owner,
repo = repo,
.token = token)
owner = owner,
repo = repo,
.token = token
)

views <- gh::gh("GET /repos/{owner}/{repo}/traffic/views",
owner = owner,
repo = repo,
.params = list("per" = "day"),
.token = token)
owner = owner,
repo = repo,
.params = list("per" = "day"),
.token = token
)
}

66 changes: 34 additions & 32 deletions R/google-analytics.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#' @importFrom assertthat assert_that is.string
#' @export
request_ga <- function(token, url, query = NULL, body = NULL, type) {

if (is.null(token)) {
# Get auth token
token <- get_token(app_name = "google")
Expand All @@ -23,18 +22,20 @@ request_ga <- function(token, url, query = NULL, body = NULL, type) {

if (type == "GET") {
result <- httr::GET(url,
body = body,
query = query,
config = config,
httr::accept_json())
body = body,
query = query,
config = config,
httr::accept_json()
)
}

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

if (httr::status_code(result) != 200) {
Expand All @@ -59,7 +60,6 @@ request_ga <- function(token, url, query = NULL, body = NULL, type) {
#' get_ga_user()
#' }
get_ga_user <- function() {

# Get auth token
token <- get_token(app_name = "google")

Expand All @@ -84,7 +84,6 @@ get_ga_user <- function() {
#' accounts <- get_ga_user()
#'
#' properties_list <- get_ga_properties(account_id = accounts$id[1])
#'
#' }
get_ga_properties <- function(account_id) {
# Get auth token
Expand Down Expand Up @@ -116,10 +115,8 @@ get_ga_properties <- function(account_id) {
#'
#' property_id <- gsub("properties/", "", properties_list$properties$name[1])
#' property_metadata <- get_ga_metadata(property_id = property_id)
#'
#' }
get_ga_metadata <- function(property_id) {

# Declare URL
url <- "https://analyticsdata.googleapis.com/v1beta/properties/property_id/metadata"
url <- gsub("property_id", property_id, url)
Expand All @@ -141,6 +138,7 @@ get_ga_metadata <- function(property_id) {
#' @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.
#'
#' @importFrom httr config accept_json content
#' @importFrom jsonlite fromJSON
#' @importFrom assertthat assert_that is.string
Expand All @@ -156,10 +154,8 @@ get_ga_metadata <- function(property_id) {
#' 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")
#'
#' }
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)

Expand All @@ -173,16 +169,22 @@ 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)),
list(
"startDate" = start_date,
"endDate" = end_date
)
),
metrics = metrics_list()
)
)
}
if (type == "dimensions") {
body_params <- list(
dateRanges = list(
list("startDate" = start_date,
"endDate" = end_date)),
list(
"startDate" = start_date,
"endDate" = end_date
)
),
dimensions = dimensions_list()
)
}
Expand All @@ -199,23 +201,22 @@ get_ga_stats <- function(property_id, start_date = "2015-08-14", end_date = NULL


metrics_list <- function() {

metrics <- 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" = "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")
)

return(metrics)
}

dimensions_list <- function() {

dimensions <- list(
list("name" = "activeUsers"),
list("name" = "newUsers"),
Expand All @@ -225,7 +226,8 @@ dimensions_list <- function() {
list("name" = "sessions"),
list("name" = "averageSessionDuration"),
list("name" = "screenPageViews"),
list("name" = "engagementRate"))
list("name" = "engagementRate")
)

return(metrics)
}
4 changes: 2 additions & 2 deletions R/youtube.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ get_youtube_stats <- function(channel_id) {
# If a URL is supplied, only take the ID from it.
if (grepl("https:", channel_id)) channel_id <- gsub("https://drive.google.com/drive/folders/", "")

query = list(
query <- list(
part = "snippet,contentDetails,statistics",
id = channel_id
)
} else {
stop(paste0("No channel_id was given"))
}
}

# Get list of topics
result <- httr::GET(url, config = config, query = query, httr::accept_json())
Expand Down

0 comments on commit 03a24e1

Please sign in to comment.