From 22a4b76d2839c1fc00159981284260186d7802c3 Mon Sep 17 00:00:00 2001 From: Renon Stewart Date: Wed, 22 Jul 2020 08:06:53 -0400 Subject: [PATCH] Version 2.5.0 --- Block/Data/Product.php | 18 +++---- Block/DataLayerAbstract.php | 2 + Block/GtmCode.php | 1 - Helper/DataLayerItem.php | 25 ++++++---- Model/Cart.php | 5 +- Model/Order.php | 10 ++-- README.md | 96 ++++++++++++++++++++++++++++--------- composer.json | 7 +-- 8 files changed, 113 insertions(+), 51 deletions(-) diff --git a/Block/Data/Product.php b/Block/Data/Product.php index b4e47bd..5b420af 100644 --- a/Block/Data/Product.php +++ b/Block/Data/Product.php @@ -62,9 +62,8 @@ protected function _prepareLayout() { /** @var $tm DataLayer */ $tm = $this->getParentBlock(); - $product = $this->getProduct(); - if ($product) { + if ($product = $this->getProduct()) { $productData = [ 'id' => $product->getId(), 'sku' => $product->getSku(), @@ -99,15 +98,18 @@ public function getPrice() { /** @var $tm DataLayer */ $tm = $this->getParentBlock(); + $price = 0; /** @var $product ProductInterface */ - $product = $this->getProduct(); - - if ($product->getTypeId() == Type::TYPE_SIMPLE) { - return $tm->formatPrice($product->getPrice()); - } else { - return $tm->formatPrice($product->getFinalPrice()); + if ($product = $this->getProduct()) { + if ($product->getTypeId() == Type::TYPE_SIMPLE) { + $price = $product->getPrice(); + } else { + $price = $product->getFinalPrice(); + } } + + return $tm->formatPrice($price); } /** diff --git a/Block/DataLayerAbstract.php b/Block/DataLayerAbstract.php index 6048b67..97f395a 100755 --- a/Block/DataLayerAbstract.php +++ b/Block/DataLayerAbstract.php @@ -15,6 +15,8 @@ /** * @method getList() + * @method setListType() + * @method getListType() */ class DataLayerAbstract extends Template { diff --git a/Block/GtmCode.php b/Block/GtmCode.php index b5f43cb..6d63a33 100755 --- a/Block/GtmCode.php +++ b/Block/GtmCode.php @@ -13,7 +13,6 @@ class GtmCode extends Template { - /** * @var GtmHelper */ diff --git a/Helper/DataLayerItem.php b/Helper/DataLayerItem.php index 29afd68..2ba57af 100755 --- a/Helper/DataLayerItem.php +++ b/Helper/DataLayerItem.php @@ -69,12 +69,13 @@ public function isCategoryLayerEnabled($store_id = null) } /** - * @param OrderItem $item + * @param OrderItem $item | QuoteItem $item * @return array + * @throws \Magento\Framework\Exception\LocalizedException */ public function getCategories($item) { - if (!$this->isCategoryLayerEnabled()) { + if (!$this->isCategoryLayerEnabled() || !$item->getProduct()) { return []; } @@ -147,13 +148,17 @@ public function getItemVariant($item) } if (!array_key_exists($item->getItemId(), $this->variants)) { - $productOptions = []; - if ($item instanceof OrderItem) { $productOptions = $this->getItemOptions($item->getProductOptions()); - } elseif ($item instanceof QuoteItem) { - $itemOptionInstance = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct()); + } elseif ($item instanceof QuoteItem + && $item->getProduct() + && $item->getProduct()->getCustomOption('simple_product') + && $item->getProduct()->getCustomOption('simple_product')->getProduct() + ) { + $itemOptionInstance = $item->getProduct()->getTypeInstance()->getOrderOptions($item->getProduct()); $productOptions = $this->getItemOptions($itemOptionInstance); + } else { + $productOptions = ''; } $this->variants[$item->getItemId()] = $this->getItemVariantOption($productOptions); @@ -211,11 +216,15 @@ public function getProductObject($item, $qty) $product = [ 'name' => $item->getName(), 'id' => $item->getSku(), - 'price' => $this->formatPrice($item->getPrice() ?: $item->getProduct()->getPrice()), + 'price' => $this->formatPrice($item->getPrice()), 'quantity' => $qty * 1, - 'parent_sku' => $item->getProduct()->getData('sku'), + 'parent_sku' => $item->getProduct() ? $item->getProduct()->getData('sku') : $item->getSku(), ]; + if (!$item->getPrice() && $item->getProduct()) { + $product['price'] = $this->formatPrice($item->getProduct()->getPrice()); + } + if ($variant = $this->getItemVariant($item)) { $product['variant'] = $variant; } diff --git a/Model/Cart.php b/Model/Cart.php index a71a733..361179c 100644 --- a/Model/Cart.php +++ b/Model/Cart.php @@ -13,7 +13,6 @@ use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Quote\Model\Quote; -use Magento\Quote\Model\Quote\Item; use MagePal\GoogleTagManager\DataLayer\QuoteData\QuoteItemProvider; use MagePal\GoogleTagManager\DataLayer\QuoteData\QuoteProvider; use MagePal\GoogleTagManager\Helper\DataLayerItem as dataLayerItemHelper; @@ -27,7 +26,7 @@ class Cart extends DataObject protected $checkoutSession; /** - * @var dataLayerdataLayerItemHelper + * @var dataLayerItemHelper */ protected $dataLayerItemHelper; @@ -93,7 +92,7 @@ public function getCart() foreach ($quote->getAllVisibleItems() as $item) { $itemData = [ 'sku' => $item->getSku(), - 'parent_sku' => $item->getProduct()->getData('sku'), + 'parent_sku' => $item->getProduct() ? $item->getProduct()->getData('sku') : $item->getSku(), 'name' => $this->escapeJsQuote($item->getName()), 'product_type' => $item->getProductType(), 'price' => $this->dataLayerItemHelper->formatPrice($item->getPrice()), diff --git a/Model/Order.php b/Model/Order.php index 665d8bc..1c50907 100644 --- a/Model/Order.php +++ b/Model/Order.php @@ -35,7 +35,7 @@ class Order extends DataObject protected $gtmHelper; /** - * @var CollectionFactory + * @var CollectionFactoryInterface */ protected $_salesOrderCollection; @@ -113,7 +113,7 @@ public function getOrderLayer() $result = []; - /* @var OrderAlias $order */ + /* @var SalesOrder $order */ foreach ($collection as $order) { $products = []; @@ -204,20 +204,20 @@ public function escapeReturn($data) } /** - * @param OrderAlias $order + * @param SalesOrder $order * @return array * @throws NoSuchEntityException */ public function getOrderDataLayer(SalesOrder $order) { - /* @var OrderAlias $order */ + /* @var SalesOrder $order */ /* @var Item $item */ $products = []; foreach ($order->getAllVisibleItems() as $item) { $product = [ 'sku' => $item->getSku(), 'id' => $item->getSku(), - 'parent_sku' => $item->getProduct()->getData('sku'), + 'parent_sku' => $item->getProduct() ? $item->getProduct()->getData('sku') : $item->getSku(), 'name' => $this->escapeJsQuote($item->getProductOptionByCode('simple_name') ?: $item->getName()), 'parent_name' => $this->escapeJsQuote($item->getName()), 'price' => $this->gtmHelper->formatPrice($item->getBasePrice()), diff --git a/README.md b/README.md index 469a8f6..65840ee 100755 --- a/README.md +++ b/README.md @@ -8,22 +8,33 @@ #### 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 and 2.3.x +Magento Enhanced Ecommerce for Google Tag Manager + ### What is Google Tag Manager -Google Tag Manager (GTM) is a user-friendly, powerful and essential integration for every Magento store. It simplifies the process of adding, editing and managing third-party JavaScript tags and other snippets of code on your Magento site. -With GTM, you can quickly and easily add Facebook tags, AdWords Conversion Tracking, Re-marketing, Bing UET, SnapChat, DoubleClick code, Google Analytics and many more in a breeze without the need for a developer to make changes to your Magento code providing the data is available to Google Tag Manager. +Google Tag Manager (GTM) is a user-friendly, powerful and essential integration for every Magento store. It simplifies +the process of adding, editing and managing third-party JavaScript tags and other snippets of code on your Magento site. +With GTM, you can quickly and easily add Facebook tags, AdWords Conversion Tracking, Re-marketing, Bing UET, SnapChat, +DoubleClick code, Google Analytics, and many more in a breeze without the need for a developer to make changes to your +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 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. -Our extension provide 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. +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.4.0 has some breaking changes to Enhanced Ecommerce. Please download the latest version of Enhanced Ecommerce 1.4.0 or greater from www.magepal.com account. +>: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. ### Why use our Google Tag Manager extension? -Adding Google Tag Manager code snippet to the header section of your Magento store may seem like the ideal and most efficient way to add GTM to your site. But this will not be sufficient and limit your ability to take full advantage of GTM when integrating third-parties tracking codes that require data from your Magento stores, 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.  +Adding Google Tag Manager code snippet to the header section of your Magento store may seem like the ideal, +and most efficient way to add GTM to your site. But this will not be sufficient and limit your ability to take +full advantage of GTM when integrating third-parties tracking codes that require data from your Magento stores, +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.  Learn more about [customizing Google Tag Manger](https://www.magepal.com/help/docs/google-tag-manager-for-magento/#api). ### Google Analytics Enhanced E-commerce @@ -34,11 +45,12 @@ Learn more about our [Google Enhanced Ecommerce](https://www.magepal.com/enhance ### Third Party Integration with Google Tag Manager Adding Facebook pixel, Bing UAT, SnapChat or any other third-party code snippet to your website but frustrated by -all the hassle and time it take to configure Google Tag Manager? Learn how simple and easy it is to integrate any +all the hassle and time it takes to configure Google Tag Manager? Learn how simple and easy it is to integrate any tracking code to your Magento store with our new [DataLayer extension](https://www.magepal.com/datalayer-for-google-tag-manager.html?utm_source=data%20layer%20for%20Google%20Tag%20Manager&utm_medium=github). ### General Data Protection Regulation (GDPR) Support -Now you can quickly disable analytic tracking for customer who do not want to by track by enabling Cookie Restriction Mode or base on existing or non-existing cookie. +Now you can quickly disable analytic tracking for customers' who do not want to by track by enabling Cookie Restriction +Mode or base on existing or non-existing cookie. - Stores > Configuration > General > Web > Default Cookie Settings > Cookie Restriction Mode. @@ -49,7 +61,7 @@ Please Note: Merchants should consult with their own legal counsel to ensure tha | Features | GTM | EE | DL | |-----------------------------|:---:|:--:|:--:| | Global Page Tracking | X | X | | -| Order Transaction Tracking | X | X | | +| Order Conversion Tracking | X | X | | | Page Type Event | | X | | | Product Clicks | | X | | | Product Detail Impressions | | X | | @@ -80,17 +92,24 @@ DL - [DataLayer for Google Tag Manager Extension](https://www.magepal.com/magent * Fully customizable with 10 lines of code * General Data Protection Regulation (GDPR) Support -![Google Tag Manager for Magento](https://image.ibb.co/dhmoLx/Google_Tag_Manager_for_Magento2_by_Magepal.png) - ### Benefits of using Google Tag Manager with Magento There are a number of benefits to using GTM with Magento: -- One Centralized Tag Management source - Google tag Manager is one of the tops and most widely used JavaScript tag management, therefore, anyone with Google Tag Manager experience will have all the knowledge they need to make edits to your site. -- Little to No Technically Knowledge - Digital marketer agencies with so tech skills can quickly make and publish changes to Google Tag Manager without needing to call in developers. -- Version Control - Every change to your Googe Tag Manager container is tracked with a history of who and what was changed. -- Easy to Use - Google Tag Manager is very simple and easy to use. You can easily export your GTM configuration in a text file that could be saved and reimport. -- Reduce Number of Magento Extensions Needed - Installing individual extensions for AdWords, Facebook tracking, Snapchat, Microsoft Bing is time consuming and resource intensive on your Magento store. Using Tag Manager you only need to install and maintaining one extension. -- Eliminate Themes and Order Success Page Edits - 99% of merchants, developers and agencies don't know or use best practice when inserting javascript tracking code snippets to a Magento store, and often just add hardcode each javascript code snippets at random places within the themes files which make it unmaintainable over time as you switch between different service provider. +- One Centralized Tag Management source - Google tag Manager is one of the tops, and most widely used JavaScript tag +management, therefore, anyone with Google Tag Manager experience will have all the knowledge they need to make edits +to your site. +- Little to No Technically Knowledge - Digital marketer agencies with so tech skills can quickly make and publish +changes to Google Tag Manager without needing to call in developers. +- Version Control - Every change to your Google Tag Manager container is track with a history of who and what was changed. +- Easy to Use - Google Tag Manager is very simple and easy to use. You can easily export your GTM configuration in a +text file that could be saved and reimport. +- Reduce Number of Magento Extensions Needed - Installing individual extensions for AdWords, Facebook tracking, +Snapchat, Microsoft Bing is time-consuming and resource intensive on your Magento store. Using Tag Manager you only +need to install and maintaining one extension. +- Eliminate Themes and Order Success Page Edits - 99% of merchants, developers and agencies don't know or use best +practice when inserting javascript tracking code snippets to a Magento store, and often just add hardcode each +javascript code snippets at random places within the themes files which make it unmaintainable over time as you switch +between different service provider. ### How to Customize Google Tag Manager Extension Need to add more data to your data layer or change existing data to meet your client needs? @@ -121,11 +140,42 @@ composer require magepal/magento2-googletagmanager Our Magento extension provide a vast array of over 60 preconfigure data layer elements to make integrating your Magento store with any third-party service a breeze using Google Tag Manager. -* Trigger: event equals gtm.dom - * pageType (i.e catalog_category_view) - * list (cart, category, detail, other) - +### Triggered Events +##### Home Page Events +* Events + * homePage**, allPage**, cmsIndexIndexPage**, mpCustomerSession + +##### Category Page Events +* Events + * productImpression*, categoryPage**, allPage**, catalogCategoryViewPage**, mpCustomerSession + * productClick*, addToCart*, productListSwatchClicked**, productListSwatchSelected** + +##### Product Detail Page Events +* Events + * productDetail*, productImpression*, productPage**, allPage**, catalogProductViewPage**, mpCustomerSession + * productClick*, addToCart*, removeFromCart*, productDetailSwatchClicked**, productDetailSwatchSelected**, addToCartItemOutOfStock*, addToCartItemOptionRequired* + +##### Shopping Cart Page Events +* Events + * cartPage**, allPage**, checkoutCartIndexPage**, productImpression*, mpCustomerSession + * productClick*, addToCart*, removeFromCart* + +##### Checkout Page Events +* Events + * checkoutPage**, allPage**, checkoutIndexIndexPage**, checkout*, checkoutOption*, mpCustomerSession + * checkoutEmailValidation*, shippingMethodAdded*, checkoutShippingStepCompleted*, checkoutShippingStepFailed*, paymentMethodAdded*, checkoutPaymentStepFailed*, checkoutPaymentStepCompleted* + +##### Order Confirmation Page Events +* Events + * purchase*, orderSuccessPage**, allPage**, checkoutOnepageSuccessPage** + +##### Other Events +* Events + * compareProductAdded**, compareProductRemoved**, wishlistProductAdded**, wishlistProductRemoved**, customerLoginAfter**, customerRegisterAfter**, newsletterSubscriberAdded** newsletterUnsubscribed** + +### Data Layer Variables + #### Customer * Trigger: event equals mpCustomerSession * customer.isLoggedIn @@ -304,7 +354,7 @@ Support --- If you encounter any problems or bugs, please open an issue on [GitHub](https://github.com/magepal/magento2-googletagmanager/issues). For fast Premium Support visit our [Google Tag Manager](https://www.magepal.com/magento2/extensions/google-tag-manager.html?utm_source=GTM&utm_medium=Premium%20Support) product page for detail. -Need help setting up or want to customize this extension to meet your business needs? Please email support@magepal.com and if we like your idea we will add this feature for free or at a discounted rate. +Need help to set up or want to customize our extension to meet your business needs? Please email support@magepal.com and if we like your idea we will add this feature for free or at a discounted rate. Magento 2 Extensions --- @@ -325,4 +375,4 @@ Magento 2 Extensions - [Custom SMTP](https://www.magepal.com/magento2/extensions/custom-smtp.html) - [Catalog Hover Image for Magento](https://www.magepal.com/magento2/extensions/catalog-hover-image-for-magento.html) -© MagePal LLC. | [www.magepal.com](http:/www.magepal.com) +© MagePal LLC. | [www.magepal.com](https://www.magepal.com) diff --git a/composer.json b/composer.json index 8b8599f..12a0d3e 100755 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ "license": [ "proprietary" ], - "homepage": "http://www.magepal.com/", + "homepage": "https://www.magepal.com/google-tag-manager.html", "support": { "email": "support@magepal.com", "issues": "https://github.com/magepal/magento2-google-tag-manager/issues/" @@ -36,10 +36,11 @@ "require": { "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", "magento/module-backend": "100.0.*|100.1.*|100.2.*|101.0.*", - "magento/framework": "100.0.*|100.1.*|101.0.*|102.0.*" + "magento/framework": "100.0.*|100.1.*|101.0.*|102.0.*", + "magepal/magento2-core":">1.1.0" }, "type": "magento2-module", - "version": "2.4.0", + "version": "2.5.0", "autoload": { "files": [ "registration.php"