From 289bfd23e1169a24c2d1ea47164153bda40d5c0d Mon Sep 17 00:00:00 2001 From: Renon Stewart Date: Mon, 1 Mar 2021 22:12:46 -0500 Subject: [PATCH] Update price logic & GA4 --- Block/Data/Product.php | 25 +----- Block/DataLayerAbstract.php | 19 ++++- .../ProductData/ProductImpressionAbstract.php | 23 +++++ Helper/Data.php | 83 +++++++++++++++++++ Helper/DataLayerItem.php | 13 --- Helper/Product.php | 5 +- Model/DataLayerEvent.php | 13 ++- README.md | 65 +++++++++------ composer.json | 4 +- etc/adminhtml/system.xml | 54 +++--------- 10 files changed, 189 insertions(+), 115 deletions(-) diff --git a/Block/Data/Product.php b/Block/Data/Product.php index 3da47a5..3546057 100644 --- a/Block/Data/Product.php +++ b/Block/Data/Product.php @@ -8,11 +8,9 @@ namespace MagePal\GoogleTagManager\Block\Data; use Exception; -use Magento\Catalog\Api\Data\ProductInterface; use Magento\Catalog\Block\Product\AbstractProduct; use Magento\Catalog\Block\Product\Context; use Magento\Catalog\Helper\Data; -use Magento\Catalog\Pricing\Price\FinalPrice; use MagePal\GoogleTagManager\Block\DataLayer; use MagePal\GoogleTagManager\DataLayer\ProductData\ProductProvider; use MagePal\GoogleTagManager\Helper\Product as ProductHelper; @@ -70,7 +68,7 @@ protected function _prepareLayout() 'parent_sku' => $product->getData('sku'), 'product_type' => $product->getTypeId(), 'name' => $product->getName(), - 'price' => $this->getPrice(), + 'price' => $this->productHelper->getProductPrice($product), 'attribute_set_id' => $product->getAttributeSetId(), 'path' => implode(" > ", $this->getBreadCrumbPath()), 'category' => $this->getProductCategoryName(), @@ -91,27 +89,6 @@ protected function _prepareLayout() return $this; } - /** - * @return mixed - */ - public function getPrice() - { - /** @var $tm DataLayer */ - $tm = $this->getParentBlock(); - $price = 0; - - /** @var $product ProductInterface */ - if ($this->getProduct()) { - $price = $this->getProduct() - ->getPriceInfo() - ->getPrice(FinalPrice::PRICE_CODE) - ->getAmount() - ->getBaseAmount() ?: 0; - } - - return $tm->formatPrice($price); - } - /** * Get category name from breadcrumb * diff --git a/Block/DataLayerAbstract.php b/Block/DataLayerAbstract.php index 97f395a..2f6e806 100755 --- a/Block/DataLayerAbstract.php +++ b/Block/DataLayerAbstract.php @@ -14,9 +14,18 @@ use MagePal\GoogleTagManager\Helper\Data as GtmHelper; /** + * @method setBlockName($name) + * @method getBlockName() + * @method setList($name) * @method getList() - * @method setListType() + * @method setListType($type) * @method getListType() + * @method setItemListName($name) + * @method getItemListName() + * @method setUseWidgetTitle($title) + * @method getUseWidgetTitle() + * @method getShowCategory() + * @method getCatalogWidgetBlockList() */ class DataLayerAbstract extends Template { @@ -88,6 +97,14 @@ public function getDataLayer() ['dataLayer' => $this] ); + return $this->processDataLayer(); + } + + /** + * @return array + */ + public function processDataLayer() + { $result = []; if (!empty($this->getVariables())) { diff --git a/DataLayer/ProductData/ProductImpressionAbstract.php b/DataLayer/ProductData/ProductImpressionAbstract.php index f5f7353..f03ddd5 100644 --- a/DataLayer/ProductData/ProductImpressionAbstract.php +++ b/DataLayer/ProductData/ProductImpressionAbstract.php @@ -28,6 +28,11 @@ abstract class ProductImpressionAbstract /** @var string */ private $listType = ''; + /** + * @var string + */ + private $itemListName = ''; + /** * @return array */ @@ -94,4 +99,22 @@ public function setListType(string $listType) $this->listType = $listType; return $this; } + + /** + * @return string + */ + public function getItemListName() + { + return $this->itemListName; + } + + /** + * @param string $itemListName + * @return ProductImpressionAbstract + */ + public function setItemListName(string $itemListName) + { + $this->itemListName = $itemListName; + return $this; + } } diff --git a/Helper/Data.php b/Helper/Data.php index f696ff9..2f52d12 100755 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -7,6 +7,9 @@ namespace MagePal\GoogleTagManager\Helper; +use Magento\Catalog\Api\Data\ProductInterface; +use Magento\Catalog\Pricing\Price\FinalPrice; +use Magento\Catalog\Model\Product\Type; use Magento\Framework\App\Helper\AbstractHelper; use Magento\Store\Model\ScopeInterface; @@ -202,4 +205,84 @@ public function setDataLayerName($name) $this->_dataLayerName = $name; return $this; } + + /** + * @param null $store_id + * @return bool + */ + public function isCategoryLayerEnabled($store_id = null) + { + return $this->scopeConfig->isSetFlag( + 'googletagmanager/general/category_layer', + ScopeInterface::SCOPE_STORE, + $store_id + ); + } + + /** + * @param ProductInterface $product + * @param array $viewItem + */ + public function addCategoryElements($product, &$viewItem) + { + if (!$this->isCategoryLayerEnabled() || !$product) { + return; + } + + $categoryList = []; + $index = 1; + $categoryCollection = $product->getCategoryCollection(); + $categories = $categoryCollection->addAttributeToSelect('name'); + + if (array_key_exists('item_category', $viewItem)) { + $index = 2; + $categoryList[] = $viewItem['item_category']; + } + + foreach ($categories as $category) { + if (!in_array($category->getName(), $categoryList)) { + $categoryList[] = $category->getName(); + + if ($index == 1) { + $viewItem['item_category'] = $category->getName(); + $index++; + } else { + $viewItem['item_category_' . $index] = $category->getName(); + $index++; + } + + if ($index >= 5) { + break; + } + } + } + } + + /** + * @param $product + * @return float + */ + public function getProductPrice($product) + { + $price = 0; + + /** @var $product ProductInterface */ + if ($product) { + $price = $product + ->getPriceInfo() + ->getPrice(FinalPrice::PRICE_CODE) + ->getAmount() + ->getBaseAmount() ?: 0; + } + + if (!$price) { + if ($product->getTypeId() == Type::TYPE_SIMPLE) { + $price = $product->getPrice(); + } else { + $price = $product->getFinalPrice(); + } + } + + return $this->formatPrice($price); + } } diff --git a/Helper/DataLayerItem.php b/Helper/DataLayerItem.php index d3e9c18..c51477a 100755 --- a/Helper/DataLayerItem.php +++ b/Helper/DataLayerItem.php @@ -55,19 +55,6 @@ public function getItemVariantFormat($store_id = null) } } - /** - * @param null $store_id - * @return bool - */ - public function isCategoryLayerEnabled($store_id = null) - { - return $this->scopeConfig->isSetFlag( - 'googletagmanager/general/category_layer', - ScopeInterface::SCOPE_STORE, - $store_id - ); - } - /** * @param OrderItem $item | QuoteItem $item * @return array diff --git a/Helper/Product.php b/Helper/Product.php index 679d702..9fc68a8 100755 --- a/Helper/Product.php +++ b/Helper/Product.php @@ -7,12 +7,11 @@ namespace MagePal\GoogleTagManager\Helper; -use Magento\Framework\App\Helper\AbstractHelper; +use Magento\Catalog\Api\Data\ProductInterface; use Magento\Framework\UrlInterface; -class Product extends AbstractHelper +class Product extends Data { - public function getImageUrl($product) { /** @var $product ProductInterface */ diff --git a/Model/DataLayerEvent.php b/Model/DataLayerEvent.php index d5cc1c2..d7f119b 100644 --- a/Model/DataLayerEvent.php +++ b/Model/DataLayerEvent.php @@ -20,9 +20,18 @@ class DataLayerEvent const CART_PAGE_EVENT = 'cartPage'; const PURCHASE_EVENT = 'purchase'; const ORDER_SUCCESS_PAGE_EVENT = 'orderSuccessPage'; - /** @deprecated - GTM_ORDER_COMPLETE_EVENT replace with PURCHASE_EVENT */ - const GTM_ORDER_COMPLETE_EVENT = 'gtm.orderComplete'; const ALL_PAGE_EVENT = 'allPage'; const SEARCH_PAGE_EVENT = 'searchPage'; const HOME_PAGE_EVENT = 'homePage'; + + /** @deprecated - GTM_ORDER_COMPLETE_EVENT replace with PURCHASE_EVENT */ + const GTM_ORDER_COMPLETE_EVENT = 'gtm.orderComplete'; + + /** Google Analytics 4 */ + const GA4_REFUND_EVENT = 'refund'; + const GA4_ADD_TO_CART_EVENT = 'add_to_cart'; + const GA4_REMOVE_FROM_CART_EVENT = 'remove_from_cart'; + const GA4_VIEW_ITEM_LIST = 'view_item_list'; + const GA4_VIEW_ITEM = 'view_item'; + const GA4_VIEW_CART = 'view_cart'; } diff --git a/README.md b/README.md index 23466d8..9df4ecb 100755 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ [![Total Downloads](https://poser.okvpn.org/magepal/magento2-googletagmanager/downloads)](https://packagist.org/packages/magepal/magento2-googletagmanager) [![Latest Stable Version](https://poser.okvpn.org/magepal/magento2-googletagmanager/v/stable)](https://packagist.org/packages/magepal/magento2-googletagmanager) -#### Whether you are a small Magento retailer or an Enterprise customer, our suite of Google Tag Manager extensions will help you integrate the most challenging GTM projects within days, instead of spending weeks or months creating custom solutions. +#### Whether you are a small Magento retailer or an Enterprise customer, our suite of Google Tag Manager extensions will help you integrate the most challenging GTM projects within days, instead of spending weeks or months creating custom solutions. For Magento 2.0.x, 2.1.x, 2.2.x, 2.3.x and 2.4.x Magento Enhanced Ecommerce for Google Tag Manager @@ -19,12 +19,12 @@ Magento code providing the data is available to Google Tag Manager. Google Tag Manager makes running your digital marketing campaigns much easier when calibrating with multiple department and Ad agencies by making available the right set of tools so that everyone can get their job done quickly without relying on developers. -Without having the all data you need at your finger tips your integration will become a difficult, time-consuming and messy since each developer will only focus on the current task at hand instead of focusing on writing reusable components for future integration. +Without having the all data you need at your fingertips your integration will become a difficult, time-consuming and messy since each developer will only focus on the current task at hand instead of focusing on writing reusable components for future integration. Our extension provides a vast array of over 60 preconfigure data layer elements to make integrating your Magento store with any other third-party service a breeze using Google Tag Manager. Extracting, customizing and adding your own custom data from your Magento store to Google Tag Manager is as easy as 10 lines of code using our easy to customize APIs. ->:warning: Google Tag Manager 2.5.0 has some breaking changes to Enhanced Ecommerce. Please download the latest version of Enhanced Ecommerce 1.5.0 or greater from www.magepal.com account. +>:warning: Google Tag Manager 2.6.0 has some breaking changes to Enhanced Ecommerce. Please download the latest version of Enhanced Ecommerce 1.6.0 or greater from www.magepal.com account. ### Why use our Google Tag Manager extension? @@ -34,9 +34,13 @@ full advantage of GTM when integrating third-parties tracking codes that require such as product name, price, items added to cart, order items, total, shipping amount or any other data. Our extension provides hundreds of data elements and events to accomplish any integration and provides the building block to make your next integration a success. With a few lines of code, you can quickly extend our extension to accomplish your -most challenging integration. Google Tag Manager is only as powerful as the data layer powering it.  +most challenging integration. Google Tag Manager is only as powerful as the data layer powering it. Learn more about [customizing Google Tag Manger](https://www.magepal.com/help/docs/google-tag-manager-for-magento/#api). +### Google Analytics 4 +Upgrade to the next generation of tracking from Google. [Google Analytics 4](https://www.magepal.com/google-analytics-4-for-google-tag-manager.html?utm_source=ga4%20for%20Google%20Tag%20Manager&utm_medium=github) comes with a bunch of key features that make it very different and more powerful than Enhanced Ecommerce. +Gain access to GA4 new approach to privacy-first tracking, channel measurement, and AI based predictive data with MagePal Google Analytics 4 Extension. + ### Google Analytics Enhanced E-commerce Want to track more? Upgrade to our new [Enhanced E-commerce for Google Tag Manager](https://www.magepal.com/enhanced-ecommerce-for-google-tag-manager.html?utm_source=Enhanced%20Ecommerce%20for%20Google%20Tag%20Manager&utm_medium=github) to take full advantage of Google Analytics most powerful e-commerce features. Gain valuable insight and increase your conversion rate by leveraging Google Enhanced Ecommerce to better understand your user actions and behaviors. @@ -58,28 +62,30 @@ Please Note: Merchants should consult with their own legal counsel to ensure tha ### Get more from Google Tag Manager with our add-on Extensions -| Features | GTM | EE | DL | -|-----------------------------|:---:|:--:|:--:| -| Global Page Tracking | X | X | | -| Order Conversion Tracking | X | X | | -| Page Type Event | | X | | -| Product Clicks | | X | | -| Product Detail Impressions | | X | | -| Add to Cart | | X | | -| Remove from Cart | | X | | -| Checkout Steps | | X | | -| Order Refunds | | X | | -| Admin Order Tracking | | X | | -| Access DataLayer using JS | | | X | -| Bing UET Tracking | | | X | -| Full Facebook Tracking | | | X | -| Custom Image Pixel Tracking | | | X | -| Custom iFrame Tracking | | | X | -| Third-Party Integration | | | X | -| Extend individual page type | | | X | +| Features | GTM | EE | GA4 | DL | +|-----------------------------|:---:|:---:|:---:|:---:| +| Global Page Tracking | X | X | X | | +| Order Conversion Tracking | X | X | X | | +| Page Type Event | | X | X | | +| Product Clicks | | X | X | | +| Product Detail Impressions | | X | X | | +| Add to Cart | | X | X | | +| Remove from Cart | | X | X | | +| Checkout Steps | | X | X | | +| Order Refunds | | X | X | | +| Admin Order Tracking | | X | X | | +| Access DataLayer using JS | | | | X | +| Bing UET Tracking | | | | X | +| Full Facebook Tracking | | | | X | +| Custom Image Pixel Tracking | | | | X | +| Custom iFrame Tracking | | | | X | +| Third-Party Integration | | | | X | +| Extend individual page type | | | | X | GTM - [Google Tag Manager for Magento 2 Extension](https://www.magepal.com/magento2/extensions/google-tag-manager.html) +GA4 - [Google Analytics 4 for Google Tag Manager Extension](https://www.magepal.com/magento2/extensions/google-analytics-4-for-google-tag-manager.html) + EE - [Enhanced E-commerce for Google Tag Manager Extension](https://www.magepal.com/magento2/extensions/enhanced-ecommerce-for-google-tag-manager.html) DL - [DataLayer for Google Tag Manager Extension](https://www.magepal.com/magento2/extensions/datalayer-for-google-tag-manager.html) @@ -152,22 +158,26 @@ Our Magento extension provide a vast array of over 60 preconfigure data layer el * Events * productImpression*, categoryPage**, allPage**, catalogCategoryViewPage**, mpCustomerSession * productClick*, addToCart*, productListSwatchClicked**, productListSwatchSelected** + * view_item_list***, select_item***, add_to_cart*** ##### Product Detail Page Events * Events * productDetail*, productImpression*, productPage**, allPage**, catalogProductViewPage**, mpCustomerSession - * productClick*, addToCart*, removeFromCart*, productDetailSwatchClicked**, productDetailSwatchSelected**, addToCartItemOutOfStock*, addToCartItemOptionRequired* + * productClick*, addToCart*, removeFromCart*, productDetailSwatchClicked**, productDetailSwatchSelected**, addToCartItemOutOfStock*, addToCartItemOptionRequired*, addToCartItemInvalidQtyIncrements* + * view_item***, select_item***, add_to_cart***, view_item_list*** ##### Shopping Cart Page Events * Events * cartPage**, allPage**, checkoutCartIndexPage**, productImpression*, mpCustomerSession * productClick*, addToCart*, removeFromCart* - + * select_item***, add_to_cart***, remove_from_cart***, view_item_list*** + ##### Checkout Page Events * Events * checkoutPage**, allPage**, checkoutIndexIndexPage**, checkout*, checkoutOption*, mpCustomerSession * checkoutEmailValidation*, shippingMethodAdded*, checkoutShippingStepCompleted*, checkoutShippingStepFailed*, paymentMethodAdded*, checkoutPaymentStepFailed*, checkoutPaymentStepCompleted* - + * begin_checkout***, add_shipping_info***, add_payment_info*** + ##### Order Confirmation Page Events * Events * purchase*, orderSuccessPage**, allPage**, checkoutOnepageSuccessPage** @@ -345,7 +355,8 @@ Our Magento extension provide a vast array of over 60 preconfigure data layer el `*` - Data layer provide by our [Enhanced Ecommerce Extension](https://www.magepal.com/enhanced-ecommerce-for-google-tag-manager.html) `**` - Data layer provide by our [Data Layer Extension](https://www.magepal.com/magento2/extensions/datalayer-for-google-tag-manager.html) - + +`***` - Data layer provide by our [Google Analytics 4 Extension](https://www.magepal.com/google-analytics-4-for-google-tag-manager.html) Contribution --- diff --git a/composer.json b/composer.json index 65b69b5..0b3a16d 100755 --- a/composer.json +++ b/composer.json @@ -37,13 +37,13 @@ "php": "~5.6.0|7.0.2|7.0.4|~7.0.6|~7.1.0|~7.1.3|~7.2.0|~7.3.0|~7.4.0", "magento/module-backend": "100.0.*|100.1.*|100.2.*|101.0.*|102.0.*", "magento/framework": "100.0.*|100.1.*|101.0.*|102.0.*|103.0.*", - "magepal/magento2-core":">1.1.0" + "magepal/magento2-core": ">=1.1.11" }, "suggest" : { "magepal/magento2-enhanced-ecommerce": "Get more from Google Tag Manager with Enhanced E-commerce. Learn more at https://www.magepal.com/enhanced-ecommerce-for-google-tag-manager.html" }, "type": "magento2-module", - "version": "2.5.5", + "version": "2.6.0", "autoload": { "files": [ "registration.php" diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 7dcbdc1..7a9ebd4 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -15,65 +15,33 @@ magepal MagePal_GoogleTagManager::googletagmanager - + 1 - Copyright © 2020 MagePal, LLC + +
+ Upgrade to the next generation of tracking from Google. Google Analytics 4 comes with a bunch of new features + that make it very similar, yet more powerful than Enhanced Ecommerce. + Gain access to GA4 new approach to privacy-first tracking, channel measurement, and AI based predictive data with our new Google Analytics 4 extension. +
Want to learn more about your customers? Gain valuable insight on your customers shopping behavior, sales performance and more. Upgrade to our new Enhanced E-commerce today, to take full advantage of Google Analytics most valuable features and reports.
- Adding Facebook Pixel, Bing UET, or other third-party JavaScript to your site using Google Tag Manager? - Learn how simple and easy it is to integrate any third party service with our new DataLayer extension. + Adding Facebook Pixel, Bing UET, or other third-party JavaScript to your site using Google Tag Manager? + Learn how simple and easy it is to integrate any third party service with our new DataLayer extension.
-
- +
]]>