R client library to access the matomo web service API

Clone this repo:

Branches

  1. d785201 Bump version to 0.4.9000 by Marc Kupietz · 1 year, 5 months ago master
  2. 71c8975 Use workaround for MATOMO bug with users by country by Marc Kupietz · 1 year, 5 months ago
  3. a3ac01a Bump version to 0.3.900 by Marc Kupietz · 1 year, 8 months ago
  4. 39dd933 Switch to httr2 and get more error messages logged by Marc Kupietz · 1 year, 8 months ago
  5. d07b8eb Replace " UTC" and "-00" in all dates by Marc Kupietz · 1 year, 8 months ago

R client library to access the Matomo web service API


Description

The package provides client access to the web service API of the web analytics platform Matomo.

Installation Prerequisites

Windows

  • You need to install git to be able to access the development version of matomor.

Linux

matomor uses some R packages with system dependencies you might need to install first:

#### Debian / Ubuntu
sudo apt install r-base-dev libcurl4-gnutls-dev libxml2-dev libsodium-dev libgit2-dev

#### Fedora / CentOS >= 8 / RHEL >= 8
sudo dnf install R-devel libcurl-devel openssl-devel libxml2-devel libsodium-devel libgit2-devel

#### CentOS < 8 / RHEL < 8
sudo yum install R-devel libcurl-devel openssl-devel libxml2-devel libsodium-devel libgit2-devel

#### Arch Linux
pacman -S base-devel gcc-fortran libsodium curl libgit2

Installation

library(devtools)
install_git("https://korap.ids-mannheim.de/gerrit/IDS-Mannheim/matomor") 

Documentation

See matomo Reporting API Reference for details.

NOTE: Currently, the date column in the data frame results refers to the first day of the respective period.

Usage

getVisitsSummary(
  matomoUrl,
  siteId,
  period = "month",
  date = "last16",
  filter_limit = 100,
  removeFirst = FALSE,
  removeLast = FALSE,
  accessToken = getAccessToken(matomoUrl)
)

Arguments

ArgumentDescription
matomoUrlbase URL of your matomo instance
siteIdmatomo site id or vector of site ids
periodday, week, month or year
datedate range (see matomo API reference)
filter_limitdefines the maximum number of rows to be returned
removeFirstlogical that determines whether the first row of each site should be removed (to account for incomplete periods)
removeLastlogical that determines whether the last row of each site should be removed (to account for incomplete periods)
accessTokenAPI Authentication Token - you can get this in your matomo interface under Settings -> Personal -> Settings -> API Authentication Token and pass it here, or you can make it persistent with persistAccessToken.

Result

Data frame with the following columns: (see Matomo API Response: Metric Definitions for a more detailed and authoritative description)

ColumnDescription
c("day","month","year")date (range)
nb_uniq_visitorsNumber of unique visitors
nb_usersNumber of unique active users (visitors with a known User ID). If you are not using User ID then this metric will be set to zero.
nb_visitsNumber of Visits (30 min of inactivity considered a new visit)
nb_actionsNumber of actions (page views, outlinks and downloads)
nb_visits_convertedNumber of visits that converted a goal
bounce_countNumber of visits that bounced (viewed only one page)
sum_visit_lengthTotal time spent, in seconds
max_actionsMaximum number of actions in a visit
avg_time_on_siteAverage time spent, in seconds, on this site
site_idId of requested site (provided by matomor)
dateinterpolated date (provided matomor)

Example result data frame returned from a query with period month.

monthnb_uniq_visitorsnb_usersnb_visitsnb_actionsnb_visits_convertedbounce_countsum_visit_lengthmax_actionsbounce_ratenb_actions_per_visitavg_time_on_sitesite_iddate
2020-0634303737705540851359524372423%18.996232020-06-01
2020-071048026955516306502599097206424%20.596432020-07-01
2020-0882302707605700581297800863121%22.4110032020-08-01
2020-0994903222749760691356448283621%23.3110632020-09-01
2020-101254036246995409013548351108925%19.397932020-10-01
2020-11180505005108621011085325212130822%21.7106432020-11-01
2020-12113402713623530574301587756521%23111232020-12-01

Examples

Plot with ggplot

library(matomor)
library(tidyverse)
library(idsThemeR)

# persistAccessToken("https://stats.xxx.org/", "ad7609a669179c4ebca7c995342f7e09")

getVisitsSummary("https://stats.xxx.org/", siteId = c(8,14), period="day", date="last60") %>%
     mutate(service = case_when(site_id == 8 ~ "A", site_id == 14 ~ "B")) %>%
     ggplot(aes(date, nb_visits, color=service)) + 
     scale_color_ids() + theme_ids() +
     geom_smooth(span=.3, se=FALSE)

Plot with highcharter

library(matomor)
library(tidyverse)
library(highcharter)
library(idsThemeR)

# persistAccessToken("https://stats.xxx.org/", "ad7609a669179c4ebca7c995342f7e09")

getVisitsSummary("https://stats.xxx.org/", siteId = c(13,14), period="month", date="last16") %>%
  mutate(service = case_when(site_id == 13 ~ "A", site_id == 14 ~ "B")) %>%
  hchart("spline", hcaes(date, nb_actions, group=service)) %>%
  hc_add_theme(hc_theme_ids_light())

Integration of non-matomo data in CSV format

library(matomor)
library(tidyverse)
library(highcharter)
library(idsThemeR)

serviceC_usage <- read.csv('./inst/exdata/serviceC_usage.csv') %>%
  mutate(service="C", date=as.Date(date)) %>%
   filter(date >= as.Date("2019-09-01"))

# matomoUtl <- "https://stats.xxx.org/"

getVisitsSummary(matomoUrl, siteId = c(13,14), period="month", date="2019-09-01,2020-11-01", removeLast=T) %>%
  mutate(service = case_when(site_id == 13 ~ "A", site_id == 14 ~ "B")) %>%
  bind_rows(serviceC_usage) %>%
  hchart("spline", hcaes(date, nb_visits, group=service)) %>%
  hc_add_theme(hc_theme_ids_light())

Actions per month from Jan 2019 until the end of last month saved as PNG

library(matomor)
library(idsThemeR)
library(lubridate)
library(tidyverse)
end_of_last_month <- floor_date(Sys.Date(), "month") - 1

getVisitsSummary("https://stats.xxx.de/", siteId = c(13,14), period="month",
  date=paste0("2019-01-01,", end_of_last_month)) %>%
      mutate(Service = case_when(site_id == 13 ~ "Angebot A", site_id == 14 ~ "Angebot B")) %>%
      ggplot(aes(date, nb_actions, col = Service, group = Service)) +
      scale_x_date(date_labels = "%b %y") +
      theme_ids() +
      scale_color_ids() +
      xlab("") +
      ylab("Abfragen") +
      labs(color = NULL) +
      scale_y_continuous(labels = scales::label_number_si()) +
      geom_point() +
      geom_line()

ggsave("./man/figures/AB-actions.png", width = 70 * .pt, height = 30 * .pt,  units = "mm", dpi = 600)

Development and License

Authors: Marc Kupietz

Copyright (c) 2020, Leibniz Institute for the German Language, Mannheim, Germany

This package is developed as part of the KorAP Corpus Analysis Platform at the Leibniz Institute for German Language (IDS).

It is published under the BSD-2 License.

Contributions

Contributions are very welcome!

Your contributions should ideally be committed via our Gerrit server to facilitate reviewing (see Gerrit Code Review - A Quick Introduction if you are not familiar with Gerrit). However, we are also happy to accept comments and pull requests via GitHub.

Please note that unless you explicitly state otherwise any contribution intentionally submitted for inclusion into this software shall – as this software itself – be under the BSD-2 License.