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:
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()
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
.
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
.
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()
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()
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()
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
add metadata about python versions.
sort out documentation of params . PR #56 by @kod-kristoff.
flask-matomo2
.