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

Update Dockerfile #94

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 55 additions & 2 deletions R/google-analytics.R
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down Expand Up @@ -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(
Expand All @@ -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)
Expand All @@ -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"),
Expand All @@ -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")
}
Expand Down Expand Up @@ -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
Expand All @@ -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)
}

Expand Down
10 changes: 2 additions & 8 deletions inst/extdata/docker/Dockerfile → inst/extdata/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
FROM rocker/tidyverse:4.3
FROM rocker/tidyverse:4.4
LABEL maintainer="[email protected]"
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/")'

Expand Down
1 change: 0 additions & 1 deletion inst/extdata/docker/github_package_list.tsv

This file was deleted.

52 changes: 0 additions & 52 deletions inst/extdata/docker/install_github.R

This file was deleted.

Loading