From f4e2fac244dd04e4f17393287dd4c6e37c2aab14 Mon Sep 17 00:00:00 2001 From: Henrik Hedelund Date: Sun, 29 Jan 2017 13:21:59 +0100 Subject: [PATCH 1/6] Add config option for user ID tracking --- Helper/Data.php | 16 ++++++++++++++++ etc/adminhtml/system.xml | 16 +++++++++++++++- etc/config.xml | 1 + i18n/en_US.csv | 2 ++ i18n/sv_SE.csv | 2 ++ 5 files changed, 36 insertions(+), 1 deletion(-) diff --git a/Helper/Data.php b/Helper/Data.php index d00122d..1b9bbbe 100644 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -40,6 +40,7 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper const XML_PATH_SITE_ID = 'piwik/tracking/site_id'; const XML_PATH_LINK_ENABLED = 'piwik/tracking/link_enabled'; const XML_PATH_LINK_DELAY = 'piwik/tracking/link_delay'; + const XML_PATH_UID_ENABLED = 'piwik/tracking/uid_enabled'; /** * Check if Piwik is enabled @@ -233,4 +234,19 @@ public function getLinkTrackingDelay($store = null) $store ); } + + /** + * Check if Piwik user ID tracking is enabled + * + * @param null|string|bool|int|Store $store + * @return bool + */ + public function isUserIdTrackingEnabled($store = null) + { + return $this->scopeConfig->isSetFlag( + self::XML_PATH_UID_ENABLED, + \Magento\Store\Model\ScopeInterface::SCOPE_STORE, + $store + ) && $this->isTrackingEnabled($store); + } } diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 84db54b..69510ad 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -107,10 +107,24 @@ validate-digits validate-zero-or-greater + + + Magento\Config\Model\Config\Source\Yesno + Send logged in customers ID to Piwik + + 1 + + diff --git a/etc/config.xml b/etc/config.xml index 2e487d1..c82ca60 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -29,6 +29,7 @@ 1 1 500 + 0 piwik.php piwik.js diff --git a/i18n/en_US.csv b/i18n/en_US.csv index 702209f..2eddcfa 100644 --- a/i18n/en_US.csv +++ b/i18n/en_US.csv @@ -11,6 +11,8 @@ "Enable tracking of outlinks and downloads","Enable tracking of outlinks and downloads" "Link Tracking Timer","Link Tracking Timer" "Delay for link tracking in milliseconds","Delay for link tracking in milliseconds" +"Enable User ID Tracking","Enable User ID Tracking" +"Send logged in customers ID to Piwik","Send logged in customers ID to Piwik" "Advanced Options","Advanced Options" "Javascript Path","Javascript Path" "Path to the Piwik tracker Javascript. Usually ""piwik.js"".","Path to the Piwik tracker Javascript. Usually ""piwik.js""." diff --git a/i18n/sv_SE.csv b/i18n/sv_SE.csv index c2dd210..c36c813 100644 --- a/i18n/sv_SE.csv +++ b/i18n/sv_SE.csv @@ -11,6 +11,8 @@ "Enable tracking of outlinks and downloads","Aktivera spårning av utlänkar och nedladdningar" "Link Tracking Timer","Länkspårningstimer" "Delay for link tracking in milliseconds","Fördröjning av länkspårning i millisekunder" +"Enable User ID Tracking","Aktivera spårning av användar-ID" +"Send logged in customers ID to Piwik","Skicka inloggade kunders ID till Piwik" "Advanced Options","Avancerade inställningar" "Javascript Path","Sökväg till javascript" "Path to the Piwik tracker Javascript. Usually ""piwik.js"".","Sökväg till Piwik-spårarens javascript. Vanligtvis ""piwik.js""." From afe75093445aff94a3dbe8c11d3a51bab5315376 Mon Sep 17 00:00:00 2001 From: Henrik Hedelund Date: Sun, 29 Jan 2017 13:27:29 +0100 Subject: [PATCH 2/6] User ID provider --- Model/UserId/Provider.php | 37 ++++++++++++++++++++++++++++++ Model/UserId/ProviderInterface.php | 37 ++++++++++++++++++++++++++++++ etc/di.xml | 28 ++++++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 Model/UserId/Provider.php create mode 100644 Model/UserId/ProviderInterface.php create mode 100644 etc/di.xml diff --git a/Model/UserId/Provider.php b/Model/UserId/Provider.php new file mode 100644 index 0000000..4fa0741 --- /dev/null +++ b/Model/UserId/Provider.php @@ -0,0 +1,37 @@ +. + */ + +namespace Henhed\Piwik\Model\UserId; + +/** + * User ID provider + * + */ +class Provider implements ProviderInterface +{ + + /** + * {@inheritDoc} + */ + public function getUserId($customerId) + { + return (string) $customerId; + } +} diff --git a/Model/UserId/ProviderInterface.php b/Model/UserId/ProviderInterface.php new file mode 100644 index 0000000..582413d --- /dev/null +++ b/Model/UserId/ProviderInterface.php @@ -0,0 +1,37 @@ +. + */ + +namespace Henhed\Piwik\Model\UserId; + +/** + * User ID provider interface + * + */ +interface ProviderInterface +{ + + /** + * Returns Piwik user ID for given Magento customer ID + * + * @param int $customerId + * @return string + */ + public function getUserId($customerId); +} diff --git a/etc/di.xml b/etc/di.xml new file mode 100644 index 0000000..9a613a4 --- /dev/null +++ b/etc/di.xml @@ -0,0 +1,28 @@ + + + + + + + From 19795900f928dbd635d92448de9164d0b4aafb3d Mon Sep 17 00:00:00 2001 From: Henrik Hedelund Date: Sun, 29 Jan 2017 13:35:12 +0100 Subject: [PATCH 3/6] Add user ID to customer data section --- CustomerData/Customer/CustomerPlugin.php | 89 +++++++++++ .../Customer/CustomerPluginTest.php | 143 ++++++++++++++++++ etc/frontend/di.xml | 8 + 3 files changed, 240 insertions(+) create mode 100644 CustomerData/Customer/CustomerPlugin.php create mode 100644 Test/Unit/CustomerData/Customer/CustomerPluginTest.php diff --git a/CustomerData/Customer/CustomerPlugin.php b/CustomerData/Customer/CustomerPlugin.php new file mode 100644 index 0000000..9c1816f --- /dev/null +++ b/CustomerData/Customer/CustomerPlugin.php @@ -0,0 +1,89 @@ +. + */ + +namespace Henhed\Piwik\CustomerData\Customer; + +/** + * Plugin for \Magento\Customer\CustomerData\Customer + * + */ +class CustomerPlugin +{ + + /** + * Current customer helper + * + * @var \Magento\Customer\Helper\Session\CurrentCustomer $_currentCustomer + */ + protected $_currentCustomer; + + /** + * Piwik data helper + * + * @var \Henhed\Piwik\Helper\Data $_dataHelper + */ + protected $_dataHelper; + + /** + * User ID Provider + * + * @var \Henhed\Piwik\Model\UserId\ProviderInterface $_uidProvider + */ + protected $_uidProvider; + + /** + * Constructor + * + * @param \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer + * @param \Henhed\Piwik\Helper\Data $dataHelper + * @param \Henhed\Piwik\Model\UserId\ProviderInterface $uidProvider + */ + public function __construct( + \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer, + \Henhed\Piwik\Helper\Data $dataHelper, + \Henhed\Piwik\Model\UserId\ProviderInterface $uidProvider + ) { + $this->_currentCustomer = $currentCustomer; + $this->_dataHelper = $dataHelper; + $this->_uidProvider = $uidProvider; + } + + /** + * Add visitor related tracker information to customer section data. + * + * @param \Magento\Customer\CustomerData\Customer $subject + * @param array $result + * @return array + */ + public function afterGetSectionData( + \Magento\Customer\CustomerData\Customer $subject, + $result + ) { + if ($this->_dataHelper->isUserIdTrackingEnabled() + && ($customerId = $this->_currentCustomer->getCustomerId()) + ) { + $userId = (string) $this->_uidProvider->getUserId($customerId); + if ($userId !== '') { + $result['piwikUserId'] = $userId; + } + } + return $result; + } +} diff --git a/Test/Unit/CustomerData/Customer/CustomerPluginTest.php b/Test/Unit/CustomerData/Customer/CustomerPluginTest.php new file mode 100644 index 0000000..31e8d0a --- /dev/null +++ b/Test/Unit/CustomerData/Customer/CustomerPluginTest.php @@ -0,0 +1,143 @@ +. + */ + +namespace Henhed\Piwik\Test\Unit\CustomerData\Customer; + +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; + +/** + * Test for \Henhed\Piwik\CustomerData\Customer\CustomerPlugin + * + */ +class CustomerPluginTest extends \PHPUnit_Framework_TestCase +{ + + /** + * Customer data plugin (test subject) instance + * + * @var \Henhed\Piwik\CustomerData\Customer\CustomerPlugin $_customerPlugin + */ + protected $_customerPlugin; + + /** + * Current customer helper mock object + * + * @var \PHPUnit_Framework_MockObject_MockObject $_currentCustomerMock + */ + protected $_currentCustomerMock; + + /** + * Piwik data helper mock object + * + * @var \PHPUnit_Framework_MockObject_MockObject $_dataHelperMock + */ + protected $_dataHelperMock; + + /** + * Piwik user ID provider mock object + * + * @var \PHPUnit_Framework_MockObject_MockObject $_uidProviderMock + */ + protected $_uidProviderMock; + + /** + * Customer data mock object + * + * @var \PHPUnit_Framework_MockObject_MockObject $_customerDataMock + */ + protected $_customerDataMock; + + /** + * Set up + * + * @return void + */ + public function setUp() + { + $className = 'Henhed\Piwik\CustomerData\Customer\CustomerPlugin'; + $objectManager = new ObjectManager($this); + $args = $objectManager->getConstructArguments($className); + $this->_customerPlugin = $objectManager->getObject($className, $args); + $this->_currentCustomerMock = $args['currentCustomer']; + $this->_dataHelperMock = $args['dataHelper']; + $this->_uidProviderMock = $args['uidProvider']; + $this->_customerDataMock = $this->getMock( + 'Magento\Customer\CustomerData\Customer', [], [], '', false + ); + } + + /** + * Data provider for `testafterGetSectionData' + * + * @return array + */ + public function testafterGetSectionDataDataProvider() + { + return [ + [false, 1, 'UID1'], + [true, null, 'UID2'], + [true, 3, ''], + [true, 4, 'UID4'] + ]; + } + + /** + * Test `afterGetSectionData' + * + * @param boolean $enabled + * @param int $customerId + * @param string $userId + * @return void + * @dataProvider testafterGetSectionDataDataProvider + */ + public function testafterGetSectionData($enabled, $customerId, $userId) + { + $expectedResult = []; + if ($enabled && $customerId && $userId) { + $expectedResult['piwikUserId'] = $userId; + } + + // Enable tracking + $this->_dataHelperMock + ->expects($this->once()) + ->method('isUserIdTrackingEnabled') + ->willReturn($enabled); + + $this->_currentCustomerMock + ->expects($enabled ? $this->once() : $this->never()) + ->method('getCustomerId') + ->willReturn($customerId); + + $this->_uidProviderMock + ->expects($enabled && $customerId ? $this->once() : $this->never()) + ->method('getUserId') + ->with($customerId) + ->willReturn($userId); + + // Assert that result of plugin equals expected result + $this->assertEquals( + $expectedResult, + $this->_customerPlugin->afterGetSectionData( + $this->_customerDataMock, + [] + ) + ); + } +} diff --git a/etc/frontend/di.xml b/etc/frontend/di.xml index dcb878f..8d0a059 100644 --- a/etc/frontend/di.xml +++ b/etc/frontend/di.xml @@ -21,9 +21,17 @@ --> + + + + + + From 4f958e24a9fc6096cba0d874de1b9691c122d870 Mon Sep 17 00:00:00 2001 From: Henrik Hedelund Date: Sun, 29 Jan 2017 13:56:53 +0100 Subject: [PATCH 4/6] Fire 'piwik:beforeTrack' JS event --- view/frontend/web/js/tracker.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/view/frontend/web/js/tracker.js b/view/frontend/web/js/tracker.js index fd13593..aae831a 100644 --- a/view/frontend/web/js/tracker.js +++ b/view/frontend/web/js/tracker.js @@ -182,13 +182,30 @@ define([ * @param {Tracker|undefined} tracker */ function pushAction(action, tracker) { + if (!_.isArray(action) || _.isEmpty(action)) { return; } else if (_.isArray(_.first(action))) { _.each(action, function (subAction) { pushAction(subAction, tracker); }); - } else if (_.isObject(tracker)) { + return; + } + + if (/^track/.test(_.first(action))) { + // Trigger event before tracking + var event = $.Event('piwik:beforeTrack'); + $(exports).triggerHandler(event, [action, tracker]); + if (event.isDefaultPrevented()) { + // Skip tracking if event listener prevented default + return; + } else if (_.isArray(event.result)) { + // Replace track action if event listener returned an array + action = event.result; + } + } + + if (_.isObject(tracker)) { var actionName = action.shift(); if (_.isFunction(tracker[actionName])) { tracker[actionName].apply(tracker, action); From 4aac1e0d9058be46fc6b0eb94d50b009769360f8 Mon Sep 17 00:00:00 2001 From: Henrik Hedelund Date: Sun, 29 Jan 2017 14:20:53 +0100 Subject: [PATCH 5/6] Call 'setUserId' before tracking --- view/frontend/web/js/tracker.js | 35 ++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/view/frontend/web/js/tracker.js b/view/frontend/web/js/tracker.js index aae831a..774ff94 100644 --- a/view/frontend/web/js/tracker.js +++ b/view/frontend/web/js/tracker.js @@ -74,6 +74,20 @@ define([ */ var storage = $.initNamespaceStorage('henhed-piwik').localStorage; + /** + * Cart data access + * + * @type {Object} + */ + var cartObservable = customerData.get('cart'); + + /** + * Customer data access + * + * @type {Object} + */ + var customerObservable = customerData.get('customer'); + /** * Append Piwik tracker script URL to head * @@ -242,6 +256,23 @@ define([ } } + /** + * Event listener for `piwik:beforeTrack'. Adds visitor data to tracker. + * + * @param {jQuery.Event} event + * @param {Array} action + * @param {Tracker|undefined} tracker + * @see \Henhed\Piwik\CustomerData\Customer\CustomerPlugin + */ + function addVisitorDataBeforeTrack(event, action, tracker) { + + var customer = customerObservable(); + + if (_.has(customer, 'piwikUserId')) { + pushAction(['setUserId', customer.piwikUserId], tracker); + } + }; + /** * Initialzie this component with given options * @@ -269,7 +300,9 @@ define([ // Listen for when the Piwik asynchronous tracker is ready exports.piwikAsyncInit = onPiwikLoaded; // Subscribe to cart updates - customerData.get('cart').subscribe(cartUpdated); + cartObservable.subscribe(cartUpdated); + // Listen for track actions to inject visitor data + $(exports).on('piwik:beforeTrack', addVisitorDataBeforeTrack); return { // Public component API From 3cc9128a46ec5c071845bfd2a7864b414aef0a59 Mon Sep 17 00:00:00 2001 From: Henrik Hedelund Date: Sun, 12 Feb 2017 11:15:52 +0100 Subject: [PATCH 6/6] Add config setting for user ID source --- CustomerData/Customer/CustomerPlugin.php | 43 +++++++--- Helper/Data.php | 14 ++-- Model/Config/Source/UserId/Provider.php | 63 +++++++++++++++ .../Customer/CustomerPluginTest.php | 54 ++++++++++--- UserId/Provider/EmailProvider.php | 68 ++++++++++++++++ .../Provider/EntityIdProvider.php | 14 +++- UserId/Provider/Pool.php | 79 +++++++++++++++++++ .../Provider}/ProviderInterface.php | 9 ++- etc/adminhtml/system.xml | 4 +- etc/config.xml | 2 +- etc/di.xml | 10 ++- i18n/en_US.csv | 2 + i18n/sv_SE.csv | 2 + 13 files changed, 327 insertions(+), 37 deletions(-) create mode 100644 Model/Config/Source/UserId/Provider.php create mode 100644 UserId/Provider/EmailProvider.php rename Model/UserId/Provider.php => UserId/Provider/EntityIdProvider.php (78%) create mode 100644 UserId/Provider/Pool.php rename {Model/UserId => UserId/Provider}/ProviderInterface.php (87%) diff --git a/CustomerData/Customer/CustomerPlugin.php b/CustomerData/Customer/CustomerPlugin.php index 9c1816f..206fbfe 100644 --- a/CustomerData/Customer/CustomerPlugin.php +++ b/CustomerData/Customer/CustomerPlugin.php @@ -42,27 +42,52 @@ class CustomerPlugin protected $_dataHelper; /** - * User ID Provider + * User ID provider pool * - * @var \Henhed\Piwik\Model\UserId\ProviderInterface $_uidProvider + * @var \Henhed\Piwik\UserId\Provider\Pool $_uidProviderPool */ - protected $_uidProvider; + protected $_uidProviderPool; /** * Constructor * * @param \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer * @param \Henhed\Piwik\Helper\Data $dataHelper - * @param \Henhed\Piwik\Model\UserId\ProviderInterface $uidProvider + * @param \Henhed\Piwik\UserId\Provider\Pool $uidProviderPool */ public function __construct( \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer, \Henhed\Piwik\Helper\Data $dataHelper, - \Henhed\Piwik\Model\UserId\ProviderInterface $uidProvider + \Henhed\Piwik\UserId\Provider\Pool $uidProviderPool ) { $this->_currentCustomer = $currentCustomer; $this->_dataHelper = $dataHelper; - $this->_uidProvider = $uidProvider; + $this->_uidProviderPool = $uidProviderPool; + } + + /** + * Get configured Piwik User ID provider or NULL + * + * @return \Henhed\Piwik\UserId\Provider\ProviderInterface|null + */ + protected function _getUserIdProvider() + { + $code = $this->_dataHelper->getUserIdProviderCode(); + return $code ? $this->_uidProviderPool->getProviderByCode($code) : null; + } + + /** + * Get Piwik User ID for current customer + * + * @return string + */ + protected function _getUserId() + { + $provider = $this->_getUserIdProvider(); + $customerId = $this->_currentCustomer->getCustomerId(); + return ($provider && $customerId) + ? (string) $provider->getUserId($customerId) + : ''; } /** @@ -76,10 +101,8 @@ public function afterGetSectionData( \Magento\Customer\CustomerData\Customer $subject, $result ) { - if ($this->_dataHelper->isUserIdTrackingEnabled() - && ($customerId = $this->_currentCustomer->getCustomerId()) - ) { - $userId = (string) $this->_uidProvider->getUserId($customerId); + if ($this->_dataHelper->isTrackingEnabled()) { + $userId = $this->_getUserId(); if ($userId !== '') { $result['piwikUserId'] = $userId; } diff --git a/Helper/Data.php b/Helper/Data.php index 1b9bbbe..d192691 100644 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -40,7 +40,7 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper const XML_PATH_SITE_ID = 'piwik/tracking/site_id'; const XML_PATH_LINK_ENABLED = 'piwik/tracking/link_enabled'; const XML_PATH_LINK_DELAY = 'piwik/tracking/link_delay'; - const XML_PATH_UID_ENABLED = 'piwik/tracking/uid_enabled'; + const XML_PATH_UID_PROVIDER = 'piwik/tracking/uid_provider'; /** * Check if Piwik is enabled @@ -236,17 +236,17 @@ public function getLinkTrackingDelay($store = null) } /** - * Check if Piwik user ID tracking is enabled + * Get provider code for Piwik user ID tracking * * @param null|string|bool|int|Store $store - * @return bool + * @return string */ - public function isUserIdTrackingEnabled($store = null) + public function getUserIdProviderCode($store = null) { - return $this->scopeConfig->isSetFlag( - self::XML_PATH_UID_ENABLED, + return $this->scopeConfig->getValue( + self::XML_PATH_UID_PROVIDER, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store - ) && $this->isTrackingEnabled($store); + ); } } diff --git a/Model/Config/Source/UserId/Provider.php b/Model/Config/Source/UserId/Provider.php new file mode 100644 index 0000000..10fef6a --- /dev/null +++ b/Model/Config/Source/UserId/Provider.php @@ -0,0 +1,63 @@ +. + */ + +namespace Henhed\Piwik\Model\Config\Source\UserId; + +/** + * User ID provider config source model + * + */ +class Provider implements \Magento\Framework\Option\ArrayInterface +{ + + /** + * User ID provider pool + * + * @var \Henhed\Piwik\UserId\Provider\Pool $_pool + */ + protected $_pool; + + /** + * Constructor + * + * @param \Henhed\Piwik\UserId\Provider\Pool $pool + */ + public function __construct(\Henhed\Piwik\UserId\Provider\Pool $pool) + { + $this->_pool = $pool; + } + + /** + * Return array of user ID providers as value-label pairs + * + * @return array + */ + public function toOptionArray() + { + $options = [['value' => '', 'label' => __('No')]]; + foreach ($this->_pool->getAllProviders() as $code => $provider) { + $options[] = [ + 'value' => $code, + 'label' => sprintf('%s (%s)', __('Yes'), $provider->getTitle()) + ]; + } + return $options; + } +} diff --git a/Test/Unit/CustomerData/Customer/CustomerPluginTest.php b/Test/Unit/CustomerData/Customer/CustomerPluginTest.php index 31e8d0a..79b0918 100644 --- a/Test/Unit/CustomerData/Customer/CustomerPluginTest.php +++ b/Test/Unit/CustomerData/Customer/CustomerPluginTest.php @@ -50,6 +50,13 @@ class CustomerPluginTest extends \PHPUnit_Framework_TestCase */ protected $_dataHelperMock; + /** + * Piwik user ID provider pool mock object + * + * @var \PHPUnit_Framework_MockObject_MockObject $_uidProviderPoolMock + */ + protected $_uidProviderPoolMock; + /** * Piwik user ID provider mock object * @@ -77,7 +84,11 @@ public function setUp() $this->_customerPlugin = $objectManager->getObject($className, $args); $this->_currentCustomerMock = $args['currentCustomer']; $this->_dataHelperMock = $args['dataHelper']; - $this->_uidProviderMock = $args['uidProvider']; + $this->_uidProviderPoolMock = $args['uidProviderPool']; + $this->_uidProviderMock = $this->getMock( + 'Henhed\Piwik\UserId\Provider\ProviderInterface', + ['getUserId', 'getTitle'], [], '', false + ); $this->_customerDataMock = $this->getMock( 'Magento\Customer\CustomerData\Customer', [], [], '', false ); @@ -91,10 +102,11 @@ public function setUp() public function testafterGetSectionDataDataProvider() { return [ - [false, 1, 'UID1'], - [true, null, 'UID2'], - [true, 3, ''], - [true, 4, 'UID4'] + [false, 1, 'p', 'UID1'], + [true, null, 'p', 'UID2'], + [true, 3, 'p', ''], + [true, 4, null, 'UID4'], + [true, 5, 'p', 'UID5'] ]; } @@ -103,30 +115,50 @@ public function testafterGetSectionDataDataProvider() * * @param boolean $enabled * @param int $customerId + * @param string|null $provider * @param string $userId * @return void * @dataProvider testafterGetSectionDataDataProvider */ - public function testafterGetSectionData($enabled, $customerId, $userId) - { + public function testafterGetSectionData( + $enabled, $customerId, $provider, $userId + ) { $expectedResult = []; - if ($enabled && $customerId && $userId) { + if ($enabled && $customerId && $provider && $userId) { $expectedResult['piwikUserId'] = $userId; } - // Enable tracking $this->_dataHelperMock ->expects($this->once()) - ->method('isUserIdTrackingEnabled') + ->method('isTrackingEnabled') ->willReturn($enabled); + $this->_dataHelperMock + ->expects($enabled ? $this->once() : $this->never()) + ->method('getUserIdProviderCode') + ->willReturn($provider); + $this->_currentCustomerMock ->expects($enabled ? $this->once() : $this->never()) ->method('getCustomerId') ->willReturn($customerId); + $this->_uidProviderPoolMock + ->expects( + ($enabled && $provider) + ? $this->once() + : $this->never() + ) + ->method('getProviderByCode') + ->with($provider) + ->willReturn($this->_uidProviderMock); + $this->_uidProviderMock - ->expects($enabled && $customerId ? $this->once() : $this->never()) + ->expects( + ($enabled && $customerId && $provider) + ? $this->once() + : $this->never() + ) ->method('getUserId') ->with($customerId) ->willReturn($userId); diff --git a/UserId/Provider/EmailProvider.php b/UserId/Provider/EmailProvider.php new file mode 100644 index 0000000..068bce5 --- /dev/null +++ b/UserId/Provider/EmailProvider.php @@ -0,0 +1,68 @@ +. + */ + +namespace Henhed\Piwik\UserId\Provider; + +use Magento\Customer\Api\CustomerRepositoryInterface; + +/** + * Customer email provider + * + */ +class EmailProvider implements ProviderInterface +{ + + /** + * Customer repository + * + * @var CustomerRepositoryInterface $_customerRepository + */ + protected $_customerRepository; + + /** + * Constructor + * + * @param CustomerRepositoryInterface $customerRepository + */ + public function __construct(CustomerRepositoryInterface $customerRepository) + { + $this->_customerRepository = $customerRepository; + } + + /** + * {@inheritDoc} + */ + public function getUserId($customerId) + { + try { + return $this->_customerRepository->getById($customerId)->getEmail(); + } catch (\Exception $e) { + return false; + } + } + + /** + * {@inheritDoc} + */ + public function getTitle() + { + return __('Customer E-mail'); + } +} diff --git a/Model/UserId/Provider.php b/UserId/Provider/EntityIdProvider.php similarity index 78% rename from Model/UserId/Provider.php rename to UserId/Provider/EntityIdProvider.php index 4fa0741..5bce075 100644 --- a/Model/UserId/Provider.php +++ b/UserId/Provider/EntityIdProvider.php @@ -18,13 +18,13 @@ * along with Henhed_Piwik. If not, see . */ -namespace Henhed\Piwik\Model\UserId; +namespace Henhed\Piwik\UserId\Provider; /** - * User ID provider + * Customer entity ID provider * */ -class Provider implements ProviderInterface +class EntityIdProvider implements ProviderInterface { /** @@ -34,4 +34,12 @@ public function getUserId($customerId) { return (string) $customerId; } + + /** + * {@inheritDoc} + */ + public function getTitle() + { + return __('Customer Entity ID'); + } } diff --git a/UserId/Provider/Pool.php b/UserId/Provider/Pool.php new file mode 100644 index 0000000..1e84b86 --- /dev/null +++ b/UserId/Provider/Pool.php @@ -0,0 +1,79 @@ +. + */ + +namespace Henhed\Piwik\UserId\Provider; + +/** + * User ID provider pool + * + */ +class Pool +{ + + /** + * User ID providers + * + * @var ProviderInterface[] $_providers + */ + protected $_providers = []; + + /** + * Constructor + * + * @param ProviderInterface[] $providers + * @throws \LogicException + */ + public function __construct(array $providers = []) + { + foreach ($providers as $code => $provider) { + if ($provider instanceof ProviderInterface) { + $this->_providers[$code] = $provider; + } else { + throw new \LogicException(sprintf( + '%s must implement %s', + get_class($provider), ProviderInterface::class + )); + } + } + } + + /** + * Get User ID provider by code + * + * @param string $code + * @return ProviderInterface|null + */ + public function getProviderByCode($code) + { + return isset($this->_providers[$code]) + ? $this->_providers[$code] + : null; + } + + /** + * Get all User ID providers added to this pool + * + * @return ProviderInterface[] + */ + public function getAllProviders() + { + return $this->_providers; + } +} diff --git a/Model/UserId/ProviderInterface.php b/UserId/Provider/ProviderInterface.php similarity index 87% rename from Model/UserId/ProviderInterface.php rename to UserId/Provider/ProviderInterface.php index 582413d..604279a 100644 --- a/Model/UserId/ProviderInterface.php +++ b/UserId/Provider/ProviderInterface.php @@ -18,7 +18,7 @@ * along with Henhed_Piwik. If not, see . */ -namespace Henhed\Piwik\Model\UserId; +namespace Henhed\Piwik\UserId\Provider; /** * User ID provider interface @@ -34,4 +34,11 @@ interface ProviderInterface * @return string */ public function getUserId($customerId); + + /** + * Get User ID provider title + * + * @return string + */ + public function getTitle(); } diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 69510ad..51f5e59 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -107,7 +107,7 @@ validate-digits validate-zero-or-greater - - Magento\Config\Model\Config\Source\Yesno + Henhed\Piwik\Model\Config\Source\UserId\Provider Send logged in customers ID to Piwik 1 diff --git a/etc/config.xml b/etc/config.xml index c82ca60..e37b7ce 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -29,7 +29,7 @@ 1 1 500 - 0 + piwik.php piwik.js diff --git a/etc/di.xml b/etc/di.xml index 9a613a4..2299d2c 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -22,7 +22,13 @@ - + + + + Henhed\Piwik\UserId\Provider\EntityIdProvider + Henhed\Piwik\UserId\Provider\EmailProvider + + + diff --git a/i18n/en_US.csv b/i18n/en_US.csv index 2eddcfa..ab4e326 100644 --- a/i18n/en_US.csv +++ b/i18n/en_US.csv @@ -12,6 +12,8 @@ "Link Tracking Timer","Link Tracking Timer" "Delay for link tracking in milliseconds","Delay for link tracking in milliseconds" "Enable User ID Tracking","Enable User ID Tracking" +"Customer Entity ID","Customer Entity ID" +"Customer E-mail","Customer E-mail" "Send logged in customers ID to Piwik","Send logged in customers ID to Piwik" "Advanced Options","Advanced Options" "Javascript Path","Javascript Path" diff --git a/i18n/sv_SE.csv b/i18n/sv_SE.csv index c36c813..540e575 100644 --- a/i18n/sv_SE.csv +++ b/i18n/sv_SE.csv @@ -12,6 +12,8 @@ "Link Tracking Timer","Länkspårningstimer" "Delay for link tracking in milliseconds","Fördröjning av länkspårning i millisekunder" "Enable User ID Tracking","Aktivera spårning av användar-ID" +"Customer Entity ID","Kundens entitets-ID" +"Customer E-mail","Kundens E-postadress" "Send logged in customers ID to Piwik","Skicka inloggade kunders ID till Piwik" "Advanced Options","Avancerade inställningar" "Javascript Path","Sökväg till javascript"