Skip to content

Commit

Permalink
[Analytics][Tracker] Feature Smile-SA#3340, add company_id and custom…
Browse files Browse the repository at this point in the history
…er_group_id in tracking data
  • Loading branch information
vahonc committed Sep 20, 2024
1 parent 586eb5f commit f611c33
Show file tree
Hide file tree
Showing 12 changed files with 495 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer
* versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteAnalytics
* @author Vadym Honcharuk <[email protected]>
* @copyright 2024 Smile
* @license Open Software License ("OSL") v. 3.0
*/
namespace Smile\ElasticsuiteAnalytics\Block\Adminhtml\Report;

use Magento\Framework\View\Element\Template;
use Magento\Customer\Model\ResourceModel\Group\CollectionFactory;

/**
* Block used to display customer group selector in reports.
*
* @SuppressWarnings(PHPMD.CamelCasePropertyName)
*
* @category Smile
* @package Smile\ElasticsuiteAnalytics
* @author Vadym Honcharuk <[email protected]>
*/
class CustomerGroupSelector extends Template
{
/**
* @var CollectionFactory
*/
protected $customerGroupCollectionFactory;

/**
* CustomerGroupSelector constructor.
*
* @param Template\Context $context The context of the template.
* @param CollectionFactory $customerGroupCollectionFactory Factory for creating customer group collection.
* @param array $data Additional block data.
*/
public function __construct(
Template\Context $context,
CollectionFactory $customerGroupCollectionFactory,
array $data = []
) {
$this->customerGroupCollectionFactory = $customerGroupCollectionFactory;
parent::__construct($context, $data);
}

/**
* Get customer groups in an option array format.
*
* @return array
*/
public function getCustomerGroups()
{
return $this->customerGroupCollectionFactory->create()->toOptionArray();
}
}
10 changes: 10 additions & 0 deletions src/module-elasticsuite-analytics/Model/Report/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ public function getStoreId()
return $this->request->getParam('store');
}

/**
* Get customer group ID.
*
* @return mixed
*/
public function getCustomerGroupId()
{
return $this->request->getParam('customer_group');
}

/**
* Get date range.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer
* versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteAnalytics
* @author Vadym Honcharuk <[email protected]>
* @copyright 2024 Smile
* @license Open Software License ("OSL") v. 3.0
*/
namespace Smile\ElasticsuiteAnalytics\Model\Report\Event;

use Smile\ElasticsuiteAnalytics\Model\Report\QueryProviderInterface;
use Smile\ElasticsuiteCore\Search\Request\QueryInterface;
use Smile\ElasticsuiteCore\Search\Request\Query\QueryFactory;
use Smile\ElasticsuiteAnalytics\Model\Report\Context;

/**
* Customer group id filter query provider.
*
* @category Smile
* @package Smile\ElasticsuiteAnalytics
*/
class CustomerGroupIdFilterQueryProvider implements QueryProviderInterface
{
/**
* @var QueryFactory
*/
private $queryFactory;

/**
* @var Context
*/
private $context;

/**
* DateFilterQueryProvider constructor.
*
* @param QueryFactory $queryFactory Query factory.
* @param Context $context Report context.
*/
public function __construct(QueryFactory $queryFactory, Context $context)
{
$this->queryFactory = $queryFactory;
$this->context = $context;
}

/**
* {@inheritDoc}
*/
public function getQuery()
{
// Get customer group ID from the context.
$customerGroupId = $this->context->getCustomerGroupId();

// Check if customer group ID is set and not 'all'.
if ($customerGroupId !== 'all' && $customerGroupId !== null) {
// Return a TERM query for the customer group ID.
return $this->queryFactory->create(
QueryInterface::TYPE_TERM,
[
'field' => 'customer.group_id',
'value' => (int) $customerGroupId,
]
);
}

// If 'all' is selected or no customer group ID is set, return null (no filtering).
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer
* versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteAnalytics
* @author Vadym Honcharuk <[email protected]>
* @copyright 2024 Smile
* @license Open Software License ("OSL") v. 3.0
*/
namespace Smile\ElasticsuiteAnalytics\Model\Report\Session;

use Smile\ElasticsuiteAnalytics\Model\Report\QueryProviderInterface;
use Smile\ElasticsuiteCore\Search\Request\QueryInterface;
use Smile\ElasticsuiteCore\Search\Request\Query\QueryFactory;
use Smile\ElasticsuiteAnalytics\Model\Report\Context;

/**
* Customer group id filter query provider.
*
* @category Smile
* @package Smile\ElasticsuiteAnalytics
*/
class CustomerGroupIdFilterQueryProvider implements QueryProviderInterface
{
/**
* @var QueryFactory
*/
private $queryFactory;

/**
* @var Context
*/
private $context;

/**
* CustomerGroupIdFilterQueryProvider constructor.
*
* @param QueryFactory $queryFactory Query factory.
* @param Context $context Report context.
*/
public function __construct(QueryFactory $queryFactory, Context $context)
{
$this->queryFactory = $queryFactory;
$this->context = $context;
}

/**
* {@inheritDoc}
*/
public function getQuery()
{
// Get customer group ID from the context.
$customerGroupId = $this->context->getCustomerGroupId();

// Check if customer group ID is set and not 'all'.
if ($customerGroupId !== 'all' && $customerGroupId !== null) {
// Return a TERM query for the customer group ID.
return $this->queryFactory->create(
QueryInterface::TYPE_BOOL,
[
'must' => [
$this->queryFactory->create(
QueryInterface::TYPE_TERM,
[
'field' => 'customer_group_id',
'value' => (int) $customerGroupId,
]
),
],
]
);
}

// If 'all' is selected or no customer group ID is set, return null (no filtering).
return null;
}
}
18 changes: 12 additions & 6 deletions src/module-elasticsuite-analytics/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
</argument>
<argument name="queryProviders" xsi:type="array">
<item name="dateFilter" xsi:type="object">Smile\ElasticsuiteAnalytics\Model\Report\Event\DateFilterQueryProvider</item>
<item name="customerGroupIdFilter" xsi:type="object">Smile\ElasticsuiteAnalytics\Model\Report\Event\CustomerGroupIdFilterQueryProvider</item>
</argument>
</arguments>
</virtualType>
Expand All @@ -41,6 +42,7 @@
</argument>
<argument name="queryProviders" xsi:type="array">
<item name="dateFilter" xsi:type="object">Smile\ElasticsuiteAnalytics\Model\Report\Session\DateFilterQueryProvider</item>
<item name="customerGroupIdFilter" xsi:type="object">Smile\ElasticsuiteAnalytics\Model\Report\Session\CustomerGroupIdFilterQueryProvider</item>
</argument>
<argument name="containerName" xsi:type="string">tracking_log_session</argument>
</arguments>
Expand All @@ -51,7 +53,7 @@
<argument name="searchRequestBuilder" xsi:type="object">Smile\ElasticsuiteAnalytics\Model\Search\Usage\Kpi\ConversionRates\SearchRequestBuilder</argument>
</arguments>
</type>

<!-- Popular search terms report config -->
<virtualType name="Smile\ElasticsuiteAnalytics\Model\Search\Usage\Terms\PopularTerms\SearchRequestBuilder" type="Smile\ElasticsuiteAnalytics\Model\Report\SearchRequestBuilder">
<arguments>
Expand All @@ -60,10 +62,11 @@
</argument>
<argument name="queryProviders" xsi:type="array">
<item name="dateFilter" xsi:type="object">Smile\ElasticsuiteAnalytics\Model\Report\Event\DateFilterQueryProvider</item>
<item name="customerGroupIdFilter" xsi:type="object">Smile\ElasticsuiteAnalytics\Model\Report\Event\CustomerGroupIdFilterQueryProvider</item>
</argument>
</arguments>
</virtualType>

<virtualType name="Smile\ElasticsuiteAnalytics\Model\Search\Usage\Terms\PopularTerms\Report" type="Smile\ElasticsuiteAnalytics\Model\Search\Usage\Terms\Report">
<arguments>
<argument name="searchRequestBuilder" xsi:type="object">Smile\ElasticsuiteAnalytics\Model\Search\Usage\Terms\PopularTerms\SearchRequestBuilder</argument>
Expand All @@ -72,7 +75,7 @@
</argument>
</arguments>
</virtualType>

<!-- Spellchecked search terms DI config. -->
<virtualType name="Smile\ElasticsuiteAnalytics\Model\Search\Usage\Terms\SpellcheckedTerms\SearchRequestBuilder" type="Smile\ElasticsuiteAnalytics\Model\Report\SearchRequestBuilder">
<arguments>
Expand All @@ -82,10 +85,11 @@
<argument name="queryProviders" xsi:type="array">
<item name="data" xsi:type="object">Smile\ElasticsuiteAnalytics\Model\Search\Usage\Terms\SpellcheckedTerms\QueryProvider</item>
<item name="dateFilter" xsi:type="object">Smile\ElasticsuiteAnalytics\Model\Report\Event\DateFilterQueryProvider</item>
<item name="customerGroupIdFilter" xsi:type="object">Smile\ElasticsuiteAnalytics\Model\Report\Event\CustomerGroupIdFilterQueryProvider</item>
</argument>
</arguments>
</virtualType>

<virtualType name="Smile\ElasticsuiteAnalytics\Model\Search\Usage\Terms\SpellcheckedTerms\Report" type="Smile\ElasticsuiteAnalytics\Model\Search\Usage\Terms\Report">
<arguments>
<argument name="searchRequestBuilder" xsi:type="object">Smile\ElasticsuiteAnalytics\Model\Search\Usage\Terms\SpellcheckedTerms\SearchRequestBuilder</argument>
Expand All @@ -94,7 +98,7 @@
</argument>
</arguments>
</virtualType>

<!-- No result search terms DI config. -->
<virtualType name="Smile\ElasticsuiteAnalytics\Model\Search\Usage\Terms\NoResultTerms\SearchRequestBuilder" type="Smile\ElasticsuiteAnalytics\Model\Report\SearchRequestBuilder">
<arguments>
Expand All @@ -104,10 +108,11 @@
</argument>
<argument name="queryProviders" xsi:type="array">
<item name="dateFilter" xsi:type="object">Smile\ElasticsuiteAnalytics\Model\Report\Session\DateFilterQueryProvider</item>
<item name="customerGroupIdFilter" xsi:type="object">Smile\ElasticsuiteAnalytics\Model\Report\Session\CustomerGroupIdFilterQueryProvider</item>
</argument>
</arguments>
</virtualType>

<virtualType name="Smile\ElasticsuiteAnalytics\Model\Search\Usage\Terms\NoResultTerms\Report" type="Smile\ElasticsuiteAnalytics\Model\Search\Usage\Terms\Report">
<arguments>
<argument name="searchRequestBuilder" xsi:type="object">Smile\ElasticsuiteAnalytics\Model\Search\Usage\Terms\NoResultTerms\SearchRequestBuilder</argument>
Expand All @@ -122,6 +127,7 @@
<arguments>
<argument name="queryProviders" xsi:type="array">
<item name="dateFilter" xsi:type="object">Smile\ElasticsuiteAnalytics\Model\Report\Session\DateFilterQueryProvider</item>
<item name="customerGroupIdFilter" xsi:type="object">Smile\ElasticsuiteAnalytics\Model\Report\Session\CustomerGroupIdFilterQueryProvider</item>
</argument>
</arguments>
</type>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
<script src="https://www.gstatic.com/charts/loader.js" src_type="url" />
<script src="Smile_ElasticsuiteAnalytics::js/report/customer-group-selector.js" />
</head>
<update handle="styles" />
<body>
Expand All @@ -26,6 +27,9 @@
<argument name="use_confirm" xsi:type="boolean">false</argument>
</arguments>
</block>
<!-- Adding the customer group selector block at the top of the page -->
<block class="Smile\ElasticsuiteAnalytics\Block\Adminhtml\Report\CustomerGroupSelector" name="report.customerGroup.selector" template="Smile_ElasticsuiteAnalytics::report/customer_group_selector.phtml" after="report.store.switcher" />

<block class="Smile\ElasticsuiteAnalytics\Block\Adminhtml\Report\DateRangeSwitcher" name="report.dateRange.switcher"/>
</referenceContainer>
<referenceContainer name="content">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer
* versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteAnalytics
* @author Vadym Honcharuk <[email protected]>
* @copyright 2024 Smile
* @license Open Software License ("OSL") v. 3.0
*/
?>

<?php
/**
* @var Smile\ElasticsuiteAnalytics\Block\Adminhtml\Report\CustomerGroupSelector $block
*/
$customerGroups = $block->getCustomerGroups();
?>
<div class="customer-group-selector">
<label for="customer_group"><?= __('Customer Group:');?></label>
<select id="customer_group" name="customer_group" class="admin__control-select">
<option value="all"><?= __('All Customer Groups');?></option>
<?php foreach ($customerGroups as $group): ?>
<option value="<?php echo $group['value']; ?>"><?php echo $group['label']; ?></option>
<?php endforeach; ?>
</select>
</div>

<script type="text/x-magento-init">
{
"*": {
"Smile_ElasticsuiteAnalytics/js/report/customer-group-selector": {}
}
}
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,17 @@
}
}
}

.customer-group-selector {
color: #41362f;
float: left;
font-size: 1.3rem;
margin-top: .59rem;
margin-left: 4rem;
}

.customer-group-selector label {
margin-right: 1rem;
font-weight: 700;
}
}
Loading

0 comments on commit f611c33

Please sign in to comment.