8
0
GoogleAnalytics4/Application/Extend/ViewConfig.php

129 Zeilen
5.4 KiB
PHP

2018-07-04 15:47:37 +02:00
<?php
2021-07-17 23:22:23 +02:00
/*
* vanilla-thunder/oxid-module-gtm
* Google Tag Manager Integration for OXID eShop v6.2+
2018-07-04 15:47:37 +02:00
*
* This program is free software;
* you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation;
* either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
2021-07-17 23:22:23 +02:00
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
2018-07-04 15:47:37 +02:00
* You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>
2021-07-17 23:22:23 +02:00
*/
2018-07-04 15:47:37 +02:00
2021-07-17 23:22:23 +02:00
namespace VanillaThunder\GoogleTagManager\Application\Extend;
use OxidEsales\Eshop\Application\Controller\FrontendController;
use OxidEsales\Eshop\Core\Registry;
use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory;
use OxidEsales\EshopCommunity\Internal\Framework\Module\Configuration\Bridge\ModuleSettingBridgeInterface;
class ViewConfig extends ViewConfig_parent
2018-07-04 15:47:37 +02:00
{
2021-07-17 23:22:23 +02:00
// Google Tag Manager Container ID
private $sContainerId = null;
2022-06-09 20:41:59 +02:00
2021-07-17 23:22:23 +02:00
public function getGtmContainerId()
2018-07-04 15:47:37 +02:00
{
2022-06-09 20:41:59 +02:00
if ($this->sContainerId === null)
{
2021-07-17 23:22:23 +02:00
$this->sContainerId = ContainerFactory::getInstance()
2022-06-09 20:41:59 +02:00
->getContainer()
->get(ModuleSettingBridgeInterface::class)
->get('vt_gtm_sContainerID', 'vt-gtm');
2021-07-17 23:22:23 +02:00
}
return $this->sContainerId;
2018-07-04 15:47:37 +02:00
}
2022-06-09 20:41:59 +02:00
private $blGA4enabled = null;
2018-07-04 15:47:37 +02:00
2022-06-09 20:41:59 +02:00
public function isGA4enabled()
{
if ($this->blGA4enabled === null)
{
$this->sContainerId = ContainerFactory::getInstance()
->getContainer()
->get(ModuleSettingBridgeInterface::class)
->get('vt_gtm_blEnableGA4', 'vt-gtm');
}
return $this->blGA4enabled;
}
2021-07-17 23:22:23 +02:00
public function getGtmDataLayer()
2018-07-04 15:47:37 +02:00
{
2022-06-09 20:41:59 +02:00
if (!$this->getGtmContainerId()) return "[]";
2018-07-04 15:47:37 +02:00
2021-07-17 23:22:23 +02:00
$oConfig = Registry::getConfig();
2022-06-09 20:41:59 +02:00
$oView = $oConfig->getTopActiveView();
/** @var FrontendController $oShop */
2018-07-04 15:47:37 +02:00
//$oShop = oxRegistry::getConfig()->getActiveShop(); /** @var oxShop $oShop */
$oUser = $oConfig->getUser();
2022-06-09 20:41:59 +02:00
$cl = $this->getTopActionClassName();
$aPageTypes = [
"content" => "cms",
"details" => "product",
"alist" => "listing",
"search" => "listing",
"basket" => "checkout",
"user" => "checkout",
"payment" => "checkout",
"order" => "checkout",
"thankyou" => "checkout",
];
2018-07-04 15:47:37 +02:00
$dataLayer = [
2022-06-09 20:41:59 +02:00
'page' => [
'type' => $aPageTypes[$cl] ?? "unknown",
'title' => $oView->getTitle(),
'cl' => $cl,
],
'userid' => ($oUser ? $oUser->getId() : false),
'sessionid' => session_id() ?? false,
//'httpref' => $_SERVER["HTTP_REFERER"] ?? "unknown"
2018-07-04 15:47:37 +02:00
];
2022-06-09 20:41:59 +02:00
return json_encode([$dataLayer], JSON_PRETTY_PRINT);
2021-07-17 23:22:23 +02:00
2018-07-04 15:47:37 +02:00
unset($dataLayer["user"]["http"]); // das brauchen wir hier nicht
2022-06-09 20:41:59 +02:00
return json_encode([$dataLayer], JSON_PRETTY_PRINT);
2018-07-04 15:47:37 +02:00
/*
// --- Produktdaten ---
$transactionProducts = [];
foreach($oOrder->getOrderArticles() as $_prod ) $transactionProducts[] = [
'name' => '', // (erforderlich) Produktname String
'sku' => '', // (erforderlich) Produkt-SKU String
'category' => '', // (optional) Produktkategorie String
'price' => '', // (erforderlich) Preis pro Einheit Numerischer Wert
'quantity' => '' // (erforderlich) Anzahl der Artikel Numerischer Wert
];
// --- Transaktionsdaten ---
$dataLayer['transactionId'] = $oOrder->oxorder__oxordernr->value; // (erforderlich) Eindeutige Transaktionskennung String
$dataLayer['transactionAffiliation'] = $oShop->oxshops__oxname->value; // (optional) Partner oder Geschäft String
$dataLayer['transactionTotal'] = $oOrder->oxorder__oxtotalordersum->value; // (erforderlich) Gesamtwert der Transaktion Numerischer Wert
$dataLayer['transactionShipping'] = $oOrder->oxorder__oxdelcost->value; // (optional) Versandkosten für die Transaktion Numerischer Wert
$dataLayer['transactionTax'] = ''; // (optional) Steuerbetrag für die Transaktion Numerischer Wert
$dataLayer['transactionProducts'] = $transactionProducts; // (optional) Liste der bei der Transaktion erworbenen Artikel Array von Produktobjekten
*/
}
2022-06-09 20:41:59 +02:00
public function triggerGA4events()
2021-07-17 23:22:23 +02:00
{
2022-06-09 20:41:59 +02:00
// general events
2021-07-17 23:22:23 +02:00
2022-06-09 20:41:59 +02:00
}
2021-07-17 23:22:23 +02:00
2022-06-09 20:41:59 +02:00
public function isPromotionList($listId)
{
$oConfig = Registry::getConfig();
$aPromotionListIds = $oConfig->getConfigParam("") ?? ['bargainItems', 'newItems', 'topBox', 'alsoBought', 'accessories', 'cross'];
2021-07-17 23:22:23 +02:00
}
2018-07-04 15:47:37 +02:00
}