diff --git a/ckanext/googleanalytics/helpers.py b/ckanext/googleanalytics/helpers.py index b1a6de0..da8b3ee 100644 --- a/ckanext/googleanalytics/helpers.py +++ b/ckanext/googleanalytics/helpers.py @@ -6,6 +6,7 @@ def get_helpers(): return { "googleanalytics_header": googleanalytics_header, "googleanalytics_resource_prefix": googleanalytics_resource_prefix, + "googleanalytics_tracking_mode": googleanalytics_tracking_mode, } @@ -37,3 +38,7 @@ def googleanalytics_header(): return tk.render_snippet( "googleanalytics/snippets/googleanalytics_header.html", data ) + + +def googleanalytics_tracking_mode(): + return utils.config_tracking_mode() diff --git a/ckanext/googleanalytics/templates/googleanalytics/snippets/_ga.html b/ckanext/googleanalytics/templates/googleanalytics/snippets/_ga.html new file mode 100644 index 0000000..2495e1d --- /dev/null +++ b/ckanext/googleanalytics/templates/googleanalytics/snippets/_ga.html @@ -0,0 +1,14 @@ + diff --git a/ckanext/googleanalytics/templates/googleanalytics/snippets/_gtag.html b/ckanext/googleanalytics/templates/googleanalytics/snippets/_gtag.html new file mode 100644 index 0000000..2ed2219 --- /dev/null +++ b/ckanext/googleanalytics/templates/googleanalytics/snippets/_gtag.html @@ -0,0 +1,16 @@ + + + diff --git a/ckanext/googleanalytics/templates/googleanalytics/snippets/googleanalytics_header.html b/ckanext/googleanalytics/templates/googleanalytics/snippets/googleanalytics_header.html index f9e5bdf..3588998 100644 --- a/ckanext/googleanalytics/templates/googleanalytics/snippets/googleanalytics_header.html +++ b/ckanext/googleanalytics/templates/googleanalytics/snippets/googleanalytics_header.html @@ -1,14 +1 @@ - +{% include 'googleanalytics/snippets/_' ~ h.googleanalytics_tracking_mode() ~ '.html' %} diff --git a/ckanext/googleanalytics/utils.py b/ckanext/googleanalytics/utils.py index 83109aa..3aeef78 100644 --- a/ckanext/googleanalytics/utils.py +++ b/ckanext/googleanalytics/utils.py @@ -10,6 +10,22 @@ def config_id(): return tk.config["googleanalytics.id"] +def config_tracking_mode(): + type_ = tk.config.get("googleanalytics.tracking_mode") + if type_: + return type_ + + id_ = config_id() + + if id_.startswith("UA-"): + return "ga" + + if id_.startswith("G-"): + return "gtag" + + return "ga" + + def config_account(): return tk.config.get("googleanalytics.account")