Use workaround for MATOMO bug with users by country
Resolves
https://gitlab.ids-mannheim.de/tfnd/IDS-Nutzungsdatenreport/-/issues/14
Change-Id: I72015a50d145fd503b69523ba0f770ddd66e421f
diff --git a/NAMESPACE b/NAMESPACE
index 9148b1c..a9260f0 100644
--- a/NAMESPACE
+++ b/NAMESPACE
@@ -37,6 +37,7 @@
importFrom(magrittr,"%>%")
importFrom(stringr,str_replace)
importFrom(stringr,str_replace_all)
+importFrom(stringr,str_split)
importFrom(tibble,as_tibble)
importFrom(tibble,rownames_to_column)
importFrom(tidyr,complete)
diff --git a/R/matomoquery.R b/R/matomoquery.R
index cfdfb86..2bdb74e 100644
--- a/R/matomoquery.R
+++ b/R/matomoquery.R
@@ -15,6 +15,7 @@
#' matomo interface under Settings -> Personal -> Settings -> API Authentication Token
#' and pass it here, or you can make it persistent with \code{\link{persistAccessToken}}.
#' @param getMethod API method to call – default: VisitsSummary.get
+#' @param ignoreEmptyResult if FALSE stop with error message on empty list results, otherwise return empty list
#' @return Data frame with visits summary as returned by matomo. Note that the \code{date} column in the returned data frame refers to the first day of the respective period.
#'
#' @importFrom httr2 request req_url_query req_perform resp_body_json resp_body_raw req_error
@@ -39,7 +40,8 @@
removeFirst = FALSE,
removeLast = FALSE,
accessToken = getAccessToken(matomoUrl),
- getMethod = "VisitsSummary.get"
+ getMethod = "VisitsSummary.get",
+ ignoreEmptyResult = FALSE
) {
if ((is.null(accessToken) || accessToken == '' ) && matomoUrl != "https://demo.matomo.org/") {
stop(
@@ -73,7 +75,7 @@
}
if (period=="range") {
- df <- if (is.list(l) && length(l) == 0) {
+ df <- if (!ignoreEmptyResult && is.list(l) && length(l) == 0) {
stop(paste0("API call ", res$url, " returned the empty list [].\n"), call. = FALSE)
} else if (is.list(l)) {
df <- bind_rows(l)
diff --git a/R/usercountry.R b/R/usercountry.R
index 0ea414c..62075e1 100644
--- a/R/usercountry.R
+++ b/R/usercountry.R
@@ -6,7 +6,7 @@
#'
#' @inheritParams matomoQuery
#'
-#' @importFrom stringr str_replace str_replace_all
+#' @importFrom stringr str_replace str_replace_all str_split
#'
#' @examples
#' \dontrun{
@@ -21,12 +21,31 @@
filter_limit = 100,
accessToken = getAccessToken(matomoUrl)
) {
- matomoQuery(matomoUrl = matomoUrl,
- siteId = siteId,
- period = period,
- date = str_replace_all(str_replace_all(date, " *UTC", ""), "-00", "-01"),
- removeFirst = FALSE,
- removeLast = FALSE,
- accessToken = accessToken,
- getMethod = "UserCountry.getCountry")
+ result <- list()
+ MAX_RETRIES <- 5
+ retry_count <- 0
+
+ while (length(result) == 0 && retry_count < MAX_RETRIES) {
+ result <- matomoQuery(matomoUrl = matomoUrl,
+ siteId = siteId,
+ period = period,
+ date = str_replace_all(str_replace_all(date, " *UTC", ""), "-00", "-01"),
+ removeFirst = FALSE,
+ removeLast = FALSE,
+ accessToken = accessToken,
+ getMethod = "UserCountry.getCountry",
+ ignoreEmptyResult = retry_count < MAX_RETRIES)
+
+ if (length(result) == 0 && retry_count < MAX_RETRIES) {
+ msg <- paste0("UserCountry.getCountry returned an empty list (known MATOMO bug). Changing date range from ", date, " to ")
+ retry_count <- retry_count + 1
+ startend <- str_split(date, ",", simplify = T)
+ l <- str_split(startend[1], "-", simplify = T)
+ l[3] <- sprintf("%02d", as.numeric(l[3]) + 1)
+ date <- paste0(paste0(l, collapse = "-"), ",", startend[2])
+ msg <- paste0(msg, date, "\n")
+ warning(msg)
+ }
+ }
+ return(result)
}
diff --git a/man/matomoQuery.Rd b/man/matomoQuery.Rd
index babfb50..79bc676 100644
--- a/man/matomoQuery.Rd
+++ b/man/matomoQuery.Rd
@@ -13,7 +13,8 @@
removeFirst = FALSE,
removeLast = FALSE,
accessToken = getAccessToken(matomoUrl),
- getMethod = "VisitsSummary.get"
+ getMethod = "VisitsSummary.get",
+ ignoreEmptyResult = FALSE
)
}
\arguments{
@@ -36,6 +37,8 @@
and pass it here, or you can make it persistent with \code{\link{persistAccessToken}}.}
\item{getMethod}{API method to call – default: VisitsSummary.get}
+
+\item{ignoreEmptyResult}{if FALSE stop with error message on empty list results, otherwise return empty list}
}
\value{
Data frame with visits summary as returned by matomo. Note that the \code{date} column in the returned data frame refers to the first day of the respective period.