flask-matomo2 is a library which lets you track the requests of your Flask website using Matomo (Piwik).
Forked from LucasHild/flask-matomo.
pip install flask-matomo2
Simply add flask-matomo2
to your dependencies:
# pyproject.toml
dependencies = [
"flask-matomo2",
]
poetry add flask-matomo2
pdm add flask-matomo2
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:
- The Matomo object is created by passing in the Flask application and arguments to configure Matomo.
- The matomo_url parameter is the url to your Matomo installation.
- 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.
- The token_auth parameter can be found in the area API in the settings of Matomo. It is required for tracking the ip address.
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()
Lucas Hild - https://lucas-hild.de This project is licensed under the MIT License - see the LICENSE file for details
- build(deps-dev): update ruff requirement from 0.0.267 to 0.0.270. PR #35 by @dependabot[bot].
- fix: allow for dont tracking based on user-agent. PR #34 by @kod-kristoff.
- Add PerfMsTracker. PR #33 by @kod-kristoff.
- Track original IP address if request was forwarded by proxy. Tanikai/flask-matomo by @Tanakai.
- Change ignored routes to compare against rules instead of endpoint. MSU-Libraries/flask-matomo by @meganschanz.
- Add ignored UserAgent prefix; set action to be url_rule. MSU-Libraries/flask-matomo by @natecollins.
- Fix matomo.ignore decorator.
- Handle request even if tracking fails. PR #30 by @kod-kristoff.
- Ignore routes by regex. PR #29 by @kod-kristoff.
- Make token_auth optional. PR #28 by @kod-kristoff.
- Track dynamic request data. PR #27 by @kod-kristoff.
- Also track request time. PR #26 by @kod-kristoff.
- Extend tracked variables. PR #25 by @kod-kristoff.
- fix matomo.details decorator. PR #19 by @kod-kristoff.
- Forked from LucasHild/flask-matomo.
- Renamed to
flask-matomo2
. - Add test suite.
- Setup CI with Github Actions.