From fb75328e77d53d1b6081e8758eee3bdd1540bb5b Mon Sep 17 00:00:00 2001 From: MZhiyenbayev Date: Thu, 8 Jun 2023 11:45:06 +0600 Subject: [PATCH] [ga][s]: updated js scripts to support GA4 property --- .../js/google_analytics_event_tracking.js | 5 ++++- .../nhs/fanstatic/js/google_analytics_header.js | 14 ++++---------- ckanext/nhs/templates/base.html | 1 + 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/ckanext/nhs/fanstatic/js/google_analytics_event_tracking.js b/ckanext/nhs/fanstatic/js/google_analytics_event_tracking.js index 634bc3e..1c00aa4 100644 --- a/ckanext/nhs/fanstatic/js/google_analytics_event_tracking.js +++ b/ckanext/nhs/fanstatic/js/google_analytics_event_tracking.js @@ -9,7 +9,10 @@ this.ckan.module('google_analytics_event_tracking', function(jQuery, _) { jQuery('a.resource-url-analytics').on('click', function() { var resource_name = (jQuery(this).prop('href')).split('/').pop(); if (resource_name && analytics_enabled) { - ga('send', 'event', 'Resource', 'Download', resource_name); + gtag('event', "Download", { + 'event_category': "Resource", + 'event_label': resource_name, + }); } }); }); diff --git a/ckanext/nhs/fanstatic/js/google_analytics_header.js b/ckanext/nhs/fanstatic/js/google_analytics_header.js index 2de28c9..bbecec5 100644 --- a/ckanext/nhs/fanstatic/js/google_analytics_header.js +++ b/ckanext/nhs/fanstatic/js/google_analytics_header.js @@ -6,16 +6,10 @@ ckan.module('google_analytics_header', function(jQuery, _) { this.sandbox.subscribe('analytics_enabled', function (analytics_enabled) { if(analytics_enabled){ - (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ - (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), - m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) - })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); - - - ga('create', googleanalytics_id, 'auto'); - ga('set', 'anonymizeIp', true); - ga('send', 'pageview'); - + window.dataLayer = window.dataLayer || []; + function gtag(){dataLayer.push(arguments);} + gtag('js', new Date()); + gtag('config', googleanalytics_id); } }); }, diff --git a/ckanext/nhs/templates/base.html b/ckanext/nhs/templates/base.html index 5204edc..000bae8 100644 --- a/ckanext/nhs/templates/base.html +++ b/ckanext/nhs/templates/base.html @@ -34,6 +34,7 @@ {% block googleanalytics_header %} {% resource 'nhs/js/google_analytics_header.js' %} {% set googleanalytics_config = h.get_googleanalytics_config() %} +