Skip to main content

Middleware for tracking ASGI reqeusts with Matomo

Project description

asgi-matomo

Packaging status CI Coverage codecov

Tracking requests with Matomo from ASGI apps.

MatomoMiddleware adds tracking of all requests to Matomo to ASGI applications (Starlette, FastAPI, Quart, etc.).

Installation

pip install asgi-matomo

Examples

Starlette

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

from asgi_matomo import MatomoMiddleware

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

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

FastAPI

from fastapi import FastAPI
from asgi_matomo import MatomoMiddleware

app = FastAPI()
app.add_middleware(
  BrotliMiddleware,
  matomo_url="YOUR MATOMO TRACKING URL",
  idsite=12345, # your service tracking id
)

@app.get("/")
def home() -> dict:
    return {"data": "a" * 4000}

API Reference

Overview

app.add_middleware(
  MatomoMiddleware,
  matomo_url="YOUR MATOMO TRACKING URL",
  idsite=12345, # your service tracking id
  access_token="SECRETTOKEN",
  assume_https=True,
  minimum_size=400,
)

Parameters:

  • (Required) matomo_url: The URL to make your tracking calls to.
  • (Required) idsite: The tracking id for your service.
  • (Optional) access_token: Access token for Matomo. If this is set cip is also tracked. Required for tracking some data.
  • (Optional) assume_https: If True, set tracked url scheme to https, useful when running behind a proxy. Defaults to True.

Notes:

Ideas for further work:

  • filtering tracked of urls
  • custom extraction of tracked data

Release Notes

Latest Changes

0.1.0 - 2023-04-28

  • Initial release.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

asgi_matomo-0.1.2.tar.gz (4.5 kB view hashes)

Uploaded Source

Built Distribution

asgi_matomo-0.1.2-py3-none-any.whl (5.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page