Skip to content

Commit

Permalink
Merge pull request #24 from henkelund/2.0.0
Browse files Browse the repository at this point in the history
2.0.0
  • Loading branch information
henkelund committed Jan 28, 2018
2 parents 770c791 + cd288ac commit ec5e702
Show file tree
Hide file tree
Showing 43 changed files with 718 additions and 522 deletions.
10 changes: 3 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@ language: php
php:
- 7.1
- 7.0
- 5.6
env:
- M2_VERSION=2.1.9
- M2_VERSION=2.0.16
matrix:
include:
- php: 5.5
env: M2_VERSION=2.0.16
- M2_VERSION=2.2.2
- M2_VERSION=2.2.1
- M2_VERSION=2.2.0
cache:
directories:
- $HOME/.composer/cache
Expand Down
6 changes: 3 additions & 3 deletions Block/Piwik.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2016-2017 Henrik Hedelund
* Copyright 2016-2018 Henrik Hedelund
*
* This file is part of Henhed_Piwik.
*
Expand Down Expand Up @@ -156,11 +156,11 @@ public function getSiteId()
*/
public function getTrackingPixelUrl()
{
$params = array(
$params = [
'idsite' => $this->getSiteId(),
'rec' => 1,
'url' => $this->_urlBuilder->getCurrentUrl()
);
];
return $this->getTrackerUrl() . '?' . http_build_query($params);
}

Expand Down
7 changes: 4 additions & 3 deletions CustomerData/Checkout/CartPlugin.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2016-2017 Henrik Hedelund
* Copyright 2016-2018 Henrik Hedelund
*
* This file is part of Henhed_Piwik.
*
Expand Down Expand Up @@ -58,13 +58,13 @@ class CartPlugin
/**
* Constructor
*
* @param \Magento\Checkout\Model\Session $checkoutSession
* @param \Magento\Checkout\Model\Session\Proxy $checkoutSession
* @param \Henhed\Piwik\Helper\Data $dataHelper
* @param \Henhed\Piwik\Helper\Tracker $trackerHelper
* @param \Henhed\Piwik\Model\TrackerFactory $trackerFactory
*/
public function __construct(
\Magento\Checkout\Model\Session $checkoutSession,
\Magento\Checkout\Model\Session\Proxy $checkoutSession,
\Henhed\Piwik\Helper\Data $dataHelper,
\Henhed\Piwik\Helper\Tracker $trackerHelper,
\Henhed\Piwik\Model\TrackerFactory $trackerFactory
Expand All @@ -81,6 +81,7 @@ public function __construct(
* @param \Magento\Checkout\CustomerData\Cart $subject
* @param array $result
* @return array
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterGetSectionData(
\Magento\Checkout\CustomerData\Cart $subject,
Expand Down
3 changes: 2 additions & 1 deletion CustomerData/Customer/CustomerPlugin.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2016-2017 Henrik Hedelund
* Copyright 2016-2018 Henrik Hedelund
*
* This file is part of Henhed_Piwik.
*
Expand Down Expand Up @@ -96,6 +96,7 @@ protected function _getUserId()
* @param \Magento\Customer\CustomerData\Customer $subject
* @param array $result
* @return array
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterGetSectionData(
\Magento\Customer\CustomerData\Customer $subject,
Expand Down
4 changes: 2 additions & 2 deletions Helper/Data.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2016-2017 Henrik Hedelund
* Copyright 2016-2018 Henrik Hedelund
*
* This file is part of Henhed_Piwik.
*
Expand Down Expand Up @@ -98,7 +98,7 @@ public function getCdnHostname($store = null)
*/
protected function _getBaseUrl($host, $secure = null)
{
if (is_null($secure)) {
if ($secure === null) {
$secure = $this->_getRequest()->isSecure();
}
if (false !== ($scheme = strpos($host, '://'))) {
Expand Down
117 changes: 115 additions & 2 deletions Helper/Tracker.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2016-2017 Henrik Hedelund
* Copyright 2016-2018 Henrik Hedelund
*
* This file is part of Henhed_Piwik.
*
Expand All @@ -22,11 +22,13 @@

use Henhed\Piwik\Model\Tracker as TrackerModel;
use Magento\Quote\Model\Quote;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\OrderItemInterface;

/**
* Piwik tracker helper
*
* @link http://piwik.org/docs/ecommerce-analytics/
* @see http://piwik.org/docs/ecommerce-analytics/
*/
class Tracker extends \Magento\Framework\App\Helper\AbstractHelper
{
Expand Down Expand Up @@ -80,4 +82,115 @@ public function addQuote(Quote $quote, TrackerModel $tracker)
$this->addQuoteTotal($quote, $tracker);
return $this;
}

/**
* Push order contents to given tracker
*
* @param \Magento\Sales\Api\Data\OrderInterface[]|\Traversable $orders
* @param \Henhed\Piwik\Model\Tracker $tracker
* @return \Henhed\Piwik\Helper\Tracker
*/
public function addOrders($orders, TrackerModel $tracker)
{
$piwikItems = [];
$piwikOrder = [];

// Collect tracking data
foreach ($orders as $order) {
foreach ($order->getItems() as $item) {
if (!$item->getParentItemId()) {
$this->_appendOrderItemData($item, $piwikItems);
}
}
$this->_appendOrderData($order, $piwikOrder);
}

// Push `addEcommerceItem'
foreach ($piwikItems as $piwikItem) {
list($sku, $name, $rowTotal, $qty) = $piwikItem;

$tracker->addEcommerceItem(
$sku,
$name,
false,
($qty > 0) // div-by-zero protection
? $rowTotal / $qty // restore to unit price
: 0,
$qty
);
}

// Push `trackEcommerceOrder'
if (!empty($piwikOrder)) {
list($orderId, $grandTotal, $subTotal, $tax, $shipping, $discount)
= $piwikOrder;

$tracker->trackEcommerceOrder(
$orderId,
$grandTotal,
$subTotal,
$tax,
$shipping,
($discount > 0)
? $discount
: false
);
}

return $this;
}

/**
* @param OrderItemInterface $item
* @param array &$data
* @return void
*/
protected function _appendOrderItemData(OrderItemInterface $item, &$data)
{
$sku = $item->getSku();
$name = $item->getName();
$price = (float) $item->getBasePriceInclTax();
$qty = (float) $item->getQtyOrdered();

// Group order items by SKU since Piwik doesn't seem to handle multiple
// `addEcommerceItem' with the same SKU.
if (!isset($data[$sku])) {
$data[$sku] = [$sku, $name, $price * $qty, $qty];
} else {
// Aggregate row total instead of unit price in case there
// are different prices for the same SKU.
$data[$sku][2] += $price * $qty;
$data[$sku][3] += $qty;
}
}

/**
* @param OrderInterface $order
* @param array &$data
* @return void
*/
protected function _appendOrderData(OrderInterface $order, &$data)
{
$orderId = $order->getIncrementId();
$grandTotal = (float) $order->getBaseGrandTotal();
$subTotal = (float) $order->getBaseSubtotalInclTax();
$tax = (float) $order->getBaseTaxAmount();
$shipping = (float) $order->getBaseShippingInclTax();
$discount = abs((float) $order->getBaseDiscountAmount());

// Aggregate all placed orders into one since Piwik seems to only
// register one `trackEcommerceOrder' per request. (For multishipping)
if (empty($data)) {
$data = [
$orderId, $grandTotal, $subTotal, $tax, $shipping, $discount
];
} else {
$data[0] .= ', ' . $orderId;
$data[1] += $grandTotal;
$data[2] += $subTotal;
$data[3] += $tax;
$data[4] += $shipping;
$data[5] += $discount;
}
}
}
2 changes: 1 addition & 1 deletion Model/Config/Source/UserId/Provider.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2016-2017 Henrik Hedelund
* Copyright 2016-2018 Henrik Hedelund
*
* This file is part of Henhed_Piwik.
*
Expand Down
4 changes: 2 additions & 2 deletions Model/Tracker.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2016-2017 Henrik Hedelund
* Copyright 2016-2018 Henrik Hedelund
*
* This file is part of Henhed_Piwik.
*
Expand Down Expand Up @@ -249,7 +249,7 @@
* @method Tracker setSessionCookieTimeout(int $seconds)
* The default is 30 minutes
*
* @link http://developer.piwik.org/api-reference/tracking-javascript
* @see http://developer.piwik.org/api-reference/tracking-javascript
*/
class Tracker
{
Expand Down
2 changes: 1 addition & 1 deletion Model/Tracker/Action.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2016-2017 Henrik Hedelund
* Copyright 2016-2018 Henrik Hedelund
*
* This file is part of Henhed_Piwik.
*
Expand Down
4 changes: 2 additions & 2 deletions Observer/BeforeTrackPageViewObserver.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2016-2017 Henrik Hedelund
* Copyright 2016-2018 Henrik Hedelund
*
* This file is part of Henhed_Piwik.
*
Expand Down Expand Up @@ -55,7 +55,7 @@ public function __construct(\Henhed\Piwik\Helper\Data $dataHelper)
public function execute(\Magento\Framework\Event\Observer $observer)
{
$tracker = $observer->getEvent()->getTracker();
/* @var $tracker \Henhed\Piwik\Model\Tracker */
/** @var \Henhed\Piwik\Model\Tracker $tracker */

$this->_pushLinkTracking($tracker);

Expand Down
7 changes: 4 additions & 3 deletions Observer/CartViewObserver.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2016-2017 Henrik Hedelund
* Copyright 2016-2018 Henrik Hedelund
*
* This file is part of Henhed_Piwik.
*
Expand Down Expand Up @@ -63,13 +63,13 @@ class CartViewObserver implements ObserverInterface
* @param \Henhed\Piwik\Model\Tracker $piwikTracker
* @param \Henhed\Piwik\Helper\Tracker $trackerHelper
* @param \Henhed\Piwik\Helper\Data $dataHelper
* @param \Magento\Checkout\Model\Session $checkoutSession
* @param \Magento\Checkout\Model\Session\Proxy $checkoutSession
*/
public function __construct(
\Henhed\Piwik\Model\Tracker $piwikTracker,
\Henhed\Piwik\Helper\Tracker $trackerHelper,
\Henhed\Piwik\Helper\Data $dataHelper,
\Magento\Checkout\Model\Session $checkoutSession
\Magento\Checkout\Model\Session\Proxy $checkoutSession
) {
$this->_piwikTracker = $piwikTracker;
$this->_trackerHelper = $trackerHelper;
Expand All @@ -82,6 +82,7 @@ public function __construct(
*
* @param \Magento\Framework\Event\Observer $observer
* @return \Henhed\Piwik\Observer\CartViewObserver
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
Expand Down
4 changes: 2 additions & 2 deletions Observer/CategoryViewObserver.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2016-2017 Henrik Hedelund
* Copyright 2016-2018 Henrik Hedelund
*
* This file is part of Henhed_Piwik.
*
Expand Down Expand Up @@ -70,7 +70,7 @@ public function execute(\Magento\Framework\Event\Observer $observer)
}

$category = $observer->getEvent()->getCategory();
/* @var $category \Magento\Catalog\Model\Category */
/** @var \Magento\Catalog\Model\Category $category */

$this->_piwikTracker->setEcommerceView(
false,
Expand Down

0 comments on commit ec5e702

Please sign in to comment.