From 1e64d3e7533e135364196e196d24b1623b0c7c23 Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Wed, 9 Oct 2024 14:43:36 -0400 Subject: [PATCH 1/2] per page metrics --- R/google-analytics.R | 57 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 2 deletions(-) diff --git a/R/google-analytics.R b/R/google-analytics.R index 8f1f3c5..e1d4cc2 100644 --- a/R/google-analytics.R +++ b/R/google-analytics.R @@ -215,11 +215,12 @@ get_ga_metadata <- function(property_id, token = NULL) { #' authorize("google") #' accounts <- get_ga_user() #' -#' properties_list <- get_ga_properties(account_id = accounts$id[1]) +#' properties_list <- get_ga_properties(account_id = accounts$id[2]) #' #' property_id <- gsub("properties/", "", properties_list$name[1]) #' metrics <- get_ga_stats(property_id, stats_type = "metrics") #' dimensions <- get_ga_stats(property_id, stats_type = "dimensions") +#' pages <- get_ga_stats(property_id, stats_type = "pages") #' } get_ga_stats <- function(property_id, start_date = "2015-08-14", token = NULL, body_params = NULL, end_date = NULL, stats_type = "metrics", dataformat = "dataframe") { @@ -253,6 +254,16 @@ get_ga_stats <- function(property_id, start_date = "2015-08-14", token = NULL, b dimensions = dimensions_list() ) } + if (stats_type == "pages") { + body_params <- list( + dateRanges = list( + "startDate" = start_date, + "endDate" = end_date + ), + metrics = metrics_page_list(), + dimensions = dimensions_page_list() + ) + } if (stats_type == "link_clicks") { body_params <- list( dateRanges = list( @@ -273,6 +284,9 @@ get_ga_stats <- function(property_id, start_date = "2015-08-14", token = NULL, b if (dataformat == "dataframe") { if (stats_type == "metrics") results <- clean_ga_metrics(results) if (stats_type %in% c("dimensions", "link_clicks")) results <- wrangle_ga_dimensions(results) + if (stats_type %in% c("pages")) { + results <- clean_ga_metrics(results, type = "pages") + } } return(results) @@ -294,6 +308,20 @@ metrics_list <- function() { return(metrics) } +metrics_page_list <- function() { + metrics <- list( + list("name" = "activeUsers"), + list("name" = "newUsers"), + list("name" = "totalUsers"), + list("name" = "eventCountPerUser"), + list("name" = "sessions"), + list("name" = "averageSessionDuration"), + list("name" = "screenPageViews"), + list("name" = "engagementRate") + ) + return(metrics) +} + dimensions_list <- function() { dimensions <- list( list("name" = "day"), @@ -306,6 +334,15 @@ dimensions_list <- function() { return(dimensions) } +dimensions_page_list <- function() { + dimensions <- list( + list("name" = "fullPageUrl") + ) + + return(dimensions) +} + + link_clicks <- function() { list("name" = "linkUrl") } @@ -403,13 +440,14 @@ get_multiple_ga_metrics <- function(account_id = NULL, #' Handle Google Analytics Lists #' @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 +#' @param type If type == "pages" then treat the data frame for in the instance that the dimensions of the subpages were collected #' @importFrom dplyr %>% mutate_all mutate_at bind_rows #' @importFrom purrr map #' @importFrom tidyr separate #' @return a data frame of cleaned metrics from Google Analytics #' @export -clean_ga_metrics <- function(metrics = NULL) { +clean_ga_metrics <- function(metrics = NULL, type = NULL) { # This is if we are running it with all the data at once if (length(metrics$metricHeaders$name) == 0) { stat_names <- metrics[[1]]$metricHeaders$name @@ -419,12 +457,27 @@ clean_ga_metrics <- function(metrics = NULL) { stat_names <- metrics$metricHeaders$name clean_df <- metrics$rows } + + if (type == "pages") { + dim_df <- clean_df$dimensionValues %>% + dplyr::bind_rows() %>% + dplyr::rename(page = value) + + clean_df <- clean_df[names(clean_df) != "dimensionValues"] + } + + clean_df <- clean_df %>% 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) + if (type == "pages") { + clean_df <- dim_df %>% + dplyr::bind_cols(clean_df) + } + return(clean_df) } From c9ced45e15d68e37e38d9823109dede65429c030 Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Wed, 16 Oct 2024 13:41:53 -0400 Subject: [PATCH 2/2] Update dockerfile bump to tidyverse 4.4 --- inst/extdata/{docker => }/Dockerfile | 10 +--- inst/extdata/docker/github_package_list.tsv | 1 - inst/extdata/docker/install_github.R | 52 --------------------- 3 files changed, 2 insertions(+), 61 deletions(-) rename inst/extdata/{docker => }/Dockerfile (53%) delete mode 100644 inst/extdata/docker/github_package_list.tsv delete mode 100644 inst/extdata/docker/install_github.R diff --git a/inst/extdata/docker/Dockerfile b/inst/extdata/Dockerfile similarity index 53% rename from inst/extdata/docker/Dockerfile rename to inst/extdata/Dockerfile index d87f1f4..004c817 100644 --- a/inst/extdata/docker/Dockerfile +++ b/inst/extdata/Dockerfile @@ -1,15 +1,9 @@ -FROM rocker/tidyverse:4.3 +FROM rocker/tidyverse:4.4 LABEL maintainer="cansav09@gmail.com" WORKDIR /rocker-build/ -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 installGithub.r fhdsl/metricminer RUN R -q -e 'install.packages("cranlogs", repos="https://cran.rstudio.com/")' diff --git a/inst/extdata/docker/github_package_list.tsv b/inst/extdata/docker/github_package_list.tsv deleted file mode 100644 index 848aed2..0000000 --- a/inst/extdata/docker/github_package_list.tsv +++ /dev/null @@ -1 +0,0 @@ -fhdsl/metricminer HEAD \ No newline at end of file diff --git a/inst/extdata/docker/install_github.R b/inst/extdata/docker/install_github.R deleted file mode 100644 index fb532ec..0000000 --- a/inst/extdata/docker/install_github.R +++ /dev/null @@ -1,52 +0,0 @@ -#!/usr/bin/env Rscript - -if (!"optparse" %in% installed.packages()) { - install.packages("optparse") -} - -library(optparse) - -################################ Set up options ################################ -# Set up optparse options -option_list <- list( - make_option( - opt_str = c("-p", "--packages"), type = "character", - default = "github_package_list.tsv" , - help = "Path to a TSV with a list of packages to be installed through Github, - where file where the first column is the github package name e.g. - jhudsl/ottrpal and the second column is the commit ID to be installed - (to be supplied to the ref argument). - ", - metavar = "character" - ), - make_option( - opt_str = c("--token"), type = "character", - default = NULL, - help = "GITHUB PAT file", - metavar = "character" - ) -) - -# Parse options -opt <- parse_args(OptionParser(option_list = option_list)) - -# Read in the token -token <- as.character(readLines(opt$token)[1]) - -# Reset GITHUB PAT to be token -Sys.unsetenv("GITHUB_PAT") -Sys.setenv(GITHUB_PAT = token) - -# set up list of packages to install -packages <- readr::read_tsv(opt$packages, - col_names = c("package_name", "ref")) - -purrr::pmap( - packages, - ~remotes::install_github(..1, - auth_token = token, - ref = ..2) - ) - -# Remove the file after we are done -file.remove(opt$token)