|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * |
| 4 | + * Based on Piwik Extension for Magento created by Adrian Speyer |
| 5 | + * |
| 6 | + * @category Matomo |
| 7 | + * @package Matomo_Analytics |
| 8 | + * @copyright Copyright (c) 2018 Thiago Contardi. |
| 9 | + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 10 | + * |
| 11 | + */ |
| 12 | + |
| 13 | +class Matomo_Analytics_Block_Script extends Mage_Core_Block_Template |
| 14 | +{ |
| 15 | + /** |
| 16 | + * Get a specific page name (may be customized via layout) |
| 17 | + * |
| 18 | + * @return string|null |
| 19 | + */ |
| 20 | + public function getPageName() |
| 21 | + { |
| 22 | + return $this->_getData('page_name'); |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Render information about specified orders and their items |
| 27 | + * http://piwik.org/docs/ecommerce-analytics/ |
| 28 | + */ |
| 29 | + public function getOrdersTrackingCode() |
| 30 | + { |
| 31 | + $orderIds = $this->getOrderIds(); |
| 32 | + if (empty($orderIds) || !is_array($orderIds)) { |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + /** @var Mage_Sales_Model_Resource_Order_Collection $collection */ |
| 37 | + $collection = Mage::getResourceModel('sales/order_collection') |
| 38 | + ->addFieldToFilter('entity_id', array('in' => $orderIds)); |
| 39 | + $result = array(); |
| 40 | + |
| 41 | + /** @var Mage_Sales_Model_Order $order */ |
| 42 | + foreach ($collection as $order) { |
| 43 | + /** @var Mage_Sales_Model_Order_Item $item */ |
| 44 | + foreach ($order->getAllVisibleItems() as $item) { |
| 45 | + |
| 46 | + //get category name |
| 47 | + $productId = $item->product_id; |
| 48 | + |
| 49 | + /** @var Mage_Catalog_Model_Product $product */ |
| 50 | + $product = Mage::getModel('catalog/product')->load($productId); |
| 51 | + $categoryName = ''; |
| 52 | + $categoryIds = $product->getCategoryIds(); |
| 53 | + |
| 54 | + if (!empty($categoryIds)) { |
| 55 | + $categoryId = $categoryIds[0]; |
| 56 | + /** @var Mage_Catalog_Model_Category $category */ |
| 57 | + $category = Mage::getModel('catalog/category')->load($categoryId); |
| 58 | + $categoryName = $category->getName(); |
| 59 | + } |
| 60 | + |
| 61 | + if ($item->getQtyOrdered()) { |
| 62 | + $qty = number_format($item->getQtyOrdered(), 0, '.', ''); |
| 63 | + } else { |
| 64 | + $qty = '0'; |
| 65 | + } |
| 66 | + $result[] = sprintf('_paq.push([\'addEcommerceItem\', \'%s\', \'%s\', \'%s\', %s, %s]);', |
| 67 | + $this->jsQuoteEscape($item->getSku()), |
| 68 | + $this->jsQuoteEscape($item->getName()), |
| 69 | + $categoryName, |
| 70 | + $item->getBasePrice(), |
| 71 | + $qty |
| 72 | + ); |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + if ($order->getGrandTotal()) { |
| 77 | + $subtotal = $order->getGrandTotal() - $order->getShippingAmount() - $order->getShippingTaxAmount(); |
| 78 | + } else { |
| 79 | + $subtotal = '0.00'; |
| 80 | + } |
| 81 | + $result[] = sprintf("_paq.push(['trackEcommerceOrder' , '%s', %s, %s, %s, %s]);", |
| 82 | + $order->getIncrementId(), |
| 83 | + $order->getBaseGrandTotal(), |
| 84 | + $subtotal, |
| 85 | + $order->getBaseTaxAmount(), |
| 86 | + $order->getBaseShippingAmount() |
| 87 | + ); |
| 88 | + } |
| 89 | + return implode("\n", $result); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Render information when cart updated |
| 94 | + * http://piwik.org/docs/ecommerce-analytics/ |
| 95 | + */ |
| 96 | + public function getEcommerceCartUpdate() |
| 97 | + { |
| 98 | + $result = array(); |
| 99 | + |
| 100 | + /** @var Mage_Checkout_Model_Cart $cart */ |
| 101 | + $cart = Mage::getModel('checkout/cart'); |
| 102 | + /** @var Mage_Sales_Model_Quote $quote */ |
| 103 | + $quote = $cart->getQuote(); |
| 104 | + |
| 105 | + /** @var Mage_Sales_Model_Quote_Item $cartItem */ |
| 106 | + foreach ($quote->getAllVisibleItems() as $cartItem) { |
| 107 | + |
| 108 | + //get category name |
| 109 | + $productId = $cartItem->product_id; |
| 110 | + |
| 111 | + /** @var Mage_Catalog_Model_Product $product */ |
| 112 | + $product = Mage::getModel('catalog/product')->load($productId); |
| 113 | + $categoryName = ''; |
| 114 | + $categoryIds = $product->getCategoryIds(); |
| 115 | + if (!empty($categoryIds)) { |
| 116 | + $categoryId = $categoryIds[0]; |
| 117 | + /** @var Mage_Catalog_Model_Category $category */ |
| 118 | + $category = Mage::getModel('catalog/category')->load($categoryId); |
| 119 | + $categoryName = $category->getName(); |
| 120 | + } |
| 121 | + $productName = $cartItem->getName(); |
| 122 | + $productName = str_replace('"', "", $productName); |
| 123 | + |
| 124 | + if ($cartItem->getPrice() == 0 || $cartItem->getPrice() < 0.00001): |
| 125 | + continue; |
| 126 | + endif; |
| 127 | + |
| 128 | + $result[] = sprintf( |
| 129 | + "_paq.push(['addEcommerceItem', '%s', '%s', '%s', %s, %s]);", |
| 130 | + $this->jsQuoteEscape($cartItem->getSku()), |
| 131 | + $this->jsQuoteEscape($productName), |
| 132 | + $categoryName, |
| 133 | + $cartItem->getPrice(), |
| 134 | + $cartItem->getQty() |
| 135 | + ); |
| 136 | + } |
| 137 | + |
| 138 | + //total in cart |
| 139 | + $grandTotal = $quote->getGrandTotal(); |
| 140 | + if ($grandTotal != 0) { |
| 141 | + $result[] = sprintf("_paq.push(['trackEcommerceCartUpdate', %s]);", $grandTotal); |
| 142 | + } |
| 143 | + |
| 144 | + return implode("\n", $result); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Render information when product page view |
| 149 | + * http://piwik.org/docs/ecommerce-analytics/ |
| 150 | + */ |
| 151 | + public function getProductPageview() |
| 152 | + { |
| 153 | + /** @var Mage_Catalog_Model_Product $product */ |
| 154 | + $product = Mage::registry('current_product'); |
| 155 | + |
| 156 | + if ($product instanceof Mage_Catalog_Model_Product) { |
| 157 | + |
| 158 | + $categoryName = ''; |
| 159 | + $categoryIds = $product->getCategoryIds(); |
| 160 | + if (!empty($categoryIds)) { |
| 161 | + $categoryId = $categoryIds[0]; |
| 162 | + /** @var Mage_Catalog_Model_Category $category */ |
| 163 | + $category = Mage::getModel('catalog/category')->load($categoryId); |
| 164 | + $categoryName = $category->getName(); |
| 165 | + } |
| 166 | + $productName = $product->getName(); |
| 167 | + |
| 168 | + return sprintf( |
| 169 | + "_paq.push(['setEcommerceView', '%s', '%s', '%s', %s]);", |
| 170 | + $this->jsQuoteEscape($product->getSku()), |
| 171 | + $this->jsQuoteEscape($productName), |
| 172 | + $categoryName, |
| 173 | + $product->getFinalPrice() |
| 174 | + ); |
| 175 | + } |
| 176 | + return ''; |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Render information of category view |
| 181 | + * http://piwik.org/docs/ecommerce-analytics/ |
| 182 | + */ |
| 183 | + public function getCategoryPageview() |
| 184 | + { |
| 185 | + $currentCategory = Mage::registry('current_category'); |
| 186 | + |
| 187 | + if ($currentCategory instanceof Mage_Catalog_Model_Category) { |
| 188 | + return sprintf("_paq.push(['setEcommerceView', false, false, '%s']);", $currentCategory->getName()); |
| 189 | + } |
| 190 | + |
| 191 | + return ''; |
| 192 | + } |
| 193 | + |
| 194 | + /** |
| 195 | + * Render Piwik tracking scripts |
| 196 | + * |
| 197 | + * @return string |
| 198 | + */ |
| 199 | + public function _toHtml() |
| 200 | + { |
| 201 | + if (!Mage::helper('matomoanalytics')->isEnabled()) { |
| 202 | + return ''; |
| 203 | + } |
| 204 | + return parent::_toHtml(); |
| 205 | + } |
| 206 | +} |
0 commit comments