Skip to content

Getting started

Installation

To install asgi-matomo:

pip install asgi-matomo

Usage

To use the MatomoMiddleware with, for instance Starlette, simply:

from asgi_matomo import MatomoMiddleware
from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette.responses import JSONResponse
from starlette.routing import Route


async def homepage(request):
    return JSONResponse({"data": 4000})


app = Starlette(
    routes=[Route("/", homepage)],
    middleware=[
        Middleware(
            MatomoMiddleware,
            matomo_url="YOUR MATOMO TRACKING URL",
            idsite=12345,  # your service tracking id
        )
    ],
)

This will track all requests made to your service.

What is tracked?

Currently asgi-matomo tracks the following variables:

  • action_name that defaults to the path
  • url of the request
  • ua: user-agent of the client
  • gt_ms: measured as the time before and after this middleware call next in the asgi stack.
  • cvar with at least http_status_code and http_method set.
  • lang: if the header accept-lang is set
  • cip: client ip, this is only tracked if access_token is given
  • sendimage=0 for performance issues

Please refer to Matomo Tracking HTTP API for available variables.