Skip to content

Commit 5552aae

Browse files
authored
chores(insights): migration / order conversion tracking (#895)
1 parent e3a2945 commit 5552aae

File tree

18 files changed

+461
-74
lines changed

18 files changed

+461
-74
lines changed

Block/Checkout/Conversion.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Block\Checkout;
4+
5+
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
6+
use Magento\Checkout\Model\Session;
7+
use Magento\Framework\View\Element\Template;
8+
use Magento\Framework\View\Element\Template\Context;
9+
use Magento\Sales\Model\Order\Item;
10+
11+
class Conversion extends Template
12+
{
13+
/**
14+
* @var Session
15+
*/
16+
protected $checkoutSession;
17+
18+
/**
19+
* @var ConfigHelper
20+
*/
21+
protected $configHelper;
22+
23+
/**
24+
* @param Context $context
25+
* @param Session $checkoutSession
26+
* @param ConfigHelper $configHelper
27+
* @param array $data
28+
*/
29+
public function __construct(
30+
Context $context,
31+
Session $checkoutSession,
32+
ConfigHelper $configHelper,
33+
array $data = []
34+
) {
35+
parent::__construct($context, $data);
36+
$this->checkoutSession = $checkoutSession;
37+
$this->configHelper = $configHelper;
38+
}
39+
40+
protected function getOrderItems()
41+
{
42+
/** @var \Magento\Sales\Model\Order $order */
43+
$order = $this->checkoutSession->getLastRealOrder();
44+
45+
return $order->getAllVisibleItems();
46+
}
47+
48+
public function getOrderItemsConversionJson()
49+
{
50+
$orderItemsData = [];
51+
$orderItems = $this->getOrderItems();
52+
53+
/** @var Item $item */
54+
foreach ($orderItems as $item) {
55+
if ($item->hasData('algoliasearch_query_param')) {
56+
$orderItemsData[$item->getProductId()] = json_decode($item->getData('algoliasearch_query_param'));
57+
}
58+
}
59+
60+
return json_encode($orderItemsData);
61+
}
62+
63+
public function toHtml()
64+
{
65+
$storeId = $this->checkoutSession->getLastRealOrder()->getStoreId();
66+
if ($this->configHelper->isClickConversionAnalyticsEnabled($storeId)
67+
&& $this->configHelper->getConversionAnalyticsMode($storeId) === 'place_order'
68+
) {
69+
return parent::toHtml();
70+
}
71+
72+
return '';
73+
}
74+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Model\Observer\ClickAnalytics;
4+
5+
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
6+
use Magento\Checkout\Model\Session as CheckoutSession;
7+
use Magento\Framework\Event\Observer;
8+
use Magento\Framework\Event\ObserverInterface;
9+
10+
class CatalogControllerProductInitBefore implements ObserverInterface
11+
{
12+
protected $analyticsParams = [
13+
'queryID',
14+
'indexName',
15+
'objectID',
16+
];
17+
18+
/** @var ConfigHelper */
19+
private $configHelper;
20+
21+
/** @var CheckoutSession */
22+
private $checkoutSession;
23+
24+
/**
25+
* CatalogControllerProductInitBefore constructor.
26+
*
27+
* @param ConfigHelper $configHelper
28+
* @param CheckoutSession $checkoutSession
29+
*/
30+
public function __construct(
31+
ConfigHelper $configHelper,
32+
CheckoutSession $checkoutSession
33+
) {
34+
$this->configHelper = $configHelper;
35+
$this->checkoutSession = $checkoutSession;
36+
}
37+
38+
/**
39+
* @param array $params
40+
*
41+
* @return bool
42+
*/
43+
protected function hasRequiredParameters($params = [])
44+
{
45+
foreach ($this->analyticsParams as $requiredParam) {
46+
if (!isset($params[$requiredParam])) {
47+
return false;
48+
}
49+
}
50+
51+
return true;
52+
}
53+
54+
public function execute(Observer $observer)
55+
{
56+
$controllerAction = $observer->getEvent()->getControllerAction();
57+
$params = $controllerAction->getRequest()->getParams();
58+
59+
$storeID = $this->checkoutSession->getQuote()->getStoreId();
60+
if ($this->hasRequiredParameters($params)
61+
&& $this->configHelper->isClickConversionAnalyticsEnabled($storeID)
62+
&& $this->configHelper->getConversionAnalyticsMode($storeID) === 'place_order'
63+
) {
64+
$conversionData = [
65+
'queryID' => $params['queryID'],
66+
'indexName' => $params['indexName'],
67+
'objectID' => $params['objectID'],
68+
];
69+
70+
$this->checkoutSession->setData('algoliasearch_query_param', json_encode($conversionData));
71+
}
72+
}
73+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Model\Observer\ClickAnalytics;
4+
5+
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
6+
use Magento\Checkout\Model\Session as CheckoutSession;
7+
use Magento\Framework\Event\Observer;
8+
use Magento\Framework\Event\ObserverInterface;
9+
10+
class CheckoutCartProductAddAfter implements ObserverInterface
11+
{
12+
/** @var ConfigHelper */
13+
private $configHelper;
14+
15+
/** @var CheckoutSession */
16+
private $checkoutSession;
17+
18+
/**
19+
* CatalogControllerProductInitBefore constructor.
20+
*
21+
* @param ConfigHelper $configHelper
22+
* @param CheckoutSession $checkoutSession
23+
*/
24+
public function __construct(
25+
ConfigHelper $configHelper,
26+
CheckoutSession $checkoutSession
27+
) {
28+
$this->configHelper = $configHelper;
29+
$this->checkoutSession = $checkoutSession;
30+
}
31+
32+
/**
33+
* @param Observer $observer
34+
*/
35+
public function execute(Observer $observer)
36+
{
37+
/** @var \Magento\Quote\Model\Quote\Item $quoteItem */
38+
$quoteItem = $observer->getEvent()->getQuoteItem();
39+
/** @var \Magento\Catalog\Model\Product $product */
40+
$product = $observer->getEvent()->getProduct();
41+
42+
if ($this->configHelper->isClickConversionAnalyticsEnabled($product->getStoreId())
43+
&& $this->configHelper->getConversionAnalyticsMode($product->getStoreId()) === 'place_order'
44+
) {
45+
$quoteItem->setData('algoliasearch_query_param', $this->checkoutSession->getData('algoliasearch_query_param'));
46+
}
47+
}
48+
}

Model/ResourceModel/Query/Grid/Collection.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ class Collection extends QueryCollection implements SearchResultInterface
1313

1414
/**
1515
* @param \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory
16-
* @param \Psr\Log\LoggerInterface $logger
1716
* @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
1817
* @param \Magento\Framework\Event\ManagerInterface $eventManager
1918
* @param mixed|null $mainTable

Plugin/QuoteItem.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Plugin;
4+
5+
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
6+
use Magento\Quote\Model\Quote\Item\AbstractItem;
7+
use Magento\Quote\Model\Quote\Item\ToOrderItem;
8+
use Magento\Sales\Api\Data\OrderItemInterface;
9+
10+
class QuoteItem
11+
{
12+
/** @var ConfigHelper */
13+
private $configHelper;
14+
15+
/**
16+
* QuoteItem constructor.
17+
*
18+
* @param ConfigHelper $configHelper
19+
*/
20+
public function __construct(ConfigHelper $configHelper)
21+
{
22+
$this->configHelper = $configHelper;
23+
}
24+
25+
/**
26+
* @param ToOrderItem $subject
27+
* @param OrderItemInterface $orderItem
28+
* @param AbstractItem $item
29+
* @param array $additional
30+
*
31+
* @return OrderItemInterface
32+
*/
33+
public function afterConvert(
34+
ToOrderItem $subject,
35+
OrderItemInterface $orderItem,
36+
AbstractItem $item,
37+
$additional = []
38+
) {
39+
$product = $item->getProduct();
40+
if ($this->configHelper->isClickConversionAnalyticsEnabled($product->getStoreId())
41+
&& $this->configHelper->getConversionAnalyticsMode($product->getStoreId()) === 'place_order'
42+
) {
43+
$orderItem->setData('algoliasearch_query_param', $item->getData('algoliasearch_query_param'));
44+
}
45+
46+
return $orderItem;
47+
}
48+
}

Setup/UpgradeData.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Setup;
4+
5+
use Magento\Framework\DB\Ddl\Table;
6+
use Magento\Framework\Setup\ModuleContextInterface;
7+
use Magento\Framework\Setup\ModuleDataSetupInterface;
8+
use Magento\Framework\Setup\UpgradeDataInterface;
9+
use Magento\Quote\Setup\QuoteSetupFactory;
10+
use Magento\Sales\Setup\SalesSetupFactory;
11+
12+
class UpgradeData implements UpgradeDataInterface
13+
{
14+
/**
15+
* @var QuoteSetupFactory
16+
*/
17+
private $quoteSetupFactory;
18+
19+
/**
20+
* @var SalesSetupFactory
21+
*/
22+
private $salesSetupFactory;
23+
24+
public function __construct(
25+
QuoteSetupFactory $quoteSetupFactory,
26+
SalesSetupFactory $salesSetupFactory
27+
) {
28+
$this->quoteSetupFactory = $quoteSetupFactory;
29+
$this->salesSetupFactory = $salesSetupFactory;
30+
}
31+
/**
32+
* @param ModuleDataSetupInterface $setup
33+
* @param ModuleContextInterface $context
34+
*
35+
* @throws \Magento\Framework\Exception\LocalizedException
36+
*/
37+
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
38+
{
39+
// beware, this is the version we are upgrading from, not to!
40+
$moduleVersion = $context->getVersion();
41+
42+
if (version_compare($moduleVersion, '2.0.0', '<')) {
43+
/** @var \Magento\Quote\Setup\QuoteSetup $quoteSetup */
44+
$quoteSetup = $this->quoteSetupFactory->create(['resourceName' => 'quote_setup', 'setup' => $setup]);
45+
$quoteSetup->addAttribute(
46+
'quote_item',
47+
'algoliasearch_query_param',
48+
[
49+
'type' => TABLE::TYPE_TEXT,
50+
'nullable' => true,
51+
'comment' => 'Reference for Algolia analytics order conversion',
52+
]
53+
);
54+
55+
/** @var \Magento\Sales\Setup\SalesSetup $salesSetup */
56+
$salesSetup = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
57+
$salesSetup->addAttribute(
58+
'order_item',
59+
'algoliasearch_query_param',
60+
[
61+
'type' => TABLE::TYPE_TEXT,
62+
'nullable' => true,
63+
'comment' => 'Reference for Algolia analytics order conversion',
64+
]
65+
);
66+
}
67+
}
68+
}

Setup/UpgradeSchema.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class UpgradeSchema implements UpgradeSchemaInterface
7676
'algoliasearch_queue/queue/number_of_retries' => '3',
7777

7878
'algoliasearch_cc_analytics/cc_analytics_group/enable' => '0',
79-
'algoliasearch_cc_analytics/cc_analytics_group/is_selector' => '.ais-hits--item a.result, .ais-infinite-hits--item a.result',
79+
'algoliasearch_cc_analytics/cc_analytics_group/is_selector' => '.ais-Hits-item a.result, .ais-InfiniteHits-item a.result',
8080
'algoliasearch_cc_analytics/cc_analytics_group/enable_conversion_analytics' => 'disabled',
8181
'algoliasearch_cc_analytics/cc_analytics_group/add_to_cart_selector' => '.action.primary.tocart',
8282

etc/adminhtml/system.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@
655655
<label>DOM selector of a result container on the instant search page</label>
656656
<comment>
657657
<![CDATA[
658-
The element must contain attributes <code>data-objectid</code> and <code>data-position</code>.
658+
The element must contain attributes <code>data-objectid</code>, <code>data-indexname</code>, <code>data-position</code>, and <code>data-queryid</code>.
659659
]]>
660660
</comment>
661661
<depends>

etc/di.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,9 @@
104104
<argument name="imageUploader" xsi:type="object">Algolia\AlgoliaSearch\QueryImageUpload</argument>
105105
</arguments>
106106
</type>
107+
108+
<!-- Click and Conversion Analytics -->
109+
<type name="Magento\Quote\Model\Quote\Item\ToOrderItem">
110+
<plugin name="algoliasearch_query_quote_item_conversion" type="Algolia\AlgoliaSearch\Plugin\QuoteItem"/>
111+
</type>
107112
</config>

etc/frontend/events.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,12 @@
66
<event name="algolia_get_attributes_to_filter">
77
<observer name="algoliasearch_apply_product_permissions" instance="Algolia\AlgoliaSearch\Model\Observer\CatalogPermissions\ApplyProductPermissionsFilter" />
88
</event>
9+
10+
<!-- Click and Conversion Analytics -->
11+
<event name="catalog_controller_product_init_before">
12+
<observer name="algoliasearch_set_parameters_to_session" instance="Algolia\AlgoliaSearch\Model\Observer\ClickAnalytics\CatalogControllerProductInitBefore" />
13+
</event>
14+
<event name="checkout_cart_product_add_after">
15+
<observer name="algoliasearch_add_request_to_quote_item" instance="Algolia\AlgoliaSearch\Model\Observer\ClickAnalytics\CheckoutCartProductAddAfter" />
16+
</event>
917
</config>

0 commit comments

Comments
 (0)