From 1bb138bb41b2554e03d3b8358705cda7226a8f2a Mon Sep 17 00:00:00 2001 From: fiammybe Date: Sat, 11 Mar 2023 22:07:46 +0100 Subject: [PATCH 1/6] adding preload that distinguishes between UA an GA4 --- htdocs/plugins/preloads/googleanalytics.php | 64 +++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 htdocs/plugins/preloads/googleanalytics.php diff --git a/htdocs/plugins/preloads/googleanalytics.php b/htdocs/plugins/preloads/googleanalytics.php new file mode 100644 index 000000000000..defbabcac82e --- /dev/null +++ b/htdocs/plugins/preloads/googleanalytics.php @@ -0,0 +1,64 @@ +insertGA4Tag($icmsConfigMetaFooter['use_google_analytics']); + } + else + { + $this->insertUniversalAnalyticsTag($icmsConfigMetaFooter['use_google_analytics']); + } + } + else { + echo 'error getting icmsConfigMetaFooter'; + } + } + catch (Exception $e) { + echo $e->getMessage(); + } + } + + function insertUniversalAnalyticsTag(string $UAtag) + { + $icmsTheme->addScript('', '', '(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\', \'UA-' . $UAtag . '\', \'auto\'); + ga(\'send\', \'pageview\');', 'module', '2001'); + + } + + function insertGA4Tag(string $GA4tag) + { + $icmsTheme->addScript('https://www.googletagmanager.com/gtag/js?id=' . $GA4tag, ['async'], '', 'module', 2000); + $icmsTheme->addScript('', '', 'window.dataLayer = window.dataLayer || []; + function gtag(){dataLayer.push(arguments);} + gtag(\'js\', new Date()); + + gtag(\'config\', \'' . $GA4tag . '\');', 'module', '2001'); + + } +} \ No newline at end of file From 3391fb5a70e85a4c7dfa1d82341f9a500c9a0ea7 Mon Sep 17 00:00:00 2001 From: fiammybe Date: Sat, 11 Mar 2023 22:09:45 +0100 Subject: [PATCH 2/6] remove UA script insert in object.php --- htdocs/libraries/icms/view/theme/Object.php | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/htdocs/libraries/icms/view/theme/Object.php b/htdocs/libraries/icms/view/theme/Object.php index e1a4a205f48f..63711a32be96 100644 --- a/htdocs/libraries/icms/view/theme/Object.php +++ b/htdocs/libraries/icms/view/theme/Object.php @@ -210,19 +210,6 @@ public function xoInit($options = array()) { $this->addMeta('meta', substr($name, 5), $value); } elseif (substr($name, 0, 6) == 'footer') { $values = $value; - if ($icmsConfigMetaFooter['use_google_analytics'] == TRUE && isset($icmsConfigMetaFooter['google_analytics']) && $icmsConfigMetaFooter['google_analytics'] != '') { - - $values = $value . ""; - } $this->template->assign("xoops_$name", $values); $this->template->assign("icms_$name", $values); } else { From 44091e9b161281a13aeb90dfd94eb9746991582d Mon Sep 17 00:00:00 2001 From: fiammybe Date: Sat, 11 Mar 2023 22:13:45 +0100 Subject: [PATCH 3/6] cleanup old unused analytics include --- htdocs/header.php | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/htdocs/header.php b/htdocs/header.php index 3e9641e20793..9a6255ad2e9a 100644 --- a/htdocs/header.php +++ b/htdocs/header.php @@ -60,20 +60,7 @@ */ $icmsTheme = $xoTheme = &$xoopsThemeFactory->createInstance(array('contentTemplate' => @$xoopsOption['template_main'])); $xoopsTpl = $icmsTpl = &$xoTheme->template; -// no longer needed because of ticket #751 -// if ($icmsConfigMetaFooter['use_google_analytics'] === TRUE -// && isset($icmsConfigMetaFooter['google_analytics']) && $icmsConfigMetaFooter['google_analytics'] != '') { -// /* Legacy GA urchin code */ -// //$xoTheme->addScript('http://www.google-analytics.com/urchin.js',array('type' => 'text/javascript'),'_uacct = "UA-' . $icmsConfigMetaFooter['google_analytics'] . '";urchinTracker();'); -// $scheme = parse_url(ICMS_URL, PHP_URL_SCHEME); -// if ($scheme == 'http') { -// /* New GA code, http protocol */ -// $xoTheme->addScript('http://www.google-analytics.com/ga.js', array('type' => 'text/javascript'),''); -// } elseif ($scheme == 'https') { -// /* New GA code, https protocol */ -// $xoTheme->addScript('https://ssl.google-analytics.com/ga.js', array('type' => 'text/javascript'),''); -// } -// } + if (isset($icmsConfigMetaFooter['google_meta']) && $icmsConfigMetaFooter['google_meta'] != '') { $xoTheme->addMeta('meta', 'verify-v1', $icmsConfigMetaFooter['google_meta']); $xoTheme->addMeta('meta', 'google-site-verification', $icmsConfigMetaFooter['google_meta']); From aff1a411cef8fa241c8e197b1615856908d32366 Mon Sep 17 00:00:00 2001 From: David Janssens Date: Fri, 17 Mar 2023 00:03:02 +0100 Subject: [PATCH 4/6] Update googleanalytics.php changed 'async' => 'async' --- htdocs/plugins/preloads/googleanalytics.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/htdocs/plugins/preloads/googleanalytics.php b/htdocs/plugins/preloads/googleanalytics.php index defbabcac82e..ee185625322c 100644 --- a/htdocs/plugins/preloads/googleanalytics.php +++ b/htdocs/plugins/preloads/googleanalytics.php @@ -53,7 +53,7 @@ function insertUniversalAnalyticsTag(string $UAtag) function insertGA4Tag(string $GA4tag) { - $icmsTheme->addScript('https://www.googletagmanager.com/gtag/js?id=' . $GA4tag, ['async'], '', 'module', 2000); + $icmsTheme->addScript('https://www.googletagmanager.com/gtag/js?id=' . $GA4tag, ['async' => 'async'], '', 'module', 2000); $icmsTheme->addScript('', '', 'window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag(\'js\', new Date()); @@ -61,4 +61,4 @@ function gtag(){dataLayer.push(arguments);} gtag(\'config\', \'' . $GA4tag . '\');', 'module', '2001'); } -} \ No newline at end of file +} From c13b57ea8569b8a80af3a0ea38ca9c1b231f2921 Mon Sep 17 00:00:00 2001 From: David Janssens Date: Tue, 30 May 2023 23:02:25 +0200 Subject: [PATCH 5/6] correct tag variable and remove echo --- htdocs/plugins/preloads/googleanalytics.php | 22 ++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/htdocs/plugins/preloads/googleanalytics.php b/htdocs/plugins/preloads/googleanalytics.php index ee185625322c..08601f2994c0 100644 --- a/htdocs/plugins/preloads/googleanalytics.php +++ b/htdocs/plugins/preloads/googleanalytics.php @@ -19,20 +19,20 @@ function eventBeforeFooter() try { - - if ($icmsConfigMetaFooter['use_google_analytics'] && isset($icmsConfigMetaFooter['google_analytics']) && $icmsConfigMetaFooter['google_analytics'] != '') { - if(substr( $icmsConfigMetaFooter['use_google_analytics'], 0, 2 ) === "G-") - { - $this->insertGA4Tag($icmsConfigMetaFooter['use_google_analytics']); - } - else + if ($icmsConfigMetaFooter['use_google_analytics']) + { + if(isset($icmsConfigMetaFooter['google_analytics']) && $icmsConfigMetaFooter['google_analytics'] != '') { - $this->insertUniversalAnalyticsTag($icmsConfigMetaFooter['use_google_analytics']); + if(substr( $icmsConfigMetaFooter['use_google_analytics'], 0, 2 ) === "G-") + { + $this->insertGA4Tag($icmsConfigMetaFooter['google_analytics']); + } + else + { + $this->insertUniversalAnalyticsTag($icmsConfigMetaFooter['google_analytics']); + } } } - else { - echo 'error getting icmsConfigMetaFooter'; - } } catch (Exception $e) { echo $e->getMessage(); From 258f63adf27bcf217b2a43827346d86361104b16 Mon Sep 17 00:00:00 2001 From: David Janssens Date: Fri, 4 Aug 2023 15:29:45 +0200 Subject: [PATCH 6/6] Update googleanalytics.php to version included in icms 1.4.6 --- htdocs/plugins/preloads/googleanalytics.php | 68 +++++++-------------- 1 file changed, 23 insertions(+), 45 deletions(-) diff --git a/htdocs/plugins/preloads/googleanalytics.php b/htdocs/plugins/preloads/googleanalytics.php index 08601f2994c0..ff4afab24043 100644 --- a/htdocs/plugins/preloads/googleanalytics.php +++ b/htdocs/plugins/preloads/googleanalytics.php @@ -2,63 +2,41 @@ /** * googleanalytics ga4.php * Created by david on 13/02/2022 22:41 - * + * Modified by skenow on 11 June 2023 */ /** * preload class to include the Google Analytics V4 tag in the Head of the webpage, after replacing the smarty variable with the GA4 property code * This can certainly be improved by checking whether we can, based on the preferences by our users (GDPR and cookies) */ -class IcmsPreloadGoogleanalytics extends icms_preload_Item { +class IcmsPreloadGa4 extends IcmsPreloadItem { - function eventBeforeFooter() - { - global $xoopsTpl; - global $icmsTheme; - global $icmsConfigMetaFooter; + function eventBeforeFooter() { + global $xoopsTpl; + global $icmsTheme; + global $icmsConfigMetaFooter; + global $xoTheme; + try { - try { - if ($icmsConfigMetaFooter['use_google_analytics']) - { - if(isset($icmsConfigMetaFooter['google_analytics']) && $icmsConfigMetaFooter['google_analytics'] != '') - { - if(substr( $icmsConfigMetaFooter['use_google_analytics'], 0, 2 ) === "G-") - { - $this->insertGA4Tag($icmsConfigMetaFooter['google_analytics']); - } - else - { - $this->insertUniversalAnalyticsTag($icmsConfigMetaFooter['google_analytics']); - } + if ($icmsConfigMetaFooter['use_google_analytics'] == TRUE && isset($icmsConfigMetaFooter['google_analytics']) && $icmsConfigMetaFooter['google_analytics'] != '') { + if (substr($icmsConfigMetaFooter['google_analytics'], 0, 2 ) === "G-") { + $tagvalue = $icmsConfigMetaFooter['google_analytics']; + } else { + $tagvalue = 'UA-' . $icmsConfigMetaFooter['google_analytics']; } - } - } - catch (Exception $e) { - echo $e->getMessage(); - } - } - - function insertUniversalAnalyticsTag(string $UAtag) - { - $icmsTheme->addScript('', '', '(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\', \'UA-' . $UAtag . '\', \'auto\'); - ga(\'send\', \'pageview\');', 'module', '2001'); - - } - - function insertGA4Tag(string $GA4tag) - { - $icmsTheme->addScript('https://www.googletagmanager.com/gtag/js?id=' . $GA4tag, ['async' => 'async'], '', 'module', 2000); - $icmsTheme->addScript('', '', 'window.dataLayer = window.dataLayer || []; + $xoTheme->addScript('https://www.googletagmanager.com/gtag/js?id=' . $tagvalue, ['async'=>'async'], '', 'module', 2000); + $xoTheme->addScript('', '', 'window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag(\'js\', new Date()); - gtag(\'config\', \'' . $GA4tag . '\');', 'module', '2001'); - + gtag(\'config\', \'' . $tagvalue . '\');', 'module', '2001'); + } else { + echo 'error getting icmsConfigMetaFooter'; + } + } + catch (Exception $e) { + echo $e->getMessage(); + } } }