Skip to content

Commit 1a5abf5

Browse files
committed
chore: add google analytics configuration
The key can be customised in the application configuration. ⚠️ add RERO_ILS_GOOGLE_ANALYTICS_TAG_ID on the configuration. Co-Authored-by: Bertrand Zuchuat <[email protected]>
1 parent 2b8e0bb commit 1a5abf5

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

rero_ils/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ def _(x):
211211
# Google
212212
# =======================
213213
# THEME_GOOGLE_SITE_VERIFICATION = []
214+
# Example of id: UA-XXXXXX-XX
215+
# RERO_ILS_GOOGLE_ANALYTICS_TAG_ID =
214216

215217
# Miscellaneous variable around templates
216218
# =======================

rero_ils/theme/templates/rero_ils/trackingcode.html

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!--
22
33
RERO ILS
4-
Copyright (C) 2019 RERO
4+
Copyright (C) 2019-2024 RERO
55
66
This program is free software: you can redistribute it and/or modify
77
it under the terms of the GNU Affero General Public License as published by
@@ -16,12 +16,17 @@
1616
along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
1818
-->
19+
{% if config.get('RERO_ILS_GOOGLE_ANALYTICS_TAG_ID') %}
1920
<!-- Global site tag (gtag.js) - Google Analytics -->
20-
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-1831395-14"></script>
21+
22+
{% set TAG_ID = config.get('RERO_ILS_GOOGLE_ANALYTICS_TAG_ID') %}
23+
<script async src="https://www.googletagmanager.com/gtag/js?id={{ TAG_ID }}"></script>
2124
<script>
2225
window.dataLayer = window.dataLayer || [];
2326
function gtag(){dataLayer.push(arguments);}
2427
gtag('js', new Date());
2528

26-
gtag('config', 'UA-1831395-14');
29+
gtag('config', '{{ TAG_ID }}');
2730
</script>
31+
32+
{% endif %}

tests/ui/test_views.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,15 @@ def test_set_user_name(
205205
login_user(user=librarian_martigny.user)
206206
assert session['user_name'] == librarian_martigny.formatted_name
207207
logout_user()
208+
209+
210+
def test_google_analytics(client, app):
211+
"""Testing the insertion of the google analytics code in the html page."""
212+
# The Google Analytics code must not be present on the page.
213+
result = client.get(url_for('rero_ils.index'))
214+
assert 'gtag' not in result.text
215+
216+
# The Google Analytics code must be present on the page.
217+
app.config['RERO_ILS_GOOGLE_ANALYTICS_TAG_ID'] = 'GA-Foo'
218+
result = client.get(url_for('rero_ils.index'))
219+
assert 'gtag' in result.text

0 commit comments

Comments
 (0)