Marc Kupietz | a93a272 | 2021-04-28 12:22:45 +0200 | [diff] [blame] | 1 | #' Get reports from a matomo API server |
| 2 | #' |
| 3 | #' See matomo Reporting API Reference (\url{https://developer.matomo.org/api-reference/reporting-api}) for details. |
| 4 | #' |
| 5 | #' @references \url{https://developer.matomo.org/api-reference/reporting-api} |
| 6 | #' |
| 7 | #' @param matomoUrl base URL of your matomo instance |
| 8 | #' @param siteId matomo site id or vector of site ids |
| 9 | #' @param period \code{day}, \code{week}, \code{month} or \code{year} |
| 10 | #' @param date date range (see \url{https://developer.matomo.org/api-reference/reporting-api}) |
| 11 | #' @param filter_limit defines the maximum number of rows to be returned |
| 12 | #' @param removeFirst logical that determines whether the first row of each site should be removed (to account for incomplete periods) |
| 13 | #' @param removeLast logical that determines whether the last row of each site should be removed (to account for incomplete periods) |
| 14 | #' @param accessToken API Authentication Token - you can get this in your |
| 15 | #' matomo interface under Settings -> Personal -> Settings -> API Authentication Token |
| 16 | #' and pass it here, or you can make it persistent with \code{\link{persistAccessToken}}. |
| 17 | #' @param getMethod API method to call – default: VisitsSummary.get |
| 18 | #' @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. |
| 19 | #' |
| 20 | #' @import httr |
| 21 | #' @importFrom jsonlite fromJSON |
| 22 | #' @importFrom dplyr mutate rowwise bind_rows select summarise n |
| 23 | #' @import tibble |
| 24 | #' @importFrom magrittr %>% |
| 25 | #' @importFrom utils head tail |
| 26 | #' |
| 27 | #' @examples |
| 28 | #' \dontrun{ |
| 29 | #' df <- matomoQuery("https://demo.matomo.org/", getMethod = "UserCountry.getCountry") |
| 30 | #' } |
| 31 | #' |
| 32 | #' @export |
| 33 | matomoQuery <- function(matomoUrl, |
| 34 | siteId, |
| 35 | period = "month", |
| 36 | date = "last16", |
Marc Kupietz | 0ec5052 | 2022-04-09 15:53:24 +0200 | [diff] [blame] | 37 | filter_limit = 500, |
Marc Kupietz | a93a272 | 2021-04-28 12:22:45 +0200 | [diff] [blame] | 38 | removeFirst = FALSE, |
| 39 | removeLast = FALSE, |
| 40 | accessToken = getAccessToken(matomoUrl), |
| 41 | getMethod = "VisitsSummary.get" |
| 42 | ) { |
Marc Kupietz | f17fb82 | 2023-04-21 20:54:56 +0200 | [diff] [blame] | 43 | if ((is.null(accessToken) || accessToken == '' ) && matomoUrl != "https://demo.matomo.org/") { |
Marc Kupietz | a93a272 | 2021-04-28 12:22:45 +0200 | [diff] [blame] | 44 | stop( |
| 45 | paste0( |
| 46 | "You must first set an access token with:\n\npersistAccessToken(\"", |
| 47 | matomoUrl, |
| 48 | "\", <token>)\n\nYou can get the token in your matomo interface under Settings -> Personal -> Security -> Auth token\nprobably at:\n\n", matomoUrl, "index.php?module=UsersManager&action=userSecurity\n\n" |
| 49 | ), |
| 50 | call. = FALSE |
| 51 | ) |
| 52 | } |
| 53 | |
| 54 | httr::GET( |
| 55 | url = matomoUrl, |
| 56 | query = list( |
| 57 | module = "API", |
Marc Kupietz | 0ec5052 | 2022-04-09 15:53:24 +0200 | [diff] [blame] | 58 | method = getMethod, |
Marc Kupietz | a93a272 | 2021-04-28 12:22:45 +0200 | [diff] [blame] | 59 | format = "json", |
Marc Kupietz | a93a272 | 2021-04-28 12:22:45 +0200 | [diff] [blame] | 60 | idSite = paste0(siteId, collapse = ","), |
| 61 | date = date, |
| 62 | period = period, |
| 63 | filter_limit = filter_limit, |
Marc Kupietz | ea986d0 | 2021-04-28 15:54:23 +0200 | [diff] [blame] | 64 | language = "de", |
Marc Kupietz | a93a272 | 2021-04-28 12:22:45 +0200 | [diff] [blame] | 65 | token_auth = accessToken |
| 66 | ) |
| 67 | ) -> res |
| 68 | |
| 69 | if (status_code(res) != 200) { |
| 70 | if (json && !http_type(res) %in% c("application/json", "application/ld+json")) { |
| 71 | stop("API did not return json", call. = FALSE) |
| 72 | } |
| 73 | result <- jsonlite::fromJSON(content(res, "text", encoding = "UTF-8")) |
| 74 | if (!is.null(result$warnings)) { |
| 75 | message <- if (nrow(result$warnings) > 1) |
| 76 | sapply(result$warnings, function(warning) paste(sprintf("%s: %s", warning[1], warning[2]), sep="\n")) |
| 77 | else |
| 78 | sprintf("%s: %s", result$warnings[1], result$warnings[2]) |
| 79 | warning(message, call. = FALSE) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | if (!http_type(res) %in% c("application/json", "application/ld+json")) { |
| 84 | stop("API did not return json", call. = FALSE) |
| 85 | } |
| 86 | |
| 87 | json <- httr::content(res, "text", encoding = "UTF-8") |
Marc Kupietz | a93a272 | 2021-04-28 12:22:45 +0200 | [diff] [blame] | 88 | |
Marc Kupietz | f17fb82 | 2023-04-21 20:54:56 +0200 | [diff] [blame] | 89 | l <- jsonlite::fromJSON(json) |
| 90 | if("result" %in% colnames(l) && l$result == 'error') { |
| 91 | stop(paste("in api call:", l$message), call. = FALSE) |
| 92 | } |
| 93 | |
Marc Kupietz | 0ec5052 | 2022-04-09 15:53:24 +0200 | [diff] [blame] | 94 | if (period=="range") { |
| 95 | df <- if (length(siteId) == 1) { |
Marc Kupietz | f17fb82 | 2023-04-21 20:54:56 +0200 | [diff] [blame] | 96 | l %>% |
Marc Kupietz | 0ec5052 | 2022-04-09 15:53:24 +0200 | [diff] [blame] | 97 | mutate(site_id=siteId) |
| 98 | } else { |
| 99 | bind_rows(jsonlite::fromJSON(json), .id = "site_id") |
Marc Kupietz | a93a272 | 2021-04-28 12:22:45 +0200 | [diff] [blame] | 100 | } |
Marc Kupietz | 0ec5052 | 2022-04-09 15:53:24 +0200 | [diff] [blame] | 101 | } else { |
Marc Kupietz | 0ec5052 | 2022-04-09 15:53:24 +0200 | [diff] [blame] | 102 | df <- (if (length(siteId) == 1) { |
| 103 | bind_rows(l, .id=period) %>% |
| 104 | head(if(removeLast) -1 else filter_limit) %>% |
| 105 | tail(if(removeFirst) -1 else filter_limit) %>% |
| 106 | mutate(site_id=siteId) |
| 107 | } else { |
| 108 | df <- bind_rows(l[[1]], .id=period) %>% |
| 109 | head(if(removeLast) -1 else filter_limit) %>% |
| 110 | tail(if(removeFirst) -1 else filter_limit) %>% |
| 111 | mutate(site_id=siteId[1]) |
| 112 | for (i in 2:length(l)) { |
| 113 | df <- bind_rows(df, |
| 114 | bind_rows(l[[i]], .id=period) %>% |
| 115 | head(if(removeLast) -1 else filter_limit) %>% |
| 116 | tail(if(removeFirst) -1 else filter_limit) %>% |
| 117 | mutate(site_id=siteId[i])) |
| 118 | } |
| 119 | df |
| 120 | }) |
| 121 | } |
Marc Kupietz | a93a272 | 2021-04-28 12:22:45 +0200 | [diff] [blame] | 122 | |
| 123 | if("day" %in% colnames(df) | "month" %in% colnames(df) | "ye" %in% colnames(df)) { |
| 124 | df <- df %>% |
| 125 | mutate(date = as.Date( |
| 126 | if (period == "month") |
| 127 | paste0(month, "-01") |
| 128 | else if (period == "year") |
| 129 | paste0(year, "-01-01") |
| 130 | else if (period == "week") |
| 131 | sub(",.*", "", week) |
| 132 | else if (period == "day") |
| 133 | day |
| 134 | else |
| 135 | stop(paste0("unsupported period parameter: '", period, "'"), call. = FALSE) |
Marc Kupietz | 0ec5052 | 2022-04-09 15:53:24 +0200 | [diff] [blame] | 136 | , optional = TRUE)) |
Marc Kupietz | a93a272 | 2021-04-28 12:22:45 +0200 | [diff] [blame] | 137 | } |
| 138 | return(df) |
| 139 | } |
| 140 | |
| 141 | utils::globalVariables(c("year", "month", "day", "week")) |
| 142 | |
| 143 | #' Save access token persistently to your keyring |
| 144 | #' |
| 145 | #' @param matomoUrl base URL of your matomo instance |
| 146 | #' @param accessToken your oauth token |
| 147 | #' @param id supply if you have multiple IDs, i.e. logins to your matomo instance |
| 148 | #' |
| 149 | #' @import keyring |
| 150 | #' |
| 151 | #' @export |
| 152 | #' |
| 153 | #' @examples |
| 154 | #' persistAccessToken("https://demo.matomo.org/", "ad7609a669179c4ebca7c995342f7e09") |
| 155 | #' |
| 156 | persistAccessToken <- function(matomoUrl, accessToken, id="default") { |
| 157 | if (is.null(accessToken)) |
| 158 | stop("It seems that you have not supplied any access token that could be persisted.", call. = FALSE) |
| 159 | |
| 160 | keyring::key_set_with_value(matomoUrl, username=id, password=accessToken, keyring = NULL) |
| 161 | } |
| 162 | |
| 163 | clearAccessToken <- function(matomoUrl, id="default") { |
| 164 | key_delete(matomoUrl, id) |
| 165 | } |
| 166 | |
| 167 | |
| 168 | |
| 169 | #' get access token for matomo from keyring |
| 170 | #' |
| 171 | #' @param matomoUrl base URL of your matomo instance |
| 172 | #' @param id supply if you have multiple IDs, i.e. logins to your matomo instance |
| 173 | #' |
| 174 | #' @return access token |
| 175 | #' @export |
| 176 | #' |
| 177 | #' @import keyring |
| 178 | #' |
| 179 | getAccessToken <- function(matomoUrl, id="default") { |
| 180 | keyList <- tryCatch(withCallingHandlers(key_list(service = matomoUrl), |
| 181 | warning = function(w) invokeRestart("muffleWarning"), |
| 182 | error = function(e) return(NULL)), |
| 183 | error = function(e) { }) |
| 184 | if (id %in% keyList) |
| 185 | key_get(matomoUrl, id) |
| 186 | else |
| 187 | NULL |
| 188 | } |
| 189 | |
| 190 | |