From 0257299b5a2f0eb16f862244daafb9b5719175bd Mon Sep 17 00:00:00 2001 From: Sergey Motornyuk Date: Tue, 23 Aug 2022 17:11:41 +0300 Subject: [PATCH] feat: use gtag for G-* IDs --- ckanext/googleanalytics/helpers.py | 5 +++++ .../templates/googleanalytics/snippets/_ga.html | 14 ++++++++++++++ .../googleanalytics/snippets/_gtag.html | 16 ++++++++++++++++ .../snippets/googleanalytics_header.html | 15 +-------------- ckanext/googleanalytics/utils.py | 16 ++++++++++++++++ 5 files changed, 52 insertions(+), 14 deletions(-) create mode 100644 ckanext/googleanalytics/templates/googleanalytics/snippets/_ga.html create mode 100644 ckanext/googleanalytics/templates/googleanalytics/snippets/_gtag.html 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")