forked from algolia/algoliasearch-magento-2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chores(insights): migration / order conversion tracking (algolia#895)
- Loading branch information
Showing
18 changed files
with
461 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
namespace Algolia\AlgoliaSearch\Block\Checkout; | ||
|
||
use Algolia\AlgoliaSearch\Helper\ConfigHelper; | ||
use Magento\Checkout\Model\Session; | ||
use Magento\Framework\View\Element\Template; | ||
use Magento\Framework\View\Element\Template\Context; | ||
use Magento\Sales\Model\Order\Item; | ||
|
||
class Conversion extends Template | ||
{ | ||
/** | ||
* @var Session | ||
*/ | ||
protected $checkoutSession; | ||
|
||
/** | ||
* @var ConfigHelper | ||
*/ | ||
protected $configHelper; | ||
|
||
/** | ||
* @param Context $context | ||
* @param Session $checkoutSession | ||
* @param ConfigHelper $configHelper | ||
* @param array $data | ||
*/ | ||
public function __construct( | ||
Context $context, | ||
Session $checkoutSession, | ||
ConfigHelper $configHelper, | ||
array $data = [] | ||
) { | ||
parent::__construct($context, $data); | ||
$this->checkoutSession = $checkoutSession; | ||
$this->configHelper = $configHelper; | ||
} | ||
|
||
protected function getOrderItems() | ||
{ | ||
/** @var \Magento\Sales\Model\Order $order */ | ||
$order = $this->checkoutSession->getLastRealOrder(); | ||
|
||
return $order->getAllVisibleItems(); | ||
} | ||
|
||
public function getOrderItemsConversionJson() | ||
{ | ||
$orderItemsData = []; | ||
$orderItems = $this->getOrderItems(); | ||
|
||
/** @var Item $item */ | ||
foreach ($orderItems as $item) { | ||
if ($item->hasData('algoliasearch_query_param')) { | ||
$orderItemsData[$item->getProductId()] = json_decode($item->getData('algoliasearch_query_param')); | ||
} | ||
} | ||
|
||
return json_encode($orderItemsData); | ||
} | ||
|
||
public function toHtml() | ||
{ | ||
$storeId = $this->checkoutSession->getLastRealOrder()->getStoreId(); | ||
if ($this->configHelper->isClickConversionAnalyticsEnabled($storeId) | ||
&& $this->configHelper->getConversionAnalyticsMode($storeId) === 'place_order' | ||
) { | ||
return parent::toHtml(); | ||
} | ||
|
||
return ''; | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
Model/Observer/ClickAnalytics/CatalogControllerProductInitBefore.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
namespace Algolia\AlgoliaSearch\Model\Observer\ClickAnalytics; | ||
|
||
use Algolia\AlgoliaSearch\Helper\ConfigHelper; | ||
use Magento\Checkout\Model\Session as CheckoutSession; | ||
use Magento\Framework\Event\Observer; | ||
use Magento\Framework\Event\ObserverInterface; | ||
|
||
class CatalogControllerProductInitBefore implements ObserverInterface | ||
{ | ||
protected $analyticsParams = [ | ||
'queryID', | ||
'indexName', | ||
'objectID', | ||
]; | ||
|
||
/** @var ConfigHelper */ | ||
private $configHelper; | ||
|
||
/** @var CheckoutSession */ | ||
private $checkoutSession; | ||
|
||
/** | ||
* CatalogControllerProductInitBefore constructor. | ||
* | ||
* @param ConfigHelper $configHelper | ||
* @param CheckoutSession $checkoutSession | ||
*/ | ||
public function __construct( | ||
ConfigHelper $configHelper, | ||
CheckoutSession $checkoutSession | ||
) { | ||
$this->configHelper = $configHelper; | ||
$this->checkoutSession = $checkoutSession; | ||
} | ||
|
||
/** | ||
* @param array $params | ||
* | ||
* @return bool | ||
*/ | ||
protected function hasRequiredParameters($params = []) | ||
{ | ||
foreach ($this->analyticsParams as $requiredParam) { | ||
if (!isset($params[$requiredParam])) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public function execute(Observer $observer) | ||
{ | ||
$controllerAction = $observer->getEvent()->getControllerAction(); | ||
$params = $controllerAction->getRequest()->getParams(); | ||
|
||
$storeID = $this->checkoutSession->getQuote()->getStoreId(); | ||
if ($this->hasRequiredParameters($params) | ||
&& $this->configHelper->isClickConversionAnalyticsEnabled($storeID) | ||
&& $this->configHelper->getConversionAnalyticsMode($storeID) === 'place_order' | ||
) { | ||
$conversionData = [ | ||
'queryID' => $params['queryID'], | ||
'indexName' => $params['indexName'], | ||
'objectID' => $params['objectID'], | ||
]; | ||
|
||
$this->checkoutSession->setData('algoliasearch_query_param', json_encode($conversionData)); | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
Model/Observer/ClickAnalytics/CheckoutCartProductAddAfter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace Algolia\AlgoliaSearch\Model\Observer\ClickAnalytics; | ||
|
||
use Algolia\AlgoliaSearch\Helper\ConfigHelper; | ||
use Magento\Checkout\Model\Session as CheckoutSession; | ||
use Magento\Framework\Event\Observer; | ||
use Magento\Framework\Event\ObserverInterface; | ||
|
||
class CheckoutCartProductAddAfter implements ObserverInterface | ||
{ | ||
/** @var ConfigHelper */ | ||
private $configHelper; | ||
|
||
/** @var CheckoutSession */ | ||
private $checkoutSession; | ||
|
||
/** | ||
* CatalogControllerProductInitBefore constructor. | ||
* | ||
* @param ConfigHelper $configHelper | ||
* @param CheckoutSession $checkoutSession | ||
*/ | ||
public function __construct( | ||
ConfigHelper $configHelper, | ||
CheckoutSession $checkoutSession | ||
) { | ||
$this->configHelper = $configHelper; | ||
$this->checkoutSession = $checkoutSession; | ||
} | ||
|
||
/** | ||
* @param Observer $observer | ||
*/ | ||
public function execute(Observer $observer) | ||
{ | ||
/** @var \Magento\Quote\Model\Quote\Item $quoteItem */ | ||
$quoteItem = $observer->getEvent()->getQuoteItem(); | ||
/** @var \Magento\Catalog\Model\Product $product */ | ||
$product = $observer->getEvent()->getProduct(); | ||
|
||
if ($this->configHelper->isClickConversionAnalyticsEnabled($product->getStoreId()) | ||
&& $this->configHelper->getConversionAnalyticsMode($product->getStoreId()) === 'place_order' | ||
) { | ||
$quoteItem->setData('algoliasearch_query_param', $this->checkoutSession->getData('algoliasearch_query_param')); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace Algolia\AlgoliaSearch\Plugin; | ||
|
||
use Algolia\AlgoliaSearch\Helper\ConfigHelper; | ||
use Magento\Quote\Model\Quote\Item\AbstractItem; | ||
use Magento\Quote\Model\Quote\Item\ToOrderItem; | ||
use Magento\Sales\Api\Data\OrderItemInterface; | ||
|
||
class QuoteItem | ||
{ | ||
/** @var ConfigHelper */ | ||
private $configHelper; | ||
|
||
/** | ||
* QuoteItem constructor. | ||
* | ||
* @param ConfigHelper $configHelper | ||
*/ | ||
public function __construct(ConfigHelper $configHelper) | ||
{ | ||
$this->configHelper = $configHelper; | ||
} | ||
|
||
/** | ||
* @param ToOrderItem $subject | ||
* @param OrderItemInterface $orderItem | ||
* @param AbstractItem $item | ||
* @param array $additional | ||
* | ||
* @return OrderItemInterface | ||
*/ | ||
public function afterConvert( | ||
ToOrderItem $subject, | ||
OrderItemInterface $orderItem, | ||
AbstractItem $item, | ||
$additional = [] | ||
) { | ||
$product = $item->getProduct(); | ||
if ($this->configHelper->isClickConversionAnalyticsEnabled($product->getStoreId()) | ||
&& $this->configHelper->getConversionAnalyticsMode($product->getStoreId()) === 'place_order' | ||
) { | ||
$orderItem->setData('algoliasearch_query_param', $item->getData('algoliasearch_query_param')); | ||
} | ||
|
||
return $orderItem; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
namespace Algolia\AlgoliaSearch\Setup; | ||
|
||
use Magento\Framework\DB\Ddl\Table; | ||
use Magento\Framework\Setup\ModuleContextInterface; | ||
use Magento\Framework\Setup\ModuleDataSetupInterface; | ||
use Magento\Framework\Setup\UpgradeDataInterface; | ||
use Magento\Quote\Setup\QuoteSetupFactory; | ||
use Magento\Sales\Setup\SalesSetupFactory; | ||
|
||
class UpgradeData implements UpgradeDataInterface | ||
{ | ||
/** | ||
* @var QuoteSetupFactory | ||
*/ | ||
private $quoteSetupFactory; | ||
|
||
/** | ||
* @var SalesSetupFactory | ||
*/ | ||
private $salesSetupFactory; | ||
|
||
public function __construct( | ||
QuoteSetupFactory $quoteSetupFactory, | ||
SalesSetupFactory $salesSetupFactory | ||
) { | ||
$this->quoteSetupFactory = $quoteSetupFactory; | ||
$this->salesSetupFactory = $salesSetupFactory; | ||
} | ||
/** | ||
* @param ModuleDataSetupInterface $setup | ||
* @param ModuleContextInterface $context | ||
* | ||
* @throws \Magento\Framework\Exception\LocalizedException | ||
*/ | ||
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) | ||
{ | ||
// beware, this is the version we are upgrading from, not to! | ||
$moduleVersion = $context->getVersion(); | ||
|
||
if (version_compare($moduleVersion, '2.0.0', '<')) { | ||
/** @var \Magento\Quote\Setup\QuoteSetup $quoteSetup */ | ||
$quoteSetup = $this->quoteSetupFactory->create(['resourceName' => 'quote_setup', 'setup' => $setup]); | ||
$quoteSetup->addAttribute( | ||
'quote_item', | ||
'algoliasearch_query_param', | ||
[ | ||
'type' => TABLE::TYPE_TEXT, | ||
'nullable' => true, | ||
'comment' => 'Reference for Algolia analytics order conversion', | ||
] | ||
); | ||
|
||
/** @var \Magento\Sales\Setup\SalesSetup $salesSetup */ | ||
$salesSetup = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]); | ||
$salesSetup->addAttribute( | ||
'order_item', | ||
'algoliasearch_query_param', | ||
[ | ||
'type' => TABLE::TYPE_TEXT, | ||
'nullable' => true, | ||
'comment' => 'Reference for Algolia analytics order conversion', | ||
] | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0"?> | ||
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> | ||
<body> | ||
<referenceContainer name="order.success.additional.info"> | ||
<block ifconfig="algoliasearch_cc_analytics/cc_analytics_group/enable" | ||
class="Algolia\AlgoliaSearch\Block\Checkout\Conversion" | ||
name="algoliasearch.analytics.order.conversion" | ||
template="Algolia_AlgoliaSearch::checkout/conversion.phtml" | ||
after="-"> | ||
</block> | ||
</referenceContainer> | ||
</body> | ||
</page> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.