diff --git a/src/module-elasticsuite-analytics/Block/Adminhtml/Report/CustomerCompanySelector.php b/src/module-elasticsuite-analytics/Block/Adminhtml/Report/CustomerCompanySelector.php index 8f88247bf..13a3fa46a 100644 --- a/src/module-elasticsuite-analytics/Block/Adminhtml/Report/CustomerCompanySelector.php +++ b/src/module-elasticsuite-analytics/Block/Adminhtml/Report/CustomerCompanySelector.php @@ -20,6 +20,7 @@ use Magento\Framework\Module\Manager as ModuleManager; use Magento\Framework\View\Element\Template; use Magento\Framework\View\Element\Template\Context; +use Smile\ElasticsuiteAnalytics\Model\Report\Context as ReportContext; /** * Block used to display customer company selector in reports. @@ -47,6 +48,11 @@ class CustomerCompanySelector extends Template */ protected $searchCriteriaBuilder; + /** + * @var ReportContext + */ + protected $reportContext; + /** * @var \Magento\Company\Api\CompanyRepositoryInterface|null */ @@ -61,6 +67,7 @@ class CustomerCompanySelector extends Template * @param ModuleManager $moduleManager Module manager. * @param ScopeConfigInterface $scopeConfig Scope configuration. * @param SearchCriteriaBuilder $searchCriteriaBuilder The search criteria builder. + * @param ReportContext $reportContext Report context. * @param array $data Additional block data. * @throws LocalizedException */ @@ -69,10 +76,12 @@ public function __construct( ModuleManager $moduleManager, ScopeConfigInterface $scopeConfig, SearchCriteriaBuilder $searchCriteriaBuilder, + ReportContext $reportContext, array $data = [] ) { $this->scopeConfig = $scopeConfig; $this->searchCriteriaBuilder = $searchCriteriaBuilder; + $this->reportContext = $reportContext; // Check if Magento_Company module is enabled before attempting to load the repository. if ($moduleManager->isEnabled('Magento_Company')) { @@ -117,4 +126,14 @@ public function getCompaniesList() return []; } + + /** + * Get customer company ID. + * + * @return mixed + */ + public function getCustomerCompanyId() + { + return $this->reportContext->getCustomerCompanyId(); + } } diff --git a/src/module-elasticsuite-analytics/Block/Adminhtml/Report/CustomerGroupSelector.php b/src/module-elasticsuite-analytics/Block/Adminhtml/Report/CustomerGroupSelector.php index 99170e56d..01612d896 100644 --- a/src/module-elasticsuite-analytics/Block/Adminhtml/Report/CustomerGroupSelector.php +++ b/src/module-elasticsuite-analytics/Block/Adminhtml/Report/CustomerGroupSelector.php @@ -15,6 +15,7 @@ use Magento\Framework\View\Element\Template; use Magento\Customer\Model\ResourceModel\Group\CollectionFactory; +use Smile\ElasticsuiteAnalytics\Model\Report\Context as ReportContext; /** * Block used to display customer group selector in reports. @@ -32,19 +33,27 @@ class CustomerGroupSelector extends Template */ protected $customerGroupCollectionFactory; + /** + * @var ReportContext + */ + protected $reportContext; + /** * CustomerGroupSelector constructor. * * @param Template\Context $context The context of the template. * @param CollectionFactory $customerGroupCollectionFactory Factory for creating customer group collection. + * @param ReportContext $reportContext Report context. * @param array $data Additional block data. */ public function __construct( Template\Context $context, CollectionFactory $customerGroupCollectionFactory, + ReportContext $reportContext, array $data = [] ) { $this->customerGroupCollectionFactory = $customerGroupCollectionFactory; + $this->reportContext = $reportContext; parent::__construct($context, $data); } @@ -57,4 +66,14 @@ public function getCustomerGroups() { return $this->customerGroupCollectionFactory->create()->toOptionArray(); } + + /** + * Get customer group ID. + * + * @return mixed + */ + public function getCurrentCustomerGroupId() + { + return $this->reportContext->getCustomerGroupId(); + } } diff --git a/src/module-elasticsuite-analytics/Model/Search/Usage/Terms/AggregationProvider.php b/src/module-elasticsuite-analytics/Model/Search/Usage/Terms/AggregationProvider.php index 6d4b15c2a..99403f0fc 100644 --- a/src/module-elasticsuite-analytics/Model/Search/Usage/Terms/AggregationProvider.php +++ b/src/module-elasticsuite-analytics/Model/Search/Usage/Terms/AggregationProvider.php @@ -16,6 +16,7 @@ use Smile\ElasticsuiteCore\Search\Request\BucketInterface; use Smile\ElasticsuiteCore\Search\Request\MetricInterface; use Smile\ElasticsuiteAnalytics\Model\Report\AggregationProviderInterface; +use Smile\ElasticsuiteCore\Search\Request\QueryInterface; /** * Default AggregationProvider @@ -84,6 +85,7 @@ public function getAggregation() 'name' => 'search_terms', 'metrics' => $this->getMetrics(), 'pipelines' => $this->getPipelines(), + 'childBuckets' => [$this->getFilteredResultCountMetric()], 'sortOrder' => ['unique_sessions' => 'desc'], 'size' => $this->helper->getMaxSearchTerms(), ]; @@ -105,9 +107,7 @@ protected function getMetrics() $this->metricFactory->create( ['name' => 'unique_visitors', 'field' => 'session.vid', 'type' => MetricInterface::TYPE_CARDINALITY] ), - $this->metricFactory->create( - ['name' => 'result_count', 'field' => 'page.product_list.product_count', 'type' => MetricInterface::TYPE_AVG] - ), + // Metrics result_count moved to a sub-aggregation to ignore filtered search pages. ]; return $metrics; @@ -124,4 +124,38 @@ protected function getPipelines() return $pipelines; } + + /** + * Return aggregation providing the filtered (no filtered search pages) result/product count metrics. + * + * @return BucketInterface + */ + protected function getFilteredResultCountMetric() + { + return $this->aggregationFactory->create( + BucketInterface::TYPE_METRIC, + [ + 'name' => 'result_count', + 'metricType' => MetricInterface::TYPE_AVG, + 'field' => 'page.product_list.product_count', + 'filter' => $this->queryFactory->create( + QueryInterface::TYPE_BOOL, + [ + 'mustNot' => [ + $this->queryFactory->create( + QueryInterface::TYPE_NESTED, + [ + 'path' => 'page.product_list.filters', + 'query' => $this->queryFactory->create( + QueryInterface::TYPE_EXISTS, + ['field' => 'page.product_list.filters'] + ), + ] + ), + ], + ] + ), + ] + ); + } } diff --git a/src/module-elasticsuite-analytics/Model/Search/Usage/Terms/Report.php b/src/module-elasticsuite-analytics/Model/Search/Usage/Terms/Report.php index 4745dac0c..cc903780e 100644 --- a/src/module-elasticsuite-analytics/Model/Search/Usage/Terms/Report.php +++ b/src/module-elasticsuite-analytics/Model/Search/Usage/Terms/Report.php @@ -47,6 +47,7 @@ public function __construct( /** * {@inheritdoc} + * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ protected function processResponse(\Smile\ElasticsuiteCore\Search\Adapter\Elasticsuite\Response\QueryResponse $response) { @@ -63,7 +64,14 @@ protected function processResponse(\Smile\ElasticsuiteCore\Search\Adapter\Elasti 'conversion_rate' => number_format(0, 2), ]; if (array_key_exists('result_count', $value->getMetrics())) { - $data[$searchTerm]['result_count'] = round((float) $value->getMetrics()['result_count'] ?: 0); + $resultCountMetrics = $value->getMetrics()['result_count']; + if (is_array($resultCountMetrics) + && array_key_exists('result_count', $resultCountMetrics) + && array_key_exists('value', $resultCountMetrics['result_count']) + ) { + $resultCountMetrics = $resultCountMetrics['result_count']['value'] ?: 0; + } + $data[$searchTerm]['result_count'] = round((float) $resultCountMetrics ?: 0); } } } diff --git a/src/module-elasticsuite-analytics/Model/Search/Usage/Terms/SpellcheckedTerms/AggregationProvider.php b/src/module-elasticsuite-analytics/Model/Search/Usage/Terms/SpellcheckedTerms/AggregationProvider.php index 773747f3b..38ba420ed 100644 --- a/src/module-elasticsuite-analytics/Model/Search/Usage/Terms/SpellcheckedTerms/AggregationProvider.php +++ b/src/module-elasticsuite-analytics/Model/Search/Usage/Terms/SpellcheckedTerms/AggregationProvider.php @@ -35,7 +35,7 @@ protected function getPipelines() PipelineInterface::TYPE_BUCKET_SELECTOR, [ 'name' => 'result_count_filter', - 'bucketsPath' => ['avg_result_count' => 'result_count'], + 'bucketsPath' => ['avg_result_count' => 'result_count.result_count'], 'script' => 'params.avg_result_count > 0', ] ), diff --git a/src/module-elasticsuite-analytics/view/adminhtml/templates/report/customer_company_selector.phtml b/src/module-elasticsuite-analytics/view/adminhtml/templates/report/customer_company_selector.phtml index b871b4230..fdd977b02 100644 --- a/src/module-elasticsuite-analytics/view/adminhtml/templates/report/customer_company_selector.phtml +++ b/src/module-elasticsuite-analytics/view/adminhtml/templates/report/customer_company_selector.phtml @@ -17,6 +17,8 @@ /** * @var Smile\ElasticsuiteAnalytics\Block\Adminhtml\Report\CustomerCompanySelector $block */ +$baseUrl = $block->getUrl('*/*/*', ['_current' => true, 'company_id' => '__company_id__']); +$companyId = $block->getCustomerCompanyId(); ?> isCompanyEnabled()): ?> getCompaniesList(); ?> @@ -25,7 +27,7 @@ @@ -33,7 +35,9 @@ diff --git a/src/module-elasticsuite-analytics/view/adminhtml/templates/report/customer_group_selector.phtml b/src/module-elasticsuite-analytics/view/adminhtml/templates/report/customer_group_selector.phtml index b004c3bc5..ded0655aa 100644 --- a/src/module-elasticsuite-analytics/view/adminhtml/templates/report/customer_group_selector.phtml +++ b/src/module-elasticsuite-analytics/view/adminhtml/templates/report/customer_group_selector.phtml @@ -18,13 +18,15 @@ * @var Smile\ElasticsuiteAnalytics\Block\Adminhtml\Report\CustomerGroupSelector $block */ $customerGroups = $block->getCustomerGroups(); +$customerGroupId = $block->getCurrentCustomerGroupId(); +$baseUrl = $block->getUrl('*/*/*', ['_current' => true, 'customer_group' => '__customer_group__']); ?>
@@ -32,7 +34,9 @@ $customerGroups = $block->getCustomerGroups(); diff --git a/src/module-elasticsuite-analytics/view/adminhtml/templates/report/date_range_switcher.phtml b/src/module-elasticsuite-analytics/view/adminhtml/templates/report/date_range_switcher.phtml index 5f0310592..e5bc62740 100644 --- a/src/module-elasticsuite-analytics/view/adminhtml/templates/report/date_range_switcher.phtml +++ b/src/module-elasticsuite-analytics/view/adminhtml/templates/report/date_range_switcher.phtml @@ -57,7 +57,7 @@ require(['jquery', 'mage/calendar', 'mage/adminhtml/tools'], function ($) { $('#getJsId('date-range-picker'); ?>').dateRange(getJsConfig(); ?>); $('#getJsId('date-range-picker', 'apply'); ?>').on('click', function() { - var url = "getUrl('*/*/*', ['from' => '__from__', 'to' => '__to__']); ?>" + let url = "getUrl('*/*/*', ['_current' => true, 'from' => '__from__', 'to' => '__to__']); ?>" .replace('__from__', Base64.encode($("#getJsId('date-range-picker', 'from'); ?>")[0].value)) .replace('__to__', Base64.encode($("#getJsId('date-range-picker', 'to'); ?>")[0].value)); window.location = url; diff --git a/src/module-elasticsuite-analytics/view/adminhtml/web/js/report/customer-company-selector.js b/src/module-elasticsuite-analytics/view/adminhtml/web/js/report/customer-company-selector.js index 40c5bb880..aba5e83ed 100644 --- a/src/module-elasticsuite-analytics/view/adminhtml/web/js/report/customer-company-selector.js +++ b/src/module-elasticsuite-analytics/view/adminhtml/web/js/report/customer-company-selector.js @@ -12,31 +12,16 @@ */ define('Smile_ElasticsuiteAnalytics/js/report/customer-company-selector', [ - 'jquery', - 'mage/url' -], function($, urlBuilder) { + 'jquery' +], function($) { 'use strict'; - return function() { - // On document ready, set the selected value in the company dropdown. - $(document).ready(function() { - var urlParams = new URLSearchParams(window.location.search); - var selectedCompany = urlParams.get('company_id'); - - if (selectedCompany) { - $('#company_id').val(selectedCompany); - } - }); - + return function(config) { // Handle the company dropdown value change. $('#company_id').on('change', function() { - var selectedCompany = $(this).val(); - var newUrl = new URL(window.location.href); - - newUrl.searchParams.set('company_id', selectedCompany); + let selectedCompany = $(this).val(); - // Redirect to the new URL with the company filter. - window.location.href = newUrl.href; + window.location = config.baseUrl.replace('__company_id__', selectedCompany); }); }; }); diff --git a/src/module-elasticsuite-analytics/view/adminhtml/web/js/report/customer-group-selector.js b/src/module-elasticsuite-analytics/view/adminhtml/web/js/report/customer-group-selector.js index 54089bc0e..b2b96548f 100644 --- a/src/module-elasticsuite-analytics/view/adminhtml/web/js/report/customer-group-selector.js +++ b/src/module-elasticsuite-analytics/view/adminhtml/web/js/report/customer-group-selector.js @@ -12,31 +12,16 @@ */ define('Smile_ElasticsuiteAnalytics/js/report/customer-group-selector', [ - 'jquery', - 'mage/url' -], function($, urlBuilder) { + 'jquery' +], function($) { 'use strict'; - return function() { - // !On document ready, set the selected value in the customer group dropdown. - $(document).ready(function() { - var urlParams = new URLSearchParams(window.location.search); - var selectedGroup = urlParams.get('customer_group'); - - if (selectedGroup) { - $('#customer_group').val(selectedGroup); - } - }); - + return function(config) { // Handle the customer group dropdown value change. $('#customer_group').on('change', function() { - var selectedGroup = $(this).val(); - var newUrl = new URL(window.location.href); - - newUrl.searchParams.set('customer_group', selectedGroup); + let selectedGroup = $(this).val(); - // Redirect to the new URL with the customer group filter. - window.location.href = newUrl.href; + window.location = config.baseUrl.replace('__customer_group__', selectedGroup); }); }; });