From 8eb734be8a38e41cfa1fea9a06392c948fd9c6c1 Mon Sep 17 00:00:00 2001 From: Christoph Staeblein Date: Tue, 2 May 2023 14:28:47 +0200 Subject: [PATCH] OXID UserCentrics support, general overhaul for cookie managers --- Application/views/blocks/_gtm_js.tpl | 46 +++------------- Application/views/blocks/_gtm_nojs.tpl | 8 ++- Docs/README.md | 9 ++- Modules/Core/ViewConfig.php | 76 +++++++++++++++++++++++--- 4 files changed, 89 insertions(+), 50 deletions(-) diff --git a/Application/views/blocks/_gtm_js.tpl b/Application/views/blocks/_gtm_js.tpl index e070806..d3d1075 100755 --- a/Application/views/blocks/_gtm_js.tpl +++ b/Application/views/blocks/_gtm_js.tpl @@ -1,40 +1,12 @@ -[{assign var="d3VtConfigObject" value=$oViewConf->getConfig()}] -[{if $d3VtConfigObject->getConfigParam('d3_gtm_settings_hasOwnCookieManager')}] - [{if $oViewConf->D3blAcceptedCookie($d3VtConfigObject->getConfigParam('d3_gtm_settings_cookieName'))}] +[{* Always prepare the data layer to avoid errors *}] + - [{if $oViewConf->getGtmContainerId()}][{strip}] - - - - [{if $oViewConf->getTopActionClassName() === "alist" }] - [{* include file="ga4_view_item_list.tpl" gtmCategory=$oView->getActiveCategory() gtmProducts=$oView->getArticleList() listtype=$oView->getListType() *}] - [{elseif $oViewConf->getTopActionClassName() === "details" }] - [{* include file="ga4_view_item.tpl" gtmProduct=$oView->getProduct() *}] - [{elseif $oViewConf->getTopActionClassName() === "search" }] - [{elseif $oViewConf->getTopActionClassName() === "basket" }] - - [{/if}] - [{/strip}][{/if}] - [{else}] - - [{/if}] -[{else}] +[{if $oViewConf->D3blShowGtmScript()}] [{if $oViewConf->getGtmContainerId()}][{strip}] - + [{if $oViewConf->getTopActionClassName() === "alist" }] [{* include file="ga4_view_item_list.tpl" gtmCategory=$oView->getActiveCategory() gtmProducts=$oView->getArticleList() listtype=$oView->getListType() *}] [{elseif $oViewConf->getTopActionClassName() === "details" }] [{* include file="ga4_view_item.tpl" gtmProduct=$oView->getProduct() *}] [{elseif $oViewConf->getTopActionClassName() === "search" }] [{elseif $oViewConf->getTopActionClassName() === "basket" }] - [{/if}] [{/strip}][{/if}] -[{/if}] + [{/if}] -[{$smarty.block.parent}] +[{$smarty.block.parent}] \ No newline at end of file diff --git a/Application/views/blocks/_gtm_nojs.tpl b/Application/views/blocks/_gtm_nojs.tpl index fa191b0..05c78fb 100644 --- a/Application/views/blocks/_gtm_nojs.tpl +++ b/Application/views/blocks/_gtm_nojs.tpl @@ -1,8 +1,12 @@ -[{if $oViewConf->getGtmContainerId()}][{strip}] +[{if $oViewConf->D3blShowGtmScript() && !$oViewConf->getCookieManagerType()}] + [{if $oViewConf->getGtmContainerId()}][{strip}] - [{/strip}][{/if}] + + [{/strip}][{/if}] + [{/if}] + [{$smarty.block.parent}] \ No newline at end of file diff --git a/Docs/README.md b/Docs/README.md index de361e6..bdf915d 100644 --- a/Docs/README.md +++ b/Docs/README.md @@ -54,12 +54,15 @@ Aktivieren Sie anschließend diese Weiche. Setzen Sie den Haken bei "Eigenen Coo - [aggrosoft - oxid-cookie-compliance](https://github.com/aggrosoft/oxid-cookie-compliance) - https://github.com/aggrosoft/oxid-cookie-compliance - die entsprechend gewählte Kategorie in den Moduleinstellungen des 'Google Analytics 4' unter - ```Einstell. > Cookie Manager Einstellungen > Cookie-ID``` eintragen + ```Einstell. > Cookie Manager Einstellungen > Cookie-ID``` eintragen + - Default-Werte sind entweder ```ANALYTICS``` oder ```MARKETING```. Bitte auf die Großschreibung achten. - [Netensio - Cookie Consent Manager](https://www.netensio.de/oxid-eshop-module/cookie-consent-manager-fuer-oxid-eshop.html) - Modul entsprechend konfigurieren - CookieID des angelegten Cookies in den Moduleinstellungen des 'Google Analytics 4' unter ```Einstell. > Cookie Manager Einstellungen > Cookie-ID``` eintragen - + - [OXID Cookie Management powered by usercentrics](https://docs.oxid-esales.com/modules/usercentrics/de/latest/einfuehrung.html) - - soweit ich weiß nichts weiter? + - In der Usercentrics-Verwaltung einen Service für Google Analytics anlegen + - Den Service-Namen in den Moduleinstellungen des 'Google Analytics 4' unter + ```Einstell. > Cookie Manager Einstellungen > Cookie-ID``` eintragen \ No newline at end of file diff --git a/Modules/Core/ViewConfig.php b/Modules/Core/ViewConfig.php index 52a5905..31fbe4f 100644 --- a/Modules/Core/ViewConfig.php +++ b/Modules/Core/ViewConfig.php @@ -22,6 +22,7 @@ class ViewConfig extends ViewConfig_parent // Google Tag Manager Container ID private $sContainerId = null; + private $sCookieManagerType = null; public function getGtmContainerId() { @@ -35,27 +36,86 @@ class ViewConfig extends ViewConfig_parent return $this->sContainerId; } + public function getCookieManagerType() + { + if ($this->sCookieManagerType === null) + { + $this->sCookieManagerType = false; + + $allowedManagerTypes = [ + 'net_cookie_manager', + 'agcookiecompliance', + 'oxps_usercentrics' + ]; + + foreach ($allowedManagerTypes as $type) { + if ($this->isModuleActive($type)) { + $this->sCookieManagerType = $type; + break; + } + } + } + return $this->sCookieManagerType; + } + /** - * @param $sCookieID * @return bool */ - public function D3blAcceptedCookie($sCookieID) + public function D3blShowGtmScript() { - $oSession = Registry::getSession(); - $aCookies = $oSession->getVariable("aCookieSel"); + $oConfig = $this->getConfig(); - if (!is_null($aCookies) && is_array($aCookies) && array_key_exists($sCookieID, $aCookies) && $aCookies[$sCookieID] == "1") { + // No Cookie Manager in use + if (!$oConfig->getConfigParam('d3_gtm_settings_hasOwnCookieManager')) { return true; } - // Aggrosoft Cookie Consent - if (method_exists($this, "isCookieCategoryEnabled")) { - return $this->isCookieCategoryEnabled($sCookieID); + $sCookieID = $oConfig->getConfigParam('d3_gtm_settings_cookieName'); + + // Netensio Cookie Manager + if ($this->getCookieManagerType() == "net_cookie_manager") { + $oSession = Registry::getSession(); + $aCookies = $oSession->getVariable("aCookieSel"); + + return (!is_null($aCookies) && is_array($aCookies) && array_key_exists($sCookieID, $aCookies) && $aCookies[$sCookieID] == "1"); } + // Aggrosoft Cookie Consent + if ($this->getCookieManagerType() == "agcookiecompliance") { + if (method_exists($this, "isCookieCategoryEnabled")) { + return $this->isCookieCategoryEnabled($sCookieID); + } + } + + // UserCentrics + if ($this->getCookieManagerType() == "oxps_usercentrics") { + // Always needs the script-tags delivered to the DOM. + return true; + } + + // Cookie Manager not (yet) supported return false; } + /** + * Get additional attributes for script tags. + * This is especially important for UserCentrics. + * @return string + */ + public function getGtmScriptAttributes() + { + if ($this->getCookieManagerType() == "oxps_usercentrics") { + $oConfig = $this->getConfig(); + $sCookieId = $oConfig->getConfigParam('d3_gtm_settings_cookieName'); + + if ($sCookieId) { + return 'type="text/plain" data-usercentrics="' . $sCookieId . '"'; + } + } + + return ""; + } + private $blGA4enabled = null; public function isGA4enabled()