Skip to content

Commit

Permalink
Add configurable measurement_id for internal analytics (#14)
Browse files Browse the repository at this point in the history
* 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 <sercasti@gmail.com>
Co-authored-by: Jay Guo <jguo144@gmail.com>
  • Loading branch information
3 people authored Oct 25, 2024
1 parent e0defae commit 9fd3c46
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 0 deletions.
5 changes: 5 additions & 0 deletions ckanext/googleanalytics/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions ckanext/googleanalytics/config_declaration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
6 changes: 6 additions & 0 deletions ckanext/googleanalytics/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -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(),
Expand All @@ -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()
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,15 @@

</script>

{% with measurement_id = h.googleanalytics_opendata_measurement_id() %}
{% if measurement_id %}
<script async src="https://www.googletagmanager.com/gtag/js?id={{ measurement_id }}"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '{{ measurement_id }}');
</script>
{% endif %}
{% endwith %}
{% endblock %}
1 change: 1 addition & 0 deletions ckanext/googleanalytics/tests/test_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})

Expand Down
1 change: 1 addition & 0 deletions test.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 9fd3c46

Please sign in to comment.