Matomo API
Apps
Matomo API Client
- class integreat_cms.matomo_api.matomo_api_client.MatomoApiClient(region: Region)[source]
Bases:
objectThis class helps to interact with Matomo API. There are three functions which can be used publicly:
get_matomo_id(): Retrieve the Matomo ID belonging to the given Matomo access tokenget_total_visits(): Retrieve the total visits for the current regionget_visits_per_language(): Retrieve the visits for the current region by language
- Parameters:
region (Region)
- __init__(region: Region) None[source]
Constructor initializes the class variables
- Parameters:
region (Region) – The region this Matomo API Manager connects to
- Return type:
None
- async async_fetch(session: ClientSession, **kwargs: Any) dict[str, Any] | list[int][source]
Uses
aiohttp.ClientSession.get()to perform an asynchronous GET request to the Matomo API.- Parameters:
session (ClientSession) – The session object which is used for the request
**kwargs (Any) – The parameters which are passed to the Matomo API
- Raises:
MatomoException – When a
ClientErrorwas raised during a Matomo API request- Returns:
The parsed
jsonresult- Return type:
- fetch(**kwargs: Any) dict[str, Any][source]
Uses
request.post()to perform an synchronous GET request to the Matomo API.- Parameters:
session – The session object which is used for the request
**kwargs (Any) – The parameters which are passed to the Matomo API
- Raises:
MatomoException – When a
ClientErrorwas raised during a Matomo API request- Returns:
The parsed
jsonresult- Return type:
- static get_access_data(total_visits: dict, webapp_downloads: dict, offline_downloads: dict) list[dict][source]
Structure the datasets for accesses in a chart.js-compatible format, returning it and the custom legend entries
- static get_language_data(languages: list[Language], datasets: list[dict]) list[dict][source]
Structure the datasets for languages in a chart.js-compatible format, returning it and the custom legend entries
- get_matomo_id(token_auth: str) int[source]
Returns the matomo website id based on the provided authentication key.
- Parameters:
token_auth (str) – The Matomo authentication token which should be used
- Raises:
MatomoException – When a
ClientErrorwas raised during a Matomo API request or the access token is not correct- Returns:
ID of the connected Matomo instance
- Return type:
- async get_matomo_id_async(**query_params: Any) list[int][source]
Async wrapper to fetch the Matomo ID with
aiohttp. Opens aClientSessionand callsasync_fetch(). Called fromget_matomo_id().- Parameters:
**query_params (Any) – The parameters which are passed to the Matomo API
- Returns:
The parsed
jsonresult- Raises:
MatomoException – When a
ClientErrorwas raised during a Matomo API request- Return type:
- get_page_accesses(start_date: date, end_date: date, region_slug: str, languages: list[Language], pages: list[Page], prefetched_translations: list[PageTranslation]) None[source]
This function handles getting the page based accesses from Matomo and saving them to the database
- Parameters:
start_date (date) – Start date
end_date (date) – End date
region_slug (str) – The region for which we want our page based accesses
languages (list[Language]) – List with the active languages of the region
prefetched_translations (list[PageTranslation]) – List with prefetched page translations
- Return type:
None
- get_total_visits(start_date: date, end_date: date, period: str = 'day') dict[str, Any][source]
Returns the total calls within a time range for all languages.
- Parameters:
- Raises:
MatomoException – When a
ClientErrorwas raised during a Matomo API request- Returns:
The total visits in the ChartData format expected by ChartJs
- Return type:
- async get_total_visits_async(query_params: dict[str, str | int | None]) dict[str, Any][source]
Async wrapper to fetch the total visits with
aiohttp. Opens aClientSessionand callsasync_fetch(). Called fromget_total_visits().- Parameters:
query_params (dict[str, str | int | None]) – The parameters which are passed to the Matomo API
- Raises:
MatomoException – When a
ClientErrorwas raised during a Matomo API request- Returns:
The parsed
jsonresult- Return type:
- get_visits_per_language(start_date: date, end_date: date, period: str) dict[str, Any][source]
Returns the total unique visitors in a timerange as defined in period
- Parameters:
- Returns:
The visits per language in the ChartData format expected by ChartJs
- Raises:
MatomoException – When a
ClientErrorwas raised during a Matomo API request- Return type:
- async get_visits_per_language_async(loop: AbstractEventLoop, query_params: dict[str, Any], languages: list[Language]) list[dict[str, Any]][source]
Async wrapper to fetch the total visits with
aiohttp. Opens aClientSession, creates aTaskfor each language to callasync_fetch()and waits for all tasks to finish withgather(). The returned list of gathered results has the correct order in which the tasks were created (at first the ordered list of languages and the last element is the task for the total visits). Called fromget_visits_per_language().- Parameters:
- Raises:
MatomoException – When a
ClientErrorwas raised during a Matomo API request- Returns:
The list of gathered results
- Return type: