Skip to main content

Track requests to your Flask server with Matomo

Project description

flask-matomo2

MIT License PyPI Docs

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 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

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

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

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

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()

Meta

Spraakbanken 2023-2024 - 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

Release Notes

Latest Changes

0.4.0 - 2024-03-04

Changed

0.3.0 - 2023-05-25

Added

0.2.0 - 2023-05-22

Changed

0.1.0

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.4.0.tar.gz (42.4 kB view hashes)

Uploaded Source

Built Distribution

flask_matomo2-0.4.0-py3-none-any.whl (7.4 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