diff --git a/Application/views/ga4/add_to_cart_listtpl.tpl b/Application/views/ga4/add_to_cart_listtpl.tpl new file mode 100644 index 0000000..0210a86 --- /dev/null +++ b/Application/views/ga4/add_to_cart_listtpl.tpl @@ -0,0 +1,57 @@ +[{$smarty.block.parent}] + +[{if $oView->getAddToBasketDecision() && $oView->d3GtmRequestedArticleLoadedByAnid() !== null}] + [{assign var="oGtmProduct" value=$oView->d3GtmRequestedArticleLoadedByAnid()}] + [{*$smarty.block.parent*}] + [{* variable $oGtmProduct is passed from parent tempalte *}] + [{assign var="d3PriceObject" value=$oGtmProduct->getPrice()}] + [{assign var="gtmCurrency" value=$oView->getActCurrency()}] + [{assign var="gtmManufacturer" value=$oGtmProduct->getManufacturer()}] + [{assign var="gtmCategory" value=$oGtmProduct->getCategory()}] + + [{block name="d3_ga4_add_to_cart_list_block"}] + [{capture name="d3_ga4_add_to_cart_listtpl"}] + [{strip}] + dataLayer.push({"event": null, "eventLabel": null, "ecommerce": null}); /* Clear the previous ecommerce object. */ + + [{*** Debug cases ***}] + [{*event.preventDefault();*}] + + let iArtQuantity = $("#amountToBasket").val(); + if(!iArtQuantity){ + iArtQuantity = 1; + } + + dataLayer.push({ + 'isAddToBasket': true, + 'event':'add_to_cart', + 'eventLabel': 'add_to_cart', + 'ecommerce': { + 'currency': "[{$currency->name}]", + 'value': iArtQuantity*[{$d3PriceObject->getPrice()}], + 'items': [ + { + 'item_id': '[{$oGtmProduct->getFieldData('oxartnum')}]', + 'item_name': '[{$oGtmProduct->getFieldData('oxtitle')}]', + 'price': [{$d3PriceObject->getPrice()}], + 'item_brand': '[{if $gtmManufacturer}][{$gtmManufacturer->oxmanufacturers__oxtitle->value}][{/if}]', + 'item_variant': '[{if $oGtmProduct->getFieldData('oxvarselect')}][{$oGtmProduct->getFieldData('oxvarselect')}][{/if}]', + [{if $gtmCategory}] + 'item_category': '[{$gtmCategory->getSplitCategoryArray(0)}]', + 'item_category_2':'[{$gtmCategory->getSplitCategoryArray(1)}]', + 'item_category_3':'[{$gtmCategory->getSplitCategoryArray(2)}]', + 'item_category_4':'[{$gtmCategory->getSplitCategoryArray(3)}]', + 'item_list_name':'[{$gtmCategory->getSplitCategoryArray()}]', + [{/if}] + 'quantity': iArtQuantity + } + ] + }[{if $oViewConf->isDebugModeOn()}], + 'debug_mode': 'true' + [{/if}] + }); + [{/strip}] + [{/capture}] + [{oxscript add=$smarty.capture.d3_ga4_add_to_cart_listtpl}] + [{/block}] +[{/if}] \ No newline at end of file diff --git a/IntelliSenseHelper.php b/IntelliSenseHelper.php index 08ac114..cdc661c 100644 --- a/IntelliSenseHelper.php +++ b/IntelliSenseHelper.php @@ -23,6 +23,18 @@ namespace D3\GoogleAnalytics4\Modules\Application\Model{ } namespace D3\GoogleAnalytics4\Modules\Application\Controller{ + + use OxidEsales\Eshop\Application\Controller\ArticleListController; + class BasketController_parent extends \OxidEsales\Eshop\Application\Controller\BasketController {} class ThankYouController_parent extends \OxidEsales\Eshop\Application\Controller\ThankYouController {} + + class ArticleListController_AddToCartHelpMethods_parent extends ArticleListController {} +} + +namespace D3\GoogleAnalytics4\Modules\Application\Component{ + + use OxidEsales\Eshop\Application\Component\BasketComponent; + + class d3GtmBasketComponentExtension_parent extends BasketComponent {} } \ No newline at end of file diff --git a/Modules/Application/Component/d3GtmBasketComponentExtension.php b/Modules/Application/Component/d3GtmBasketComponentExtension.php new file mode 100644 index 0000000..5ca7638 --- /dev/null +++ b/Modules/Application/Component/d3GtmBasketComponentExtension.php @@ -0,0 +1,17 @@ +setVariable('d3GtmAddToBasketTrigger', true); + + return $return; + } +} \ No newline at end of file diff --git a/Modules/Application/Controller/ArticleListController_AddToCartHelpMethods.php b/Modules/Application/Controller/ArticleListController_AddToCartHelpMethods.php new file mode 100644 index 0000000..775f12d --- /dev/null +++ b/Modules/Application/Controller/ArticleListController_AddToCartHelpMethods.php @@ -0,0 +1,39 @@ +getVariable('d3GtmAddToBasketTrigger'); + + Registry::getSession()->setVariable('d3GtmAddToBasketTrigger', false); + + return (bool) $decision; + } + + /** + * @return Article|null + */ + public function d3GtmRequestedArticleLoadedByAnid() + { + $sAnid = Registry::getRequest()->getRequestEscapedParameter('anid'); + + $oArticle = null; + + if ($sAnid){ + /** @var Article $oArticle */ + $oArticle = oxNew(Article::class); + $oArticle->load($sAnid); + } + + return $oArticle; + } +} \ No newline at end of file diff --git a/metadata.php b/metadata.php index 167f1b7..0072bb0 100755 --- a/metadata.php +++ b/metadata.php @@ -1,11 +1,15 @@ Basket::class, OEBasketController::class => BasketController::class, OEManufacturer::class => Manufacturer::class, - OEThankYouController::class => ThankYouController::class + OEThankYouController::class => ThankYouController::class, + OEArticleListController::class => ArticleListController_AddToCartHelpMethods::class, + OEBasketComponent::class => d3GtmBasketComponentExtension::class ], 'templates' => [], 'blocks' => [ @@ -95,6 +101,12 @@ $aModule = [ 'file' => '/Application/views/ga4/add_to_cart.tpl', 'position' => 150 ], + [ + 'template' => 'page/list/list.tpl', + 'block' => 'page_list_listbody', + 'file' => '/Application/views/ga4/add_to_cart_listtpl.tpl', + 'position' => 150 + ], // remove_from_cart [ 'template' => 'page/checkout/basket.tpl',