Skip to main content

Track requests to your Flask server with Matomo

Project description

flask-matomo2

PyPI version PyPI - Python Version PyPI - Downloads Docs MIT License

Maturity badge - level 3 Stage

Code Coverage

CI(check) CI(release) CI(scheduled) CI(test) Documentation Status

flask-matomo2 is a library which lets you track the requests of your Flask website using Matomo (Piwik).

Forked from LucasHild/flask-matomo.

Installation

pip install flask-matomo2

Using flask-matomo2 in your project

Simply add flask-matomo2 to your dependencies:

# pyproject.toml
dependencies = [
  "flask-matomo2",
]

Using uv

uv add flask-matomo2

Using Poetry

poetry add flask-matomo2

Using PDM

pdm add flask-matomo2

Usage

from flask import Flask, jsonify
from flask_matomo2 import Matomo

app = Flask(__name__)
matomo = Matomo(
    app, 
    matomo_url="https://matomo.mydomain.com",
    id_site=5, token_auth="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")

@app.route("/")
def index():
  return jsonify({"page": "index"})

if __name__ == "__main__":
  app.run()

In the code above:

  1. The Matomo object is created by passing in the Flask application and arguments to configure Matomo.
  2. The matomo_url parameter is the url to your Matomo installation.
  3. The id_site parameter is the id of your site. This is used if you track several websites with one Matomo installation. It can be found if you open your Matomo dashboard, change to site you want to track and look for &idSite= in the url.
  4. The token_auth parameter can be found in the area API in the settings of Matomo. It is required for tracking the ip address.

Adding details to route

You can provide details to a route in 2 ways, first by using the matomo.details decorator:

from flask import Flask, jsonify
from flask_matomo2 import Matomo

matomo = Matomo(
    matomo_url="https://matomo.mydomain.com",
    id_site=5, token_auth="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
app = Flask(__name__)
matomo.init_app(app)

@app.route("/foo")
@matomo.details(action_name="Foo")
def foo():
  return jsonify({"page": "foo"})

if __name__ == "__main__":
  app.run()

Here the Matomo object is created before the Flask object and then calling init_app. Or by giving details to the Matomo constructor:

from flask import Flask, jsonify
from flask_matomo2 import Matomo

app = Flask(__name__)
matomo = Matomo(
  app,
  matomo_url="https://matomo.mydomain.com",
  id_site=5,
  token_auth="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
  routes_details={
    "/foo": {
      "action_name": "Foo"
    }
  }
)

@app.route("/foo")
def foo():
  return jsonify({"page": "foo"})

if __name__ == "__main__":
  app.run()

Adjusting the tracked url

If your app is behind a proxy and you don't adjust the url in any other way, you can adjust the tracked url by setting base_url without trailing / in either the constructor:

from flask import Flask, jsonify
from flask_matomo2 import Matomo

matomo = Matomo(
    matomo_url="https://matomo.mydomain.com",
    id_site=5,
    base_url="https://mydomain.com/apps")
app = Flask(__name__)
matomo.init_app(app)

@app.route("/foo")
@matomo.details(action_name="Foo")
def foo():
  return jsonify({"page": "foo"})

if __name__ == "__main__":
  app.run()

Or a call to activate:

from flask import Flask, jsonify
from flask_matomo2 import Matomo

matomo = Matomo.activate_later()

matomo.activate(
    matomo_url="https://matomo.mydomain.com",
    id_site=5, 
    base_url="https://mydomain.com/apps"
)
app = Flask(__name__)
matomo.init_app(app)

@app.route("/foo")
@matomo.details(action_name="Foo")
def foo():
  return jsonify({"page": "foo"})

if __name__ == "__main__":
  app.run()

The result is that a request to /foo will be tracked as https://mydomain.com/apps/foo.

Using a custom client

By default, Matomo uses httpx.Client to make the tracking call. You can override this by setting client as long as the client uses the same api as httpx:s Client.

Setting timeout for client requests

By default, Matomo uses a 5 seconds timeout for the client. You can override this timeout by setting http_timeout. Note that this settings will be ignored if you provide a custom client.

Ignoring a route

You can ignore tracking a route by decorating the route with @matomo.ignore():

from flask import Flask, jsonify
from flask_matomo2 import Matomo

matomo = Matomo(
    matomo_url="https://matomo.mydomain.com",
    id_site=5,
)
app = Flask(__name__)
matomo.init_app(app)

@app.route("/foo")
@matomo.ignore()
def foo():
  return jsonify({"page": "foo"})

if __name__ == "__main__":
  app.run()

Or ignore the route in the matomo constructor:

from flask import Flask, jsonify
from flask_matomo2 import Matomo

matomo = Matomo(
    matomo_url="https://matomo.mydomain.com",
    id_site=5,
    ignored_routes=["/foo"]
)
app = Flask(__name__)
matomo.init_app(app)

@app.route("/foo")
def foo():
  return jsonify({"page": "foo"})

if __name__ == "__main__":
  app.run()

Ignore routes by patterns

You can also ignore routes by giving a list of regexes to the constructor:

from flask import Flask, jsonify
from flask_matomo2 import Matomo

matomo = Matomo(
    matomo_url="https://matomo.mydomain.com",
    id_site=5,
    ignored_patterns=["/fo.*"]
)
app = Flask(__name__)
matomo.init_app(app)

@app.route("/foo")
def foo():
  return jsonify({"page": "foo"})

if __name__ == "__main__":
  app.run()

Ignore requests by User-Agent patterns

You can supply regex patterns to ignore request based on User-Agent:

from flask import Flask, jsonify
from flask_matomo2 import Matomo

matomo = Matomo(
    matomo_url="https://matomo.mydomain.com",
    id_site=5,
    ignored_ua_patterns=[".*bot.*"]
)
app = Flask(__name__)
matomo.init_app(app)

@app.route("/foo")
def foo():
  return jsonify({"page": "foo"})

if __name__ == "__main__":
  app.run()

Meta

Spraakbanken 2023-2025 - https://spraakbanken.gu.se Lucas Hild (original project Flask-Matomo)- https://lucas-hild.de This project is licensed under the MIT License - see the LICENSE file for details

This project keeps a changelog.

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

flask_matomo2-0.6.0.tar.gz (55.6 kB view details)

Uploaded Source

Built Distribution

flask_matomo2-0.6.0-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file flask_matomo2-0.6.0.tar.gz.

File metadata

  • Download URL: flask_matomo2-0.6.0.tar.gz
  • Upload date:
  • Size: 55.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for flask_matomo2-0.6.0.tar.gz
Algorithm Hash digest
SHA256 c413ad783297c4277e4cffef1620280b7683506979053ee7a3b66af29e934c64
MD5 667580d1047b8aa9c928fa7d1350822b
BLAKE2b-256 2b583cff41b8519f680300767e324d03eca7117561890a3a7c9a09c9d40e6c3a

See more details on using hashes here.

Provenance

The following attestation bundles were made for flask_matomo2-0.6.0.tar.gz:

Publisher: release.yml on spraakbanken/flask-matomo2

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file flask_matomo2-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: flask_matomo2-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for flask_matomo2-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a744d60a6f8fae4f3770a5ca10cd8a956307ec631a84eb496ab532313c38581d
MD5 3e7ca98a30127b79f2752c97075814d2
BLAKE2b-256 b14a5b8d2e52e6e6bd2a5b0b7460189495e4d942a64c2b6f957e8bd1552dc570

See more details on using hashes here.

Provenance

The following attestation bundles were made for flask_matomo2-0.6.0-py3-none-any.whl:

Publisher: release.yml on spraakbanken/flask-matomo2

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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