diff --git a/ckanext/googleanalytics/config.py b/ckanext/googleanalytics/config.py index 3711479..929950b 100644 --- a/ckanext/googleanalytics/config.py +++ b/ckanext/googleanalytics/config.py @@ -32,7 +32,7 @@ def download_handler(): def tracking_mode(): - # type: () -> Literal["ga", "gtag"] + # type: () -> Literal["ga", "gtag", "gtm"] type_ = tk.config.get("googleanalytics.tracking_mode") if type_: return type_ @@ -45,6 +45,10 @@ def tracking_mode(): if id_.startswith("G-"): return "gtag" + if id_.startswith("GTM-"): + return "gtm" + + return "ga" diff --git a/ckanext/googleanalytics/helpers.py b/ckanext/googleanalytics/helpers.py index 0754d99..52be647 100644 --- a/ckanext/googleanalytics/helpers.py +++ b/ckanext/googleanalytics/helpers.py @@ -6,6 +6,7 @@ from ckanext.googleanalytics import config def get_helpers(): return { "googleanalytics_header": googleanalytics_header, + "googleanalytics_id": googleanalytics_id, "googleanalytics_resource_prefix": googleanalytics_resource_prefix, "googleanalytics_tracking_mode": googleanalytics_tracking_mode, } @@ -43,3 +44,6 @@ def googleanalytics_header(): def googleanalytics_tracking_mode(): return config.tracking_mode() + +def googleanalytics_id(): + return config.tracking_id() diff --git a/ckanext/googleanalytics/templates/googleanalytics/snippets/_gtm.html b/ckanext/googleanalytics/templates/googleanalytics/snippets/_gtm.html new file mode 100644 index 0000000..d74f7a9 --- /dev/null +++ b/ckanext/googleanalytics/templates/googleanalytics/snippets/_gtm.html @@ -0,0 +1,7 @@ + + + diff --git a/ckanext/googleanalytics/templates/page.html b/ckanext/googleanalytics/templates/page.html new file mode 100644 index 0000000..9278733 --- /dev/null +++ b/ckanext/googleanalytics/templates/page.html @@ -0,0 +1,16 @@ +{% ckan_extends %} + +{% block page %} + {% block googleanalytics_body_script %} + {% if h.googleanalytics_tracking_mode() == "gtm" %} + {% with id = h.googleanalytics_id() %} + + + + {% endwith %} + {% endif %} + {% endblock %} + + {{ super() }} +{% endblock page %}