/**
  * Push `trackSiteSearch' to tracker on search result page
  *
  * @param \Magento\Framework\Event\Observer $observer
  * @return \Henhed\Piwik\Observer\SearchResultObserver
  */
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
     if (!$this->_dataHelper->isTrackingEnabled()) {
         return $this;
     }
     $query = $this->_queryFactory->get();
     $piwikBlock = $this->_view->getLayout()->getBlock('piwik.tracker');
     /* @var $query \Magento\Search\Model\Query */
     /* @var $piwikBlock \Henhed\Piwik\Block\Piwik */
     $keyword = $query->getQueryText();
     $resultsCount = $query->getNumResults();
     if (is_null($resultsCount)) {
         // If this is a new search query the result count hasn't been saved
         // yet so we have to fetch it from the search result block instead.
         $resultBock = $this->_view->getLayout()->getBlock('search.result');
         /* @var $resultBock \Magento\CatalogSearch\Block\Result */
         if ($resultBock) {
             $resultsCount = $resultBock->getResultCount();
         }
     }
     if (is_null($resultsCount)) {
         $this->_piwikTracker->trackSiteSearch($keyword);
     } else {
         $this->_piwikTracker->trackSiteSearch($keyword, false, (int) $resultsCount);
     }
     if ($piwikBlock) {
         // Don't push `trackPageView' when `trackSiteSearch' is set
         $piwikBlock->setSkipTrackPageView(true);
     }
     return $this;
 }
 /**
  * Push trackEcommerceOrder to tracker on checkout success page
  *
  * @param \Magento\Framework\Event\Observer $observer
  * @return \Henhed\Piwik\Observer\CheckoutSuccessObserver
  */
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
     $orderIds = $observer->getEvent()->getOrderIds();
     if (!$this->_dataHelper->isTrackingEnabled() || empty($orderIds) || !is_array($orderIds)) {
         return $this;
     }
     $collection = $this->_orderCollectionFactory->create();
     $collection->addFieldToFilter('entity_id', ['in' => $orderIds]);
     // Group order items by SKU since Piwik doesn't seem to handle multiple
     // `addEcommerceItem' with the same SKU.
     $piwikItems = [];
     // Aggregate all placed orders into one since Piwik seems to only
     // register one `trackEcommerceOrder' per request. (For multishipping)
     $piwikOrder = [];
     foreach ($collection as $order) {
         /* @var $order \Magento\Sales\Model\Order */
         foreach ($order->getAllVisibleItems() as $item) {
             /* @var $item \Magento\Sales\Model\Order\Item */
             $sku = $item->getSku();
             $name = $item->getName();
             $price = (double) $item->getBasePriceInclTax();
             $qty = (double) $item->getQtyOrdered();
             if (!isset($piwikItems[$sku])) {
                 $piwikItems[$sku] = [$sku, $name, $price * $qty, $qty];
             } else {
                 // Aggregate row total instead of unit price in case there
                 // are different prices for the same SKU.
                 $piwikItems[$sku][2] += $price * $qty;
                 $piwikItems[$sku][3] += $qty;
             }
         }
         $orderId = $order->getIncrementId();
         $grandTotal = (double) $order->getBaseGrandTotal();
         $subTotal = (double) $order->getBaseSubtotalInclTax();
         $tax = (double) $order->getBaseTaxAmount();
         $shipping = (double) $order->getBaseShippingInclTax();
         $discount = abs((double) $order->getBaseDiscountAmount());
         if (empty($piwikOrder)) {
             $piwikOrder = [$orderId, $grandTotal, $subTotal, $tax, $shipping, $discount];
         } else {
             $piwikOrder[0] .= ', ' . $orderId;
             $piwikOrder[1] += $grandTotal;
             $piwikOrder[2] += $subTotal;
             $piwikOrder[3] += $tax;
             $piwikOrder[4] += $shipping;
             $piwikOrder[5] += $discount;
         }
     }
     // Push `addEcommerceItem'
     foreach ($piwikItems as $piwikItem) {
         list($sku, $name, $rowTotal, $qty) = $piwikItem;
         $this->_piwikTracker->addEcommerceItem($sku, $name, false, $qty > 0 ? $rowTotal / $qty : 0, $qty);
     }
     // Push `trackEcommerceOrder'
     if (!empty($piwikOrder)) {
         list($orderId, $grandTotal, $subTotal, $tax, $shipping, $discount) = $piwikOrder;
         $this->_piwikTracker->trackEcommerceOrder($orderId, $grandTotal, $subTotal, $tax, $shipping, $discount > 0 ? $discount : false);
     }
     return $this;
 }
 /**
  * Push trackEcommerceCartUpdate to tracker on cart view page
  *
  * @param \Magento\Framework\Event\Observer $observer
  * @return \Henhed\Piwik\Observer\CartViewObserver
  */
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
     if ($this->_dataHelper->isTrackingEnabled()) {
         $this->_trackerHelper->addQuote($this->_checkoutSession->getQuote(), $this->_piwikTracker);
     }
     return $this;
 }
 /**
  * Push EcommerceView to tracker on category view page
  *
  * @param \Magento\Framework\Event\Observer $observer
  * @return \Henhed\Piwik\Observer\CategoryViewObserver
  */
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
     if (!$this->_dataHelper->isTrackingEnabled()) {
         return $this;
     }
     $category = $observer->getEvent()->getCategory();
     /* @var $category \Magento\Catalog\Model\Category */
     $this->_piwikTracker->setEcommerceView(false, false, $category->getName());
     return $this;
 }
 /**
  * Push link tracking options to given tracker
  *
  * @param \Henhed\Piwik\Model\Tracker $tracker
  * @return \Henhed\Piwik\Observer\BeforeTrackPageViewObserver
  */
 protected function _pushLinkTracking(\Henhed\Piwik\Model\Tracker $tracker)
 {
     if ($this->_dataHelper->isLinkTrackingEnabled()) {
         $tracker->enableLinkTracking(true);
         $delay = $this->_dataHelper->getLinkTrackingDelay();
         if ($delay > 0) {
             $tracker->setLinkTrackingTimer($delay);
         }
     }
     return $this;
 }
 /**
  * Push EcommerceView to tracker on product view page
  *
  * @param \Magento\Framework\Event\Observer $observer
  * @return \Henhed\Piwik\Observer\ProductViewObserver
  */
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
     if (!$this->_dataHelper->isTrackingEnabled()) {
         return $this;
     }
     $product = $observer->getEvent()->getProduct();
     /* @var $product \Magento\Catalog\Model\Product */
     $category = $product->getCategory();
     $this->_piwikTracker->setEcommerceView($product->getSku(), $product->getName(), $category ? $category->getName() : false, $product->getFinalPrice());
     return $this;
 }
 /**
  * Add `trackEcommerceCartUpdate' checkout cart customer data
  *
  * @param \Magento\Checkout\CustomerData\Cart $subject
  * @param array $result
  * @return array
  */
 public function afterGetSectionData(\Magento\Checkout\CustomerData\Cart $subject, $result)
 {
     if ($this->_dataHelper->isTrackingEnabled()) {
         $quote = $this->_checkoutSession->getQuote();
         if ($quote->getId()) {
             $tracker = $this->_trackerFactory->create();
             $this->_trackerHelper->addQuote($quote, $tracker);
             $result['piwikActions'] = $tracker->toArray();
         }
     }
     return $result;
 }
 /**
  * Generate Piwik tracking script
  *
  * @return string
  */
 protected function _toHtml()
 {
     if ($this->_dataHelper->isTrackingEnabled()) {
         $this->_prepareTracker();
         return parent::_toHtml();
     }
     return '';
 }
 /**
  * Test \Henhed\Piwik\Helper\Data::isLinkTrackingEnabled
  *
  * Also covers `isTrackingEnabled'
  *
  * @param bool $linkEnabled
  * @param bool $enabled
  * @param string $hostname
  * @param int $siteId
  * @param bool $returnValue
  * @dataProvider isLinkTrackingEnabledDataProvider
  */
 public function testIsLinkTrackingEnabled($linkEnabled, $enabled, $hostname, $siteId, $returnValue)
 {
     $this->_prepareScopeConfigMock($enabled, $hostname, $siteId, $linkEnabled);
     $this->assertEquals($returnValue, $this->_helper->isLinkTrackingEnabled());
 }