From 9fd3c4699a71e0bb88561835612a3af839c02dac Mon Sep 17 00:00:00 2001 From: scastineyras <162158563+scastineyras@users.noreply.github.com> Date: Fri, 25 Oct 2024 17:59:56 -0300 Subject: [PATCH] Add configurable measurement_id for internal analytics (#14) * add new property for frontend client side analytics, we will add an internal analytics solution to feed the standard dashboard portal metrics * added google analytics script for internal tracking --------- Co-authored-by: Sergio Castineyras Co-authored-by: Jay Guo --- ckanext/googleanalytics/config.py | 5 +++++ ckanext/googleanalytics/config_declaration.yaml | 5 +++++ ckanext/googleanalytics/helpers.py | 6 ++++++ .../templates/googleanalytics/snippets/_gtag.html | 11 +++++++++++ ckanext/googleanalytics/tests/test_view.py | 1 + test.ini | 1 + 6 files changed, 29 insertions(+) diff --git a/ckanext/googleanalytics/config.py b/ckanext/googleanalytics/config.py index 149e3dd1..04674319 100644 --- a/ckanext/googleanalytics/config.py +++ b/ckanext/googleanalytics/config.py @@ -21,6 +21,11 @@ def tracking_id(): return tk.config["googleanalytics.id"] +def opendata_measurement_id(): + # type: () -> str + return str(tk.config.get("googleanalytics.opendata_measurement_id", '')) + + def download_handler(): handler_path = tk.config.get(CONFIG_HANDLER_PATH) if handler_path: diff --git a/ckanext/googleanalytics/config_declaration.yaml b/ckanext/googleanalytics/config_declaration.yaml index 9ef0c2e9..4890cd38 100644 --- a/ckanext/googleanalytics/config_declaration.yaml +++ b/ckanext/googleanalytics/config_declaration.yaml @@ -56,3 +56,8 @@ groups: - key: googleanalytics.recent_view_days default: 14 + + - key: googleanalytics.opendata_measurement_id + default: G-111X1XXXXX + description: | + The internal OpenData measurement ID for Google Analytics 4. diff --git a/ckanext/googleanalytics/helpers.py b/ckanext/googleanalytics/helpers.py index ddd75717..838e4322 100644 --- a/ckanext/googleanalytics/helpers.py +++ b/ckanext/googleanalytics/helpers.py @@ -7,6 +7,7 @@ def get_helpers(): return { "googleanalytics_header": googleanalytics_header, "googleanalytics_id": googleanalytics_id, + "googleanalytics_opendata_measurement_id": googleanalytics_opendata_measurement_id, "googleanalytics_resource_prefix": googleanalytics_resource_prefix, "googleanalytics_tracking_mode": googleanalytics_tracking_mode, } @@ -33,6 +34,7 @@ def googleanalytics_header(): data = { "googleanalytics_id": config.tracking_id(), + "googleanalytics_opendata_measurement_id": config.opendata_measurement_id(), "googleanalytics_domain": config.domain(), "googleanalytics_fields": str(fields), "googleanalytics_linked_domains": config.linked_domains(), @@ -48,3 +50,7 @@ def googleanalytics_tracking_mode(): def googleanalytics_id(): return config.tracking_id() + + +def googleanalytics_opendata_measurement_id(): + return config.opendata_measurement_id() diff --git a/ckanext/googleanalytics/templates/googleanalytics/snippets/_gtag.html b/ckanext/googleanalytics/templates/googleanalytics/snippets/_gtag.html index fa0ee913..c28b1209 100644 --- a/ckanext/googleanalytics/templates/googleanalytics/snippets/_gtag.html +++ b/ckanext/googleanalytics/templates/googleanalytics/snippets/_gtag.html @@ -23,4 +23,15 @@ + {% with measurement_id = h.googleanalytics_opendata_measurement_id() %} + {% if measurement_id %} + + + {% endif %} + {% endwith %} {% endblock %} diff --git a/ckanext/googleanalytics/tests/test_view.py b/ckanext/googleanalytics/tests/test_view.py index 03162df2..d32331fb 100644 --- a/ckanext/googleanalytics/tests/test_view.py +++ b/ckanext/googleanalytics/tests/test_view.py @@ -9,6 +9,7 @@ def _render_header(mode, tracking_id): "googleanalytics_id": tracking_id, "googleanalytics_domain": config.domain(), "googleanalytics_fields": config.fields(), + "googleanalytics_opendata_measurement_id": config.opendata_measurement_id(), "googleanalytics_linked_domains": config.linked_domains() }) diff --git a/test.ini b/test.ini index f112f25c..df09e2ce 100644 --- a/test.ini +++ b/test.ini @@ -11,6 +11,7 @@ use = config:../ckan/test-core.ini ckan.plugins = googleanalytics googleanalytics.id = UA-000000000-1 +googleanalytics.opendata_measurement_id = G-0000000000 # Logging configuration [loggers]