diff --git a/Application/Tracking/Modifiers/EntityModifierByCurrentAction.php b/Application/Tracking/Modifiers/EntityModifierByCurrentAction.php index 1ffd75b..93f0c74 100644 --- a/Application/Tracking/Modifiers/EntityModifierByCurrentAction.php +++ b/Application/Tracking/Modifiers/EntityModifierByCurrentAction.php @@ -172,6 +172,15 @@ protected function modifyByThankYouController($order, $user, $basket) } $this->activePageEntity->setBoughtProducts($basketProducts); + + // Promotion - Benutzerdefinierte dimensionen + foreach ($basket->getVouchers() as $voucherId => $voucher) { + $this->activePageEntity->addPromotion( + $voucherId, + $voucher->sVoucherNr, + $voucher->dVoucherdiscount + ); + } } /** diff --git a/Component/Tracking/ActivePageEntity.php b/Component/Tracking/ActivePageEntity.php index 31c8e7f..a89f132 100644 --- a/Component/Tracking/ActivePageEntity.php +++ b/Component/Tracking/ActivePageEntity.php @@ -134,6 +134,11 @@ class ActivePageEntity implements ActivePageEntityInterface */ private $email = ''; + /** + * @var array + */ + private $promotions = null; + /** * @return string */ @@ -469,4 +474,32 @@ public function getEmail() { return $this->email; } + + /** + * @return array + */ + public function getPromotions() + { + return $this->promotions; + } + + /** + * @param string $voucherId oxvoucher.oxid + * @param string $voucherCode Eingelöster Gutscheincode + * @param float $discount Eingelöster Gutscheinwert + * + * @see https://docs.econda.de/de/MONDE/data-services/data-model-management/promotions+und+gutscheine.html + */ + public function addPromotion($voucherId, $voucherCode, $discount) + { + if ($this->promotions === null) { + $this->promotions = []; + } + + $this->promotions[] = [ + 'voucherId' => $voucherId, + 'voucherCode' => $voucherCode, + 'discount' => $discount + ]; + } } diff --git a/Component/Tracking/ActivePageEntityInterface.php b/Component/Tracking/ActivePageEntityInterface.php index 3a993b6..bd1ccb5 100644 --- a/Component/Tracking/ActivePageEntityInterface.php +++ b/Component/Tracking/ActivePageEntityInterface.php @@ -205,4 +205,18 @@ public function setEmail($email); * @return string */ public function getEmail(); + + /** + * @return array + */ + public function getPromotions(); + + /** + * @param string $voucherId oxvoucher.oxid + * @param string $voucherCode Eingelöster Gutscheincode + * @param float $discount Eingelöster Gutscheinwert + * + * @see https://docs.econda.de/de/MONDE/data-services/data-model-management/promotions+und+gutscheine.html + */ + public function addPromotion($voucherId, $voucherCode, $discount); } diff --git a/Component/Tracking/SdkExtension/Promotions.php b/Component/Tracking/SdkExtension/Promotions.php new file mode 100644 index 0000000..de74765 --- /dev/null +++ b/Component/Tracking/SdkExtension/Promotions.php @@ -0,0 +1,105 @@ + + * Date: 10.12.19 + * Time: 17:09 + */ + +namespace OxidEsales\PersonalizationModule\Component\Tracking\SdkExtension; + +use Econda\Tracking\TrackingItemInterface; +use Econda\Util\BaseObject; +use OxidEsales\EshopCommunity\Internal\Application\ContainerFactory; +use OxidEsales\EshopCommunity\Internal\Common\Database\QueryBuilderFactoryInterface; + +/** + * Class Promotions + * @package OxidEsales\PersonalizationModule\Component\Tracking\SdkExtension + * @see https://docs.econda.de/de/MONDE/data-services/data-model-management/promotions+und+gutscheine.html + */ +class Promotions extends BaseObject implements TrackingItemInterface +{ + /** + * @var array + */ + protected $data = []; + + /** + * @inheritDoc + * @return array + */ + public function getTrackingData() + { + $data = array_map([$this, 'convert'], $this->data); + + return [ + 'promotions' => $data + ]; + } + + /** + * @param array $promotion + */ + protected function convert($promotion) + { + $title = $this->getVoucherserieTitle($promotion['voucherId']); + + return $this->econdaFormat( + $title, + $promotion['voucherCode'], + $promotion['discount'] + ); + } + + /** + * @param string(64) $action Gutschein- oder Marketingaktion + * @param string(64) $code Eingelöster Gutscheincode + * @param float $wert Eingelöster Gutscheinwert + */ + protected function econdaFormat($action, $code, $wert) + { + $action = $this->truncate($action, 64); + $code = $this->truncate($code, 64); + + return [$action, $code, $wert]; + } + + /** + * @param string $string + * @param integer $length + * @return string + */ + protected function truncate($string, $length) + { + if (strlen($string) > $length) { + $string = substr($string, 0, $length - 3) . '...'; + } + + return $string; + } + + /** + * @param string $voucherId OXID of Voucher + * @return string + */ + protected function getVoucherserieTitle($voucherId) + { + $container = ContainerFactory::getInstance()->getContainer(); + + $qb = $container->get(QueryBuilderFactoryInterface::class)->create() + ->select('oxserienr') + ->from(getViewName('oxvoucherseries'), 'vs') + ->join('vs', 'oxvouchers', 'vo', 'vo.OXVOUCHERSERIEID = vs.OXID') + ->where('vo.OXID = :oxid') + ->setParameter('oxid', $voucherId); + + $name = $qb->execute()->fetchColumn(0); + + if (empty($name)) { + return 'Voucher name no longer exists in the database'; + } + + return $name; + } +} diff --git a/Component/Tracking/TrackingCodeGenerator.php b/Component/Tracking/TrackingCodeGenerator.php index d8f93d1..d3c421d 100644 --- a/Component/Tracking/TrackingCodeGenerator.php +++ b/Component/Tracking/TrackingCodeGenerator.php @@ -15,6 +15,7 @@ use Econda\Tracking\TransactionProduct; use OxidEsales\PersonalizationModule\Component\Tracking\SdkExtension\Email; use OxidEsales\PersonalizationModule\Component\Tracking\SdkExtension\PageView; +use OxidEsales\PersonalizationModule\Component\Tracking\SdkExtension\Promotions; /** * Class responsible for generating Econda tracking script. @@ -51,6 +52,7 @@ public function generateCode() $this->addCurrentOrderProcessToPageView(); $this->addUserRegistrationDataToPageView(); $this->addTransactionDataToPageView(); + $this->addPromotionsDataToPageView(); $this->addSearchQueryDataToPageView(); $this->addLoginUserDataToPageView(); $this->addCartDataToPageView(); @@ -101,6 +103,16 @@ private function addTransactionDataToPageView() } } + /** + * @see https://docs.econda.de/de/MONDE/data-services/data-model-management/promotions+und+gutscheine.html + */ + private function addPromotionsDataToPageView() + { + if ($this->activePageEntity->getPromotions()) { + $this->pageView->add(new Promotions(['data' => $this->activePageEntity->getPromotions()])); + } + } + private function addCurrentProductDataToPageView() { if (!empty($this->activePageEntity->getProductData() && empty($this->activePageEntity->getProductToBasket()))) {