Skip to content

Commit bd01918

Browse files
Jan Petrdamcou
authored andcommitted
Events tracking (#805)
1 parent 3465a2a commit bd01918

File tree

20 files changed

+649
-33
lines changed

20 files changed

+649
-33
lines changed

Controller/Adminhtml/Query/Save.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Algolia\AlgoliaSearch\Controller\Adminhtml\Query;
44

5+
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
56
use Algolia\AlgoliaSearch\Helper\MerchandisingHelper;
67
use Algolia\AlgoliaSearch\Helper\ProxyHelper;
78
use Algolia\AlgoliaSearch\Model\ImageUploader;
@@ -18,6 +19,11 @@ class Save extends AbstractAction
1819
*/
1920
protected $dataPersistor;
2021

22+
/**
23+
* @var ConfigHelper
24+
*/
25+
protected $configHelper;
26+
2127
/**
2228
* @var ImageUploader
2329
*/
@@ -33,6 +39,7 @@ class Save extends AbstractAction
3339
* @param ProxyHelper $proxyHelper
3440
* @param StoreManagerInterface $storeManager
3541
* @param DataPersistorInterface $dataPersistor
42+
* @param ConfigHelper $configHelper
3643
* @param ImageUploader $imageUploader
3744
*
3845
* @return Save
@@ -45,9 +52,11 @@ public function __construct(
4552
ProxyHelper $proxyHelper,
4653
StoreManagerInterface $storeManager,
4754
DataPersistorInterface $dataPersistor,
55+
ConfigHelper $configHelper,
4856
ImageUploader $imageUploader
4957
) {
5058
$this->dataPersistor = $dataPersistor;
59+
$this->configHelper = $configHelper;
5160
$this->imageUploader = $imageUploader;
5261

5362
parent::__construct(
@@ -103,6 +112,12 @@ public function execute()
103112
$query->setData($data);
104113
$query->setCreatedAt(time());
105114

115+
$storeId = isset($data['store_id']) && $data['store_id'] != 0 ? $data['store_id'] : null;
116+
117+
$this->trackQueryMerchandisingData($query, 'banner_image', 'Uploaded Banner', $storeId);
118+
$this->trackQueryMerchandisingData($query, 'banner_alt', 'Add Alt Text', $storeId);
119+
$this->trackQueryMerchandisingData($query, 'banner_url', 'Add Banner URL', $storeId);
120+
106121
try {
107122
$query->getResource()->save($query);
108123

@@ -194,4 +209,22 @@ private function prepareBannerContent($data)
194209

195210
return $content;
196211
}
212+
213+
/**
214+
* @param string $query
215+
* @param string $attributeCode
216+
* @param string $eventName
217+
* @param int|null $storeId
218+
*/
219+
private function trackQueryMerchandisingData($query, $attributeCode, $eventName, $storeId = null)
220+
{
221+
if (($query->isObjectNew() && $query->getData($attributeCode))
222+
|| $query->getOrigData($attributeCode) !== $query->getData($attributeCode)) {
223+
$this->proxyHelper->trackEvent(
224+
$this->configHelper->getApplicationID($storeId),
225+
$eventName,
226+
['source' => 'magento2.querymerch.edit']
227+
);
228+
}
229+
}
197230
}

Helper/ConfigHelper.php

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -465,13 +465,23 @@ public function isAutocompleteDebugEnabled($storeId = null)
465465
return $this->configInterface->isSetFlag(self::AUTOCOMPLETE_MENU_DEBUG, ScopeInterface::SCOPE_STORE, $storeId);
466466
}
467467

468-
public function getSortingIndices($originalIndexName, $storeId = null, $currentCustomerGroupId = null)
468+
public function getRawSortingValue($storeId = null)
469469
{
470-
$attrs = $this->unserialize($this->configInterface->getValue(
470+
return $this->configInterface->getValue(
471471
self::SORTING_INDICES,
472472
ScopeInterface::SCOPE_STORE,
473473
$storeId
474-
));
474+
);
475+
}
476+
477+
public function getSorting($storeId = null)
478+
{
479+
return $this->unserialize($this->getRawSortingValue($storeId));
480+
}
481+
482+
public function getSortingIndices($originalIndexName, $storeId = null, $currentCustomerGroupId = null)
483+
{
484+
$attrs = $this->getSorting($storeId);
475485

476486
$currency = $this->getCurrencyCode($storeId);
477487
$attributesToAdd = [];
@@ -692,13 +702,28 @@ public function getCategoryCustomRanking($storeId = null)
692702
return [];
693703
}
694704

695-
public function getProductCustomRanking($storeId = null)
705+
/**
706+
* @param int|null $storeId
707+
*
708+
* @return mixed
709+
*/
710+
public function getRawProductCustomRanking($storeId = null)
696711
{
697-
$attrs = $this->unserialize($this->configInterface->getValue(
712+
return $this->configInterface->getValue(
698713
self::PRODUCT_CUSTOM_RANKING,
699714
ScopeInterface::SCOPE_STORE,
700715
$storeId
701-
));
716+
);
717+
}
718+
719+
/**
720+
* @param int|null $storeId
721+
*
722+
* @return array|mixed
723+
*/
724+
public function getProductCustomRanking($storeId = null)
725+
{
726+
$attrs = $this->unserialize($this->getRawProductCustomRanking($storeId));
702727

703728
if (is_array($attrs)) {
704729
return $attrs;

Helper/ProxyHelper.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
class ProxyHelper
66
{
77
const PROXY_URL = 'https://magento-proxy.algolia.com/';
8+
89
const PROXY_URL_PARAM_GET_INFO = 'get-info/';
910
const PROXY_URL_PARAM_POST_DATA = 'hs-push/';
11+
const PROXY_URL_PARAM_TRACK_EVENT = 'event/';
1012

1113
const INFO_TYPE_EXTENSION_SUPPORT = 'extension_support';
1214
const INFO_TYPE_QUERY_RULES = 'query_rules';
@@ -57,6 +59,22 @@ public function getInfo($type)
5759
return $info;
5860
}
5961

62+
/**
63+
* @param string $appId
64+
* @param string $eventName
65+
* @param array $data
66+
*/
67+
public function trackEvent($appId, $eventName, $data)
68+
{
69+
$params = [
70+
'appId' => $appId,
71+
'eventName' => $eventName,
72+
'data' => $data,
73+
];
74+
75+
$this->postRequest($params, self::PROXY_URL_PARAM_TRACK_EVENT);
76+
}
77+
6078
public function getClientConfigurationData()
6179
{
6280
if (!$this->allClientData) {
@@ -75,11 +93,7 @@ public function pushSupportTicket($data)
7593
{
7694
$result = $this->postRequest($data, self::PROXY_URL_PARAM_POST_DATA);
7795

78-
if ($result === 'true') {
79-
return true;
80-
}
81-
82-
return false;
96+
return $result === 'true';
8397
}
8498

8599
/**

Helper/SupportHelper.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ public function processContactForm($data)
6363
'note' => $this->noteBuilder->getNote($data['send_additional_info']),
6464
];
6565

66+
$this->proxyHelper->trackEvent($this->configHelper->getApplicationID(), 'Sent Contact Form', [
67+
'source' => 'magento2.help.contact',
68+
]);
69+
6670
return $this->proxyHelper->pushSupportTicket($messageData);
6771
}
6872

Model/ConfigurationTracker.php

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Model;
4+
5+
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
6+
use Algolia\AlgoliaSearch\Helper\Data;
7+
use Algolia\AlgoliaSearch\Helper\ProxyHelper;
8+
use Algolia\AlgoliaSearch\Model\ResourceModel\LandingPage\Collection as LandingPageCollection;
9+
use Algolia\AlgoliaSearch\Model\ResourceModel\Query\Collection as QueryCollection;
10+
use Algolia\AlgoliaSearch\Setup\UpgradeSchema;
11+
12+
class ConfigurationTracker
13+
{
14+
/** @var Data */
15+
private $proxyHelper;
16+
17+
/** @var ConfigHelper */
18+
private $configHelper;
19+
20+
/** @var QueryCollection */
21+
private $queryCollection;
22+
23+
/** @var LandingPageCollection */
24+
private $landingPageCollection;
25+
26+
/** @var UpgradeSchema */
27+
private $upgradeSchema;
28+
29+
/**
30+
* @param ProxyHelper $proxyHelper
31+
* @param ConfigHelper $configHelper
32+
* @param QueryCollection $queryCollection
33+
* @param LandingPageCollection $landingPageCollection
34+
*/
35+
public function __construct(
36+
ProxyHelper $proxyHelper,
37+
ConfigHelper $configHelper,
38+
QueryCollection $queryCollection,
39+
LandingPageCollection $landingPageCollection,
40+
UpgradeSchema $upgradeSchema
41+
) {
42+
$this->proxyHelper = $proxyHelper;
43+
$this->configHelper = $configHelper;
44+
$this->queryCollection = $queryCollection;
45+
$this->landingPageCollection = $landingPageCollection;
46+
$this->upgradeSchema = $upgradeSchema;
47+
}
48+
49+
/**
50+
* @param int $storeId
51+
*/
52+
public function trackConfiguration($storeId)
53+
{
54+
$this->proxyHelper->trackEvent($this->configHelper->getApplicationID($storeId), 'Configuration saved', [
55+
'source' => 'magento2.saveconfig',
56+
'indexingEnabled' => $this->configHelper->isEnabledBackend($storeId),
57+
'searchEnabled' => $this->configHelper->isEnabledFrontEnd($storeId),
58+
'autocompleteEnabled' => $this->configHelper->isAutoCompleteEnabled($storeId),
59+
'instantsearchEnabled' => $this->configHelper->isInstantEnabled($storeId),
60+
'sortingChanged' => $this->isSortingChanged($storeId),
61+
'rankingChanged' => $this->isCustomRankingChanged($storeId),
62+
'replaceImageByVariantUsed' => $this->configHelper->useAdaptiveImage($storeId),
63+
'indexingQueueEnabled' => $this->configHelper->isQueueActive($storeId),
64+
'synonymsManagementEnabled' => $this->configHelper->isEnabledSynonyms($storeId),
65+
'clickAnalyticsEnabled' => $this->configHelper->isClickConversionAnalyticsEnabled($storeId),
66+
'googleAnalyticsEnabled' => $this->configHelper->isAnalyticsEnabled($storeId),
67+
'customerGroupsEnabled' => $this->configHelper->isCustomerGroupsEnabled($storeId),
68+
'merchangisingQRsCreated' => $this->getCountMerchandisingQueries() > 0,
69+
'noOfMerchandisingQRs' => (int) $this->getCountMerchandisingQueries(),
70+
'landingPageCreated' => $this->getCountLandingPages() > 0,
71+
'noOfLandingPages' => (int) $this->getCountLandingPages(),
72+
'storeId' => $storeId,
73+
]);
74+
}
75+
76+
/**
77+
* @param null $storeId
78+
*
79+
* @return bool
80+
*/
81+
private function isSortingChanged($storeId = null)
82+
{
83+
return $this->configHelper->getRawSortingValue($storeId)
84+
!== $this->getDefaultConfigurationFromPath(ConfigHelper::SORTING_INDICES);
85+
}
86+
87+
/**
88+
* @param null $storeId
89+
*
90+
* @return bool
91+
*/
92+
private function isCustomRankingChanged($storeId = null)
93+
{
94+
return $this->configHelper->getRawProductCustomRanking($storeId)
95+
!== $this->getDefaultConfigurationFromPath(ConfigHelper::PRODUCT_CUSTOM_RANKING);
96+
}
97+
98+
/**
99+
* @return int
100+
*/
101+
private function getCountMerchandisingQueries()
102+
{
103+
return $this->queryCollection->getSize();
104+
}
105+
106+
/**
107+
* @return int
108+
*/
109+
private function getCountLandingPages()
110+
{
111+
return $this->landingPageCollection->getSize();
112+
}
113+
114+
/**
115+
* @param string $path
116+
*
117+
* @return mixed|null
118+
*/
119+
private function getDefaultConfigurationFromPath($path)
120+
{
121+
$config = $this->upgradeSchema->getDefaultConfigData();
122+
if (isset($config[$path]) && $config[$path]) {
123+
return $config[$path];
124+
}
125+
126+
return null;
127+
}
128+
}

Model/Observer/SaveSettings.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Algolia\AlgoliaSearch\Model\Observer;
44

5+
use Algolia\AlgoliaSearch\Model\ConfigurationTracker;
56
use Algolia\AlgoliaSearch\Model\IndicesConfigurator;
67
use AlgoliaSearch\AlgoliaException;
78
use Magento\Framework\Event\Observer;
@@ -16,10 +17,17 @@ class SaveSettings implements ObserverInterface
1617
/** @var IndicesConfigurator */
1718
private $indicesConfigurator;
1819

19-
public function __construct(StoreManagerInterface $storeManager, IndicesConfigurator $indicesConfigurator)
20-
{
20+
/** @var ConfigurationTracker */
21+
private $configurationTracker;
22+
23+
public function __construct(
24+
StoreManagerInterface $storeManager,
25+
IndicesConfigurator $indicesConfigurator,
26+
ConfigurationTracker $configurationTracker
27+
) {
2128
$this->storeManager = $storeManager;
2229
$this->indicesConfigurator = $indicesConfigurator;
30+
$this->configurationTracker = $configurationTracker;
2331
}
2432

2533
/**
@@ -29,10 +37,11 @@ public function __construct(StoreManagerInterface $storeManager, IndicesConfigur
2937
*/
3038
public function execute(Observer $observer)
3139
{
32-
foreach ($this->storeManager->getStores() as $store) {
33-
if ($store->getIsActive()) {
34-
$this->indicesConfigurator->saveConfigurationToAlgolia($store->getId());
35-
}
40+
$storeIds = array_keys($this->storeManager->getStores());
41+
42+
foreach ($storeIds as $storeId) {
43+
$this->indicesConfigurator->saveConfigurationToAlgolia($storeId);
44+
$this->configurationTracker->trackConfiguration($storeId);
3645
}
3746
}
3847
}

Model/Source/Facets.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ protected function getTableData()
6565
if ($this->isQueryRulesEnabled()) {
6666
$config['create_rule'] = [
6767
'label' => 'Create Query rule?',
68-
'values' => ['1' => 'Yes', '2' => 'No'],
68+
'values' => ['2' => 'No', '1' => 'Yes'],
6969
];
7070
}
7171

ViewModel/Adminhtml/Common.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,12 @@ public function __construct(
203203
$this->extensionNotification = $extensionNotification;
204204
}
205205

206+
/** @return string */
207+
public function getApplicationId()
208+
{
209+
return $this->configHelper->getApplicationID();
210+
}
211+
206212
/** @return bool */
207213
public function isQueryRulesEnabled()
208214
{

view/adminhtml/layout/default.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
</head>
66

77
<referenceBlock name="before.body.end">
8-
<block class="Magento\Backend\Block\Template" name="algolia_common" template="Algolia_AlgoliaSearch::common.phtml" >
8+
<block class="Magento\Backend\Block\Template" name="algolia_tracking" template="Algolia_AlgoliaSearch::tracking.phtml" />
9+
<block class="Magento\Backend\Block\Template" name="algolia_common" template="Algolia_AlgoliaSearch::common.phtml">
910
<arguments>
1011
<argument name="view_model" xsi:type="object">Algolia\AlgoliaSearch\ViewModel\Adminhtml\Common</argument>
1112
</arguments>

0 commit comments

Comments
 (0)