diff --git a/Block/Checkout/Conversion.php b/Block/Checkout/Conversion.php new file mode 100644 index 000000000..c048d3c55 --- /dev/null +++ b/Block/Checkout/Conversion.php @@ -0,0 +1,74 @@ +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 ''; + } +} diff --git a/Model/Observer/ClickAnalytics/CatalogControllerProductInitBefore.php b/Model/Observer/ClickAnalytics/CatalogControllerProductInitBefore.php new file mode 100644 index 000000000..7bd0dc120 --- /dev/null +++ b/Model/Observer/ClickAnalytics/CatalogControllerProductInitBefore.php @@ -0,0 +1,73 @@ +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)); + } + } +} diff --git a/Model/Observer/ClickAnalytics/CheckoutCartProductAddAfter.php b/Model/Observer/ClickAnalytics/CheckoutCartProductAddAfter.php new file mode 100644 index 000000000..4218746d9 --- /dev/null +++ b/Model/Observer/ClickAnalytics/CheckoutCartProductAddAfter.php @@ -0,0 +1,48 @@ +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')); + } + } +} diff --git a/Model/ResourceModel/Query/Grid/Collection.php b/Model/ResourceModel/Query/Grid/Collection.php index aec716203..63634ff8a 100644 --- a/Model/ResourceModel/Query/Grid/Collection.php +++ b/Model/ResourceModel/Query/Grid/Collection.php @@ -13,7 +13,6 @@ class Collection extends QueryCollection implements SearchResultInterface /** * @param \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory - * @param \Psr\Log\LoggerInterface $logger * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy * @param \Magento\Framework\Event\ManagerInterface $eventManager * @param mixed|null $mainTable diff --git a/Plugin/QuoteItem.php b/Plugin/QuoteItem.php new file mode 100644 index 000000000..21be9257a --- /dev/null +++ b/Plugin/QuoteItem.php @@ -0,0 +1,48 @@ +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; + } +} diff --git a/Setup/UpgradeData.php b/Setup/UpgradeData.php new file mode 100644 index 000000000..74ea22e0e --- /dev/null +++ b/Setup/UpgradeData.php @@ -0,0 +1,68 @@ +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', + ] + ); + } + } +} diff --git a/Setup/UpgradeSchema.php b/Setup/UpgradeSchema.php index c9baa31d0..1b9762248 100644 --- a/Setup/UpgradeSchema.php +++ b/Setup/UpgradeSchema.php @@ -76,7 +76,7 @@ class UpgradeSchema implements UpgradeSchemaInterface 'algoliasearch_queue/queue/number_of_retries' => '3', 'algoliasearch_cc_analytics/cc_analytics_group/enable' => '0', - 'algoliasearch_cc_analytics/cc_analytics_group/is_selector' => '.ais-hits--item a.result, .ais-infinite-hits--item a.result', + 'algoliasearch_cc_analytics/cc_analytics_group/is_selector' => '.ais-Hits-item a.result, .ais-InfiniteHits-item a.result', 'algoliasearch_cc_analytics/cc_analytics_group/enable_conversion_analytics' => 'disabled', 'algoliasearch_cc_analytics/cc_analytics_group/add_to_cart_selector' => '.action.primary.tocart', diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index dc99496e0..fad5c7ccd 100755 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -655,7 +655,7 @@ data-objectid and data-position. + The element must contain attributes data-objectid, data-indexname, data-position, and data-queryid. ]]> diff --git a/etc/di.xml b/etc/di.xml index f9f6c2c1d..f0ea803fb 100755 --- a/etc/di.xml +++ b/etc/di.xml @@ -104,4 +104,9 @@ Algolia\AlgoliaSearch\QueryImageUpload + + + + + diff --git a/etc/frontend/events.xml b/etc/frontend/events.xml index 9cecd3ceb..fda41bf6e 100755 --- a/etc/frontend/events.xml +++ b/etc/frontend/events.xml @@ -6,4 +6,12 @@ + + + + + + + + \ No newline at end of file diff --git a/view/frontend/layout/checkout_onepage_success.xml b/view/frontend/layout/checkout_onepage_success.xml new file mode 100644 index 000000000..63df7951a --- /dev/null +++ b/view/frontend/layout/checkout_onepage_success.xml @@ -0,0 +1,13 @@ + + + + + + + + + diff --git a/view/frontend/templates/autocomplete/product.phtml b/view/frontend/templates/autocomplete/product.phtml index 95f25f302..aa9250fae 100644 --- a/view/frontend/templates/autocomplete/product.phtml +++ b/view/frontend/templates/autocomplete/product.phtml @@ -10,7 +10,10 @@ $tierFormatedVar = 'price' . $priceKey . '_tier_formated' \ No newline at end of file + diff --git a/view/frontend/templates/checkout/conversion.phtml b/view/frontend/templates/checkout/conversion.phtml new file mode 100644 index 000000000..f604b4672 --- /dev/null +++ b/view/frontend/templates/checkout/conversion.phtml @@ -0,0 +1,8 @@ +getOrderItemsConversionJson(); +?> + + \ No newline at end of file diff --git a/view/frontend/templates/instant/hit.phtml b/view/frontend/templates/instant/hit.phtml index 8d894b0c4..e910715e2 100644 --- a/view/frontend/templates/instant/hit.phtml +++ b/view/frontend/templates/instant/hit.phtml @@ -12,7 +12,14 @@ $tierFormatedVar = 'price' . $priceKey . '_tier_formated'