Skip to content

Commit

Permalink
feat: add matomo tracking site
Browse files Browse the repository at this point in the history
Reviewed-by: andriac
  • Loading branch information
root authored and andriacap committed Apr 17, 2024
1 parent c560a91 commit 25b8510
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
6 changes: 6 additions & 0 deletions atlas/atlasRoutes.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@ def index():
else:
lastDiscoveries = []

if current_app.config["MATOMO_URL"] != "":
matomo_script = current_app.config["MATOMO_SCRIPT"]
else:
matomo_script = ""

connection.close()
session.close()

Expand All @@ -213,6 +218,7 @@ def index():
mostViewTaxon=mostViewTaxon,
customStatMedias=customStatMedias,
lastDiscoveries=lastDiscoveries,
matomo_script=matomo_script,
)


Expand Down
5 changes: 5 additions & 0 deletions atlas/configuration/config.py.example
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ LANGUAGES = {
# Code de suivi des statistiques Google Analytics (si AFFICHAGE_FOOTER = True)
ID_GOOGLE_ANALYTICS = "UA-xxxxxxx-xx"

# Suivi de statistiques avec MATOMO si MATOMO_URL != ""
MATOMO_URL = "" # URL lié à votre MATOMO
MATOMO_SITE_ID = 1 # id du site suivi qui correspond ici à celui du GeoNature Atlas (votre site doit être renseigné dans MATOMO)
#### LIEN AIDE MATOMO : https://developer.matomo.org/guides/tracking-javascript-guide

# Utiliser et afficher le glossaire (static/custom/glossaire.json.sample)
GLOSSAIRE = False

Expand Down
19 changes: 18 additions & 1 deletion atlas/configuration/config_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@
},
}

# LATEST VERSION OF MATOMO
MATOMO_SCRIPT_TO_INCLUDE= """
var _paq = _paq || [];
var _paq = window._paq = window._paq || [];
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u="//url_matomo/";
_paq.push(['setTrackerUrl', u+'matomo.php']);
_paq.push(['setSiteId', site_id]);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.type='text/javascript'; g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
})();
"""


class SecretSchemaConf(Schema):
class Meta:
Expand Down Expand Up @@ -208,7 +223,9 @@ class Meta:
# (no need to restart the atlas service when updating templates)
# Defaults to False to have the best performance in production
TEMPLATES_AUTO_RELOAD = fields.Boolean(load_default=False)

MATOMO_SCRIPT = fields.String(load_default=MATOMO_SCRIPT_TO_INCLUDE)
MATOMO_URL = fields.String(load_default="")
MATOMO_SITE_ID = fields.Integer(load_default=0)
@validates_schema
def validate_url_taxhub(self, data, **kwargs):
"""
Expand Down
13 changes: 13 additions & 0 deletions atlas/templates/core/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@
{% endblock %}
<link rel="stylesheet" href="{{ url_for('static', filename='custom/custom.css') }}"/>
{% block metaTags %}{% endblock %}

{% if configuration.MATOMO_URL and configuration.MATOMO_SITE_ID is defined %}
{% set site_id = configuration.MATOMO_SITE_ID | int(default=-1) %}
{% set url_matomo = configuration.MATOMO_URL %}
<!-- Matomo -->
<script type="text/javascript">
var url_matomo = "{{ url_matomo }}";
var site_id = {{ site_id | int }};
{{ matomo_script | safe }}
</script>
<!-- End Matomo Code -->
{% endif %}

</head>

<body>
Expand Down

0 comments on commit 25b8510

Please sign in to comment.