diff --git a/DESCRIPTION b/DESCRIPTION index 1de4e02..3dfbaf7 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: flipAPI Type: Package Title: Web APIs tools -Version: 1.6.6 +Version: 1.7.0 Author: Displayr Maintainer: Displayr Description: Functions to extract data and interact with web APIs. @@ -17,9 +17,6 @@ Imports: mime, httr, ip2location, - jsonlite, - lubridate, - RGoogleAnalytics, stringr, readxl, utils, @@ -41,10 +38,11 @@ Imports: zeallot, zip Remotes: Displayr/flipTransformations, - Tatvic/RGoogleAnalytics, Suggests: testthat, httptest, + jsonlite, + withr, officer, gifski RoxygenNote: 7.3.3 diff --git a/NAMESPACE b/NAMESPACE index 16de0f1..01581dc 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -8,7 +8,6 @@ export(ExportToDropbox) export(FactbaseUploadWidget) export(GeocodeIPs) export(GetDirectLink) -export(GoogleAnalytics) export(ImportFromDropbox) export(IsDisplayrCloudDriveAvailable) export(QDeleteFiles) @@ -27,10 +26,6 @@ import(zeallot) importFrom(DBI,dbConnect) importFrom(DBI,dbDisconnect) importFrom(DBI,dbGetQuery) -importFrom(RGoogleAnalytics,GetProfiles) -importFrom(RGoogleAnalytics,GetReportData) -importFrom(RGoogleAnalytics,Init) -importFrom(RGoogleAnalytics,QueryBuilder) importFrom(RJSONIO,toJSON) importFrom(arrow,BufferOutputStream) importFrom(arrow,write_parquet) diff --git a/R/GoogleAnalytics.R b/R/GoogleAnalytics.R deleted file mode 100644 index 558ec66..0000000 --- a/R/GoogleAnalytics.R +++ /dev/null @@ -1,100 +0,0 @@ -#' Request data from Google Analytics -#' -#' @param dimensions A string (comma separated) or list of strings containing -#' the dimensions to request. -#' @param metrics Similar to \code{dimensions} but for metrics. -#' @param start.date,end.date The start and end dates for the query. If it is a -#' string it will be coerced to a date using YMD format. -#' @param secret.text The JSON formatted string containing the secret text that -#' identifies your account. -#' @param table.num If your account contains multiple tables, use this to select -#' the table to query. Defaults to the first table. -#' @importFrom RGoogleAnalytics GetProfiles QueryBuilder GetReportData Init -#' -#' @details See -#' \url{https://developers.google.com/analytics/devguides/reporting/core/dimsmets} -#' for details of the available dimensions and metrics. -#' -#' See -#' \url{http://wiki.q-researchsoftware.com/wiki/How_to_Import_Data_from_Google_Analytics} -#' for more information about setting up your Google Analytics account for -#' use with this function. The parameter \code{secret.text} is the text -#' contained in the JSON file you get when adding a Service Account. -#' -#' @export -GoogleAnalytics <- function(dimensions, metrics, start.date, end.date, secret.text, table.num = 1) -{ - .checkGaNames <- function(x, max.num) - { - type <- deparse(substitute(x)) - x <- stringr::str_trim(unlist(strsplit(x, ",", fixed = TRUE))) - have.prefix <- grepl("^ga:", x) - if (any(!have.prefix)) - x[!have.prefix] <- paste0("ga:", x[!have.prefix]) - - if (length(x) > max.num) - stop("Too many ", type, ". ", length(x), " specified, max ", max.num) - - paste(x, collapse = ",") - } - - dimensions <- .checkGaNames(dimensions, 7) - metrics <- .checkGaNames(metrics, 10) - - if (nchar(metrics) == 0) - stop("You must supply at least 1 metric.") - - if (is.character(start.date)) - start.date <- lubridate::ymd(start.date) - if (is.character(end.date)) - end.date <- lubridate::ymd(end.date) - - # We want the dates to go Monday-Sunday, so move start.date to previous Monday - # and end.date to next Sunday. - one.day <- lubridate::days(1) - start.date <- lubridate::floor_date(start.date - one.day, "week") + one.day - end.date <- lubridate::ceiling_date(end.date, "week") - - secret.text <- gsub("[^\x20-\x7E]", "", secret.text) - secrets <- jsonlite::fromJSON(secret.text) - secrets$private_key <- gsub("-----BEGIN PRIVATE KEY-----", - "-----BEGIN PRIVATE KEY-----\n", - secrets$private_key) - secrets$private_key <- gsub("-----END PRIVATE KEY-----", - "\n-----END PRIVATE KEY-----\n", - secrets$private_key) - - scope <- "https://www.googleapis.com/auth/analytics.readonly" - - token <- httr::oauth_service_token(httr::oauth_endpoints("google"), secrets, scope) - - table.id <- RGoogleAnalytics::GetProfiles(token)$id[table.num] - - query.list <- RGoogleAnalytics::Init(start.date = start.date, end.date = end.date, - dimensions = dimensions, - metrics = metrics, - max.results = 10000, - table.id = paste0("ga:", table.id)) - ga.query <- RGoogleAnalytics::QueryBuilder(query.list) - ga.data <- RGoogleAnalytics::GetReportData(ga.query, token, - split_daywise = (ga.query$start.date() != ga.query$end.date())) - - if ("date" %in% names(ga.data)) - { - ga.data$date <- lubridate::ymd(ga.data$date) - } - if ("pagePathLevel1" %in% names(ga.data)) - { - ga.data$pagePathNoQuery <- sub("\\?.*", "", ga.data$pagePathLevel1) - ga.data$pagePathNoQuery <- sub("(.+)/$", "\\1", ga.data$pagePathNoQuery) - } - if ("sourceMedium" %in% names(ga.data)) - { - sourceMedium <- strsplit(ga.data$sourceMedium, " / ", fixed = TRUE) - sourceMedium <- data.frame(do.call("rbind", sourceMedium), stringsAsFactors = FALSE) - names(sourceMedium) <- c("source", "medium") - ga.data <- cbind(ga.data, sourceMedium) - } - - ga.data -} diff --git a/man/GoogleAnalytics.Rd b/man/GoogleAnalytics.Rd deleted file mode 100644 index 551b0e0..0000000 --- a/man/GoogleAnalytics.Rd +++ /dev/null @@ -1,44 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/GoogleAnalytics.R -\name{GoogleAnalytics} -\alias{GoogleAnalytics} -\title{Request data from Google Analytics} -\usage{ -GoogleAnalytics( - dimensions, - metrics, - start.date, - end.date, - secret.text, - table.num = 1 -) -} -\arguments{ -\item{dimensions}{A string (comma separated) or list of strings containing -the dimensions to request.} - -\item{metrics}{Similar to \code{dimensions} but for metrics.} - -\item{start.date, end.date}{The start and end dates for the query. If it is a -string it will be coerced to a date using YMD format.} - -\item{secret.text}{The JSON formatted string containing the secret text that -identifies your account.} - -\item{table.num}{If your account contains multiple tables, use this to select -the table to query. Defaults to the first table.} -} -\description{ -Request data from Google Analytics -} -\details{ -See - \url{https://developers.google.com/analytics/devguides/reporting/core/dimsmets} - for details of the available dimensions and metrics. - - See - \url{http://wiki.q-researchsoftware.com/wiki/How_to_Import_Data_from_Google_Analytics} - for more information about setting up your Google Analytics account for - use with this function. The parameter \code{secret.text} is the text - contained in the JSON file you get when adding a Service Account. -} diff --git a/tests/testthat/test-datamart.R b/tests/testthat/test-datamart.R index 5d6b89a..cfcd719 100644 --- a/tests/testthat/test-datamart.R +++ b/tests/testthat/test-datamart.R @@ -1,4 +1,5 @@ library (testthat) +library(jsonlite) test_that("SaveData/LoadData", { skip_if(!nzchar(companySecret), "Not in test environment or no company set up")