8
0
GoogleAnalytics4/Modules/Core/ViewConfig.php

206 Zeilen
6.4 KiB
PHP

<?php
2023-01-27 09:07:09 +01:00
/**
2023-05-23 10:49:40 +02:00
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* https://www.d3data.de
*
2023-01-27 09:07:09 +01:00
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
2023-05-23 10:49:40 +02:00
* @author D3 Data Development - Daniel Seifert <info@shopmodule.com>
* @link https://www.oxidmodule.com
*/
namespace D3\GoogleAnalytics4\Modules\Core;
use D3\GoogleAnalytics4\Application\Model\ManagerHandler;
use D3\GoogleAnalytics4\Application\Model\ManagerTypes;
use OxidEsales\Eshop\Application\Controller\FrontendController;
use OxidEsales\Eshop\Application\Model\User;
use OxidEsales\Eshop\Core\Config;
use OxidEsales\Eshop\Core\Registry;
class ViewConfig extends ViewConfig_parent
{
// Google Tag Manager Container ID
private $sContainerId = null;
private $sCookieManagerType = null;
public function getGtmContainerId()
{
if ($this->sContainerId === null)
{
2023-09-13 15:09:27 +02:00
$this->sContainerId = Registry::getConfig()->getConfigParam('d3_gtm_sContainerID');
}
return $this->sContainerId;
}
/**
* @return void
*/
public function defineCookieManagerType() :void
{
if ($this->sCookieManagerType === null)
{
/** @var ManagerHandler $oManagerHandler */
$oManagerHandler = oxNew(ManagerHandler::class);
$this->sCookieManagerType = $oManagerHandler->getCurrManager();
}
}
/**
* @return bool
*/
public function shallUseOwnCookieManager() :bool
{
return (bool) Registry::getConfig()->getConfigParam('d3_gtm_settings_hasOwnCookieManager');
}
/**
* @return bool
*/
public function D3blShowGtmScript()
{
/** @var Config $oConfig */
$oConfig = Registry::getConfig();
// No Cookie Manager in use
if (!$this->shallUseOwnCookieManager()) {
return true;
}
$this->defineCookieManagerType();
2023-09-13 15:09:27 +02:00
$sCookieID = trim($oConfig->getConfigParam('d3_gtm_settings_controlParameter'));
// Netensio Cookie Manager
if ($this->sCookieManagerType === ManagerTypes::NET_COOKIE_MANAGER) {
$oSession = Registry::getSession();
$aCookies = $oSession->getVariable("aCookieSel");
return (is_array($aCookies) && array_key_exists($sCookieID, $aCookies) && $aCookies[$sCookieID] == "1");
}
// Aggrosoft Cookie Consent
if ($this->sCookieManagerType === ManagerTypes::AGCOOKIECOMPLIANCE) {
if (method_exists($this, "isCookieCategoryEnabled")) {
return $this->isCookieCategoryEnabled($sCookieID);
}
}
// UserCentrics or consentmanager
if (
$this->sCookieManagerType === ManagerTypes::USERCENTRICS_MODULE
or $this->sCookieManagerType === ManagerTypes::USERCENTRICS_MANUALLY
or $this->sCookieManagerType === ManagerTypes::CONSENTMANAGER
2023-06-27 09:39:43 +02:00
or $this->sCookieManagerType === ManagerTypes::COOKIEFIRST
2023-09-07 15:08:06 +02:00
or $this->sCookieManagerType === ManagerTypes::COOKIEBOT
or $this->sCookieManagerType === ManagerTypes::EXTERNAL_SERVICE
)
{
// 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() :string
{
2023-09-13 15:09:27 +02:00
$sControlParameter = trim(Registry::getConfig()->getConfigParam('d3_gtm_settings_controlParameter'));
2023-09-13 15:13:35 +02:00
if (false === $this->shallUseOwnCookieManager() or ($sControlParameter === '')){
return "";
}
if (
$this->sCookieManagerType === ManagerTypes::USERCENTRICS_MODULE
or $this->sCookieManagerType === ManagerTypes::USERCENTRICS_MANUALLY
)
{
2023-09-13 15:13:35 +02:00
return 'data-usercentrics="' . $sControlParameter . '" type="text/plain" async=""';
}
if ($this->sCookieManagerType === ManagerTypes::CONSENTMANAGER)
{
2023-09-13 15:13:35 +02:00
return 'type="text/plain" class="cmplazyload" data-cmp-vendor="'.$sControlParameter.'"';
}
2023-06-27 09:39:43 +02:00
if ($this->sCookieManagerType === ManagerTypes::COOKIEFIRST){
return 'type="text/plain" data-cookiefirst-category="' . $sControlParameter .'"';
2023-06-27 09:39:43 +02:00
}
2023-09-07 15:08:06 +02:00
if ($this->sCookieManagerType === ManagerTypes::COOKIEBOT){
return 'type="text/plain" data-cookieconsent="' . $sControlParameter .'"';
2023-09-07 15:08:06 +02:00
}
return "";
}
private $blGA4enabled = null;
public function isGA4enabled()
{
if ($this->blGA4enabled === null)
{
2023-09-13 15:09:27 +02:00
$this->sContainerId = Registry::getConfig()->getConfigParam('d3_gtm_blEnableGA4');
}
return $this->blGA4enabled;
}
public function getGtmDataLayer()
{
if (!$this->getGtmContainerId()) return "[]";
$oConfig = Registry::getConfig();
$oView = $oConfig->getTopActiveView();
/** @var User|false|null $oUser */
$oUser = $oConfig->getUser();
$cl = $this->getTopActiveClassName();
$aPageTypes = [
"content" => "cms",
"details" => "product",
"alist" => "listing",
"search" => "listing",
"basket" => "checkout",
"user" => "checkout",
"payment" => "checkout",
"order" => "checkout",
"thankyou" => "checkout",
"start" => "start",
];
$dataLayer = [
'page' => [
'type' => $aPageTypes[$cl] ?? "unknown",
'title' => $oView->getTitle(),
'cl' => $cl,
],
'userid' => $oUser instanceof User ? $oUser->getId() : false,
'sessionid' => session_id(),
//'httpref' => $_SERVER["HTTP_REFERER"] ?? "unknown"
];
return json_encode([$dataLayer], JSON_PRETTY_PRINT);
2023-01-27 09:07:09 +01:00
}
2023-08-17 08:50:41 +02:00
public function isDebugModeOn() :bool
{
return Registry::getConfig()->getConfigParam('d3_gtm_blEnableDebug');
}
public function isPromotionList($listId)
{
$oConfig = Registry::getConfig();
$aPromotionListIds = $oConfig->getConfigParam("") ?? ['bargainItems', 'newItems', 'topBox', 'alsoBought', 'accessories', 'cross'];
}
2018-07-04 15:47:37 +02:00
}