Giter Club home page Giter Club logo

umami-asgi's Introduction

ASGI Middleware for Umami Analytics

This project provides a middleware for your ASGI-compatible app (e.g. FastAPI, Starlette) that automatically sends access events to your umami instance. If you want to track your API clients in a GDPR-friendly way, then this middleware is for you.

Usage

For this middleware, you need (at least) the following information:

  • The URL of your Umami API (e.g. https://umami.example.com/api
  • The website GUID (e.g. 12345678-1234-1234-1234-123456789012)

Install the package via pip:

pip install umami-asgi

Then, you can add the umami middleware to your ASGI app. Here is an example for FastAPI:

from fastapi import FastAPI
from umami_asgi import UmamiMiddleware

app = FastAPI()
app.add_middleware(
    UmamiMiddleware,
    api_url="https://umami.example.com/api",
    website_id="12345678-1234-1234-1234-123456789012"
)

For more extensive examples, see the examples directory.

Try it out

In the examples directory, you can find examples for FastAPI and Starlette that you can try out easily. First, create a .env file in the examples directory with the following content:

UMAMI_API_ENDPOINT="https://example.com/api"
UMAMI_SITE_ID="your-site-id"

Then, install the necessary dependencies and run the example:

pip install uvicorn python-dotenv fastapi starlette 

# FastAPI:
python -m uvicorn examples.fastapi_app:app --env-file examples/.env

# Starlette:
python -m uvicorn examples.starlette_app:app --env-file examples/.env

Configuring the Middleware

The middleware has the following configuration options:

Parameter Type Description
api_url str The URL of your Umami API endpoint (e.g. https://umami.example.com/api)
website_id str The GUID of your website in Umami (e.g. 12345678-1234-1234-1234-123456789012
follow_redirects Optional[bool] Determines whether the POST request to the Umami API should follow redirects or not. Default is true.
proxy_enabled Optional[bool] If true, the middleware trusts the headers X-Forwarded-For, X-Forwarded-Host, and X-Real-IP. Default is false.
trusted_proxies Optional[List[str]] Hosts from which the headers mentioned above are trusted. Only used when proxy_enabled is set to true. If 0.0.0.0 is included, all hosts are trusted. Default is None.

Known issues

  • Country tracking does not seem to work properly. If you need this feature, open an issue and I'll take a deeper look into it.
  • Per-endpoint exclusion of tracking is not supported yet.

License

This project is licensed under the MIT License - see the LICENSE file for details.

umami-asgi's People

Contributors

tanikai avatar

Watchers

 avatar

umami-asgi's Issues

Country is not tracked correctly

When running this middleware behind a reverse proxy, the country cannot be tracked properly. This might be caused by incorrectly set headers.

image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.