knitr::opts_chunk$set( collapse = TRUE, message = FALSE, warning = FALSE, eval = FALSE, comment = "#>" )
library(statZHmatomo) library(magrittr) library(dplyr) library(tidyr)
Diese Vignette beschreibt wie eine Verbindung zu einem der drei verfügbaren Matomo Instanzen aufgebaut werden kann und wie die Daten dann gelesen und genutzt werden können.
Die Funktion read_mamatomo_data.R
greift auf die matomo Reporting API zu. Bisher ist die Funktion nur für die Hauptmodule getestet, welche sich auch im matomo Dashboard befinden. Generell kann die Funktion jedoch um Argumente erweitert werden und für jegliche vorhandenen Module genutzt werden.
Eine komplette Übersicht der Module befindet sich auch im unter Administration -> Plattform -> API.
Die Funktion read_matomo_data.R
braucht mindestens einen Wert für die Argumente connection
, apiModule
und apiAction
. Standardmässig sind folgende Argumente vorbestimmt:
date
= "yesterday"period
= "day"format
= "csv"Weitere Auswahlmöglichkeiten können in der Help File nachgelesen werden: ?read_matomo_data.R
Um read_matomo_data.R
zu nutzen, muss mittels der Funktion set_matomot_server.R
die Verbindung zu einem der drei Matomo Server gesetzt werden:
openzh
webzh-dk
webzh
Folgender Code speichert die Verbindung zum ZHWeb Daten- und Publikationskatalog als Objekt mit dem Namen con_obj ab, welches dann als erstes Argument in read_matomo_data.R
genutzt wird (siehe unten).
con_obj <- set_matomo_server(server = "webzh-dk")
Die Kategorie "Verhalten (EN: Behaviour)" im matomo Dashboard entspricht dem API module "Actions".
Die Übersicht "Seiten (EN: Pages)" im matomo Dashboard entspricht der API action "getPageUrls"
## Argument date muss nicht gesetzt werden, da "yesterday" bereits vorbestimmt ist read_matomo_data(connection = con_obj, apiModule = "Actions", apiAction = "getDownloads") %>% select(1:4) %>% janitor::clean_names() %>% knitr::kable()
## Gestern read_matomo_data(connection = con_obj, apiModule = "Actions", apiAction = "getDownloads") %>% select(1:4) %>% janitor::clean_names() %>% knitr::kable()
## Tag auf aktuellen Tag setzen date_this_month <- Sys.Date() ## Für period month auswählen read_matomo_data(connection = con_obj, apiModule = "Actions", apiAction = "getPageUrls", date = date_this_month, period = "month") %>% select(1:4)
date_this_month <- Sys.Date() ## Dieser Monat read_matomo_data(connection = con_obj, apiModule = "Actions", apiAction = "getPageUrls", date = date_this_month, period = "month") %>% select(1:4) %>% janitor::clean_names() %>% knitr::kable()
## Tag auf einen Tag im letzten Monat setzen date_last_month <- Sys.Date - 31 ## Dieser Monat read_matomo_data(connection = con_obj, apiModule = "Actions", apiAction = "getPageUrls", date = date_last_month, period = "month") %>% select(1:4)
date_last_month <- Sys.Date() - 31 ## Dieser Monat read_matomo_data(connection = con_obj, apiModule = "Actions", apiAction = "getPageUrls", date = date_last_month, period = "month") %>% select(1:4) %>% janitor::clean_names() %>% knitr::kable()
## Period auf "year" setzen read_matomo_data(connection = con_obj, apiModule = "Actions", apiAction = "getPageUrls", period = "year") %>% select(1:4)
## Period auf "year" setzen read_matomo_data(connection = con_obj, apiModule = "Actions", apiAction = "getPageUrls", period = "year") %>% select(1:4) %>% janitor::clean_names() %>% knitr::kable()
Im JSON Format gibt die API auch die sogenannten "subtables" heraus. Neu ist hier die List-Column "subtable" in welcher nun Metriken für die einzelnen Datensätze im Datenkatalog zu finden sind.
## Subtable getPage_json <- read_matomo_data(connection = con_obj, apiModule = "Actions", apiAction = "getPageUrls", period = "year", format = "json", idSubtable = 3)
Die matomo Instanz des ZHWeb Daten- und Publikationskatalog hat insgesamt fünf konfigurierte Custom Dimensions, welche sich im Dashboard unter "Verhalten" befinden.
Für die folgenden Custom Dimensions lassen sich Reports erstellen.
read_matomo_data(connection = con_obj, apiModule = "CustomDimensions", apiAction = "getConfiguredCustomDimensions", format = "json") %>% select(-extractions) %>% filter(active == TRUE) %>% as_tibble() %>% knitr::kable()
custom_dim_format <- read_matomo_data(connection = con_obj, format = "json", apiModule = "CustomDimensions", apiAction = "getCustomDimension", idDimension = 1) %>% as_tibble() custom_dim_format %>% select(label, subtable) %>% unnest() %>% select(1:2, nb_visits, nb_hits) %>% DT::datatable()
custom_dim_searchTerm <- read_matomo_data(connection = con_obj, format = "json", apiModule = "CustomDimensions", apiAction = "getCustomDimension", idDimension = 2) %>% as_tibble() custom_dim_searchTerm %>% select(label, subtable) %>% unnest() %>% select(1:2, nb_visits, nb_hits) %>% DT::datatable()
custom_dim_topic <- read_matomo_data(connection = con_obj, format = "json", apiModule = "CustomDimensions", apiAction = "getCustomDimension", idDimension = 3) %>% as_tibble() custom_dim_topic %>% select(label, subtable) %>% unnest() %>% select(1:2, nb_visits, nb_hits) %>% DT::datatable()
## Data custom_dim_downloadURL <- read_matomo_data(connection = con_obj, format = "json", apiModule = "CustomDimensions", apiAction = "getCustomDimension", idDimension = 4) %>% as_tibble() ## Datatable custom_dim_downloadURL %>% select(label, subtable) %>% tidyr::unnest() %>% select(1:2, nb_visits, nb_hits) %>% DT::datatable()
## Data custom_dim_identifier <- read_matomo_data(connection = con_obj, format = "json", apiModule = "CustomDimensions", apiAction = "getCustomDimension", idDimension = 9) %>% as_tibble() ## Datatable custom_dim_identifier %>% select(label, subtable) %>% tidyr::unnest() %>% select(1:2, nb_visits, nb_hits) %>% DT::datatable()
Für das ausgewählte apiModule und apiAction wird ein kompletter Report mit jeglichen Variablen und dazugehörigem Vokabular zurückgegeben. Sehr nützlich für Dashboards.
downloadUrl_processed_report <- read_matomo_data(connection = con_obj, format = "json", apiModule = "CustomDimensions", apiAction = "getCustomDimension", idDimension = 4, processed_report = TRUE ) downloadUrl_processed_report$website downloadUrl_processed_report$prettyDate downloadUrl_processed_report$metadata$metricsDocumentation$nb_visits
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.