Skip to content

Commit e822203

Browse files
rootandriacap
root
authored andcommitted
feat: add matomo tracking site
Reviewed-by: andriac
1 parent c560a91 commit e822203

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

atlas/atlasRoutes.py

+8
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,13 @@ def index():
204204
else:
205205
lastDiscoveries = []
206206

207+
if current_app.config["MATOMO_URL"] != "":
208+
matomo_script = current_app.config["MATOMO_SCRIPT"]
209+
else:
210+
matomo_script = ""
211+
212+
213+
207214
connection.close()
208215
session.close()
209216

@@ -213,6 +220,7 @@ def index():
213220
mostViewTaxon=mostViewTaxon,
214221
customStatMedias=customStatMedias,
215222
lastDiscoveries=lastDiscoveries,
223+
matomo_script=matomo_script,
216224
)
217225

218226

atlas/configuration/config.py.example

+5
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ LANGUAGES = {
8787
# Code de suivi des statistiques Google Analytics (si AFFICHAGE_FOOTER = True)
8888
ID_GOOGLE_ANALYTICS = "UA-xxxxxxx-xx"
8989

90+
# Suivi de statistiques avec MATOMO si MATOMO_URL != ""
91+
MATOMO_URL = "" # URL lié à votre MATOMO
92+
MATOMO_SITE_ID = 1 # id du site suivi qui correspond ici à celui du GeoNature Atlas (votre site doit être renseigné dans MATOMO)
93+
#### LIEN AIDE MATOMO : https://developer.matomo.org/guides/tracking-javascript-guide
94+
9095
# Utiliser et afficher le glossaire (static/custom/glossaire.json.sample)
9196
GLOSSAIRE = False
9297

atlas/configuration/config_schema.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,21 @@
7777
},
7878
}
7979

80+
# LATEST VERSION OF MATOMO
81+
MATOMO_SCRIPT_TO_INCLUDE= """
82+
var _paq = _paq || [];
83+
var _paq = window._paq = window._paq || [];
84+
_paq.push(['trackPageView']);
85+
_paq.push(['enableLinkTracking']);
86+
(function() {
87+
var u="//url_matomo/";
88+
_paq.push(['setTrackerUrl', u+'matomo.php']);
89+
_paq.push(['setSiteId', site_id]);
90+
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
91+
g.type='text/javascript'; g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
92+
})();
93+
"""
94+
8095

8196
class SecretSchemaConf(Schema):
8297
class Meta:
@@ -208,7 +223,9 @@ class Meta:
208223
# (no need to restart the atlas service when updating templates)
209224
# Defaults to False to have the best performance in production
210225
TEMPLATES_AUTO_RELOAD = fields.Boolean(load_default=False)
211-
226+
MATOMO_SCRIPT = fields.String(load_default=MATOMO_SCRIPT_TO_INCLUDE)
227+
MATOMO_URL = fields.String(load_default="")
228+
MATOMO_SITE_ID = fields.Integer(load_default=0)
212229
@validates_schema
213230
def validate_url_taxhub(self, data, **kwargs):
214231
"""

atlas/templates/core/layout.html

+13
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@
1515
{% endblock %}
1616
<link rel="stylesheet" href="{{ url_for('static', filename='custom/custom.css') }}"/>
1717
{% block metaTags %}{% endblock %}
18+
19+
{% if configuration.MATOMO_URL and configuration.MATOMO_SITE_ID is defined %}
20+
{% set site_id = configuration.MATOMO_SITE_ID | int(default=-1) %}
21+
{% set url_matomo = configuration.MATOMO_URL %}
22+
<!-- Matomo -->
23+
<script type="text/javascript">
24+
var url_matomo = "{{ url_matomo }}";
25+
var site_id = {{ site_id | int }};
26+
{{ matomo_script | safe }}
27+
</script>
28+
<!-- End Matomo Code -->
29+
{% endif %}
30+
1831
</head>
1932

2033
<body>

0 commit comments

Comments
 (0)