Track requests to your Flask server with Matomo
Project description
Flask-Matomo
Flask-Matomo 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-matomo
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:
- 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.
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
Lucas Hild - https://lucas-hild.de This project is licensed under the MIT License - see the LICENSE file for details
Release Notes
Latest Changes
0.2.0 - 2023-05-22
Changed
- 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.
0.1.0
- Forked from LucasHild/flask-matomo.
- Renamed to
flask-matomo2
. - Add test suite.
- Setup CI with Github Actions.
Project details
Release history Release notifications | RSS feed
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.2.1.tar.gz
(5.8 kB
view hashes)
Built Distribution
Close
Hashes for flask_matomo2-0.2.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5bffa510eddf3c3629fab38bf768eef952ac45f32827e72a89ff6971223aa589 |
|
MD5 | 331000eca437dbf43886d73f3bc19542 |
|
BLAKE2b-256 | b8f0a094ce371dbfd991d5caa2a7e537ae473abc3c878563b95af4e7228dd506 |