Files
wyczarujprezent.pl/modules/ecsgtmpro/classes/gtm/EcsGtmProDataLayer.php
2024-10-28 22:14:22 +01:00

184 lines
6.9 KiB
PHP

<?php
/**
* 2022 ECSoft
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to dev.ecsoft@gmail.com so we can send you a copy immediately.
*
* @author ECSoft <dev.ecsoft@gmail.com>
* @copyright 2022 ECSoft
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of ECSoft
*/
require_once dirname(__FILE__) . '/EcsGtmProEcommerce.php';
class EcsGtmProDataLayer
{
protected $dlManager;
public $pageCategory;
public $event;
public $eventCallback;
public $eventTimeout;
public $ecommerce;
public function __construct($dlManager)
{
$this->dlManager = $dlManager;
$currency = !empty(Context::getContext()->currency->iso_code) ? Context::getContext()->currency->iso_code : null;
$this->ecommerce = new EcsGtmProEcommerce($this, $currency);
}
public function getManager()
{
return $this->dlManager;
}
public function addItem($product, $type, $list = null)
{
if ($this->getManager()->isEE()) {
$this->addEEItem($product, $type, $list);
}
if ($this->getManager()->isGA4()) {
$this->addGA4Item($product, $list);
}
if (empty($this->event)) {
$this->event = $type;
}
}
public function addEEItem($product, $type, $list = null)
{
$dataLayerProduct = new EcsGtmProDataLayerProduct($this->getManager()->module, $product, $list);
$dataLayerProduct->removeNull();
if ($type == 'view_item') {
if (empty($this->ecommerce->detail)) {
$this->ecommerce->detail = new stdClass();
}
$this->ecommerce->detail->products = array($dataLayerProduct);
} elseif ($type == 'view_item_list' || $type == 'search_results') {
if (empty($this->ecommerce->impressions)) {
$this->ecommerce->impressions = array();
}
$this->ecommerce->impressions[] = $dataLayerProduct;
} elseif ($type == 'view_cart') {
if (empty($this->ecommerce->checkout)) {
$this->ecommerce->checkout = array();
}
$this->ecommerce->checkout[] = $dataLayerProduct;
} elseif ($type == 'begin_checkout') {
if (empty($this->ecommerce->checkout->products)) {
$this->ecommerce->checkout = new stdClass();
$this->ecommerce->checkout->products = array();
}
$this->ecommerce->checkout->products[] = $dataLayerProduct;
} elseif ($type == 'purchase') {
if (empty($this->ecommerce->purchase->products)) {
$this->ecommerce->purchase = new stdClass();
$this->ecommerce->purchase->products = array();
}
$this->ecommerce->purchase->products[] = $dataLayerProduct;
} elseif ($type == 'add_to_cart') {
if (empty($this->ecommerce->add->products)) {
$this->ecommerce->add = new stdClass();
}
$this->ecommerce->add->products = array($dataLayerProduct);
$this->event = empty($this->event) ? 'addToCart' : $this->event;
} elseif ($type == 'remove_from_cart') {
if (empty($this->ecommerce->remove->products)) {
$this->ecommerce->remove = new stdClass();
}
$this->ecommerce->remove->products = array($dataLayerProduct);
$this->event = empty($this->event) ? 'removeFromCart' : $this->event;
} elseif ($type == 'refund') {
if (empty($this->ecommerce->refund->products)) {
$this->ecommerce->refund = new stdClass();
$this->ecommerce->refund->products = array();
}
$this->ecommerce->refund->products[] = $dataLayerProduct;
}
}
public function getItems()
{
$items = array();
if ($this->getManager()->isGA4()) {
$items = $this->ecommerce->items;
} elseif ($this->getManager()->isEE()) {
if (!empty($this->ecommerce->detail->products)) {
$items = $this->ecommerce->detail->products;
} elseif (!empty($this->ecommerce->impressions)) {
$items = $this->ecommerce->impressions;
} elseif (!empty($this->ecommerce->checkout)) {
$items = $this->ecommerce->checkout;
}
}
return $items;
}
public function addGA4Item($product, $list)
{
$dataLayerProduct = new EcsGtmProDataLayerItem($this->getManager()->module, $product, $list);
$dataLayerProduct->removeNull();
if (empty($this->ecommerce->items)) {
$this->ecommerce->items = array();
}
$this->ecommerce->items[] = $dataLayerProduct;
}
public function setSubtotalsFromCart($cart)
{
if (Validate::isLoadedObject($cart)) {
$this->ecommerce->value = (float) round($cart->getOrderTotal(), _ECSGTMPRO_PRICE_DECIMAL_);
$this->ecommerce->total_tax_exc = (float) round($cart->getOrderTotal(false), _ECSGTMPRO_PRICE_DECIMAL_);
$this->ecommerce->total_products = (float) round($cart->getOrderTotal(true, Cart::ONLY_PRODUCTS), _ECSGTMPRO_PRICE_DECIMAL_);
$this->ecommerce->total_shipping = (float) round($cart->getOrderTotal(true, Cart::ONLY_SHIPPING), _ECSGTMPRO_PRICE_DECIMAL_);
$this->ecommerce->total_discounts = (float) round($cart->getOrderTotal(true, Cart::ONLY_DISCOUNTS), _ECSGTMPRO_PRICE_DECIMAL_);
}
}
public function toJson()
{
$js = '';
if (count((array)$this)) {
$eventCallback = false;
if (!empty($this->eventCallback)) {
$this->eventCallback = str_replace('"', '@@', $this->eventCallback);
$this->eventCallback = "!##".$this->eventCallback."##!";
$eventCallback = true;
if (empty($this->event)) {
$this->event = 'eventForCallback';
}
if (empty($this->eventTimeout)) {
$this->eventTimeout = 2000;
}
} else {
unset($this->eventCallback);
unset($this->eventTimeout);
}
$jsonOptions = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT;
$js = json_encode($this, $jsonOptions);
if ($eventCallback) {
$js = str_replace('##!"', '', str_replace('"!##', '', $js));
$js = str_replace('@@', '"', $js);
}
}
return $js;
}
}