Skip to content

Commit 6666813

Browse files
authored
Personalization Frontend (#994)
1 parent 92c04d3 commit 6666813

33 files changed

+1530
-260
lines changed

Block/Algolia.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Algolia\AlgoliaSearch\Helper\AlgoliaHelper;
66
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
7+
use Algolia\AlgoliaSearch\Helper\Configuration\PersonalizationHelper;
78
use Algolia\AlgoliaSearch\Helper\Data as CoreHelper;
89
use Algolia\AlgoliaSearch\Helper\Entity\CategoryHelper;
910
use Algolia\AlgoliaSearch\Helper\Entity\ProductHelper;
@@ -37,6 +38,7 @@ class Algolia extends Template implements CollectionDataSourceInterface
3738
private $coreHelper;
3839
private $categoryHelper;
3940
private $landingPageHelper;
41+
private $personalizationHelper;
4042
private $checkoutSession;
4143
private $date;
4244

@@ -57,6 +59,7 @@ public function __construct(
5759
CoreHelper $coreHelper,
5860
CategoryHelper $categoryHelper,
5961
LandingPageHelper $landingPageHelper,
62+
PersonalizationHelper $personalizationHelper,
6063
CheckoutSession $checkoutSession,
6164
DateTime $date,
6265
array $data = []
@@ -74,6 +77,7 @@ public function __construct(
7477
$this->coreHelper = $coreHelper;
7578
$this->categoryHelper = $categoryHelper;
7679
$this->landingPageHelper = $landingPageHelper;
80+
$this->personalizationHelper = $personalizationHelper;
7781
$this->checkoutSession = $checkoutSession;
7882
$this->date = $date;
7983

@@ -121,6 +125,11 @@ public function getAlgoliaHelper()
121125
return $this->algoliaHelper;
122126
}
123127

128+
public function getPersonalizationHelper()
129+
{
130+
return $this->personalizationHelper;
131+
}
132+
124133
public function getCurrencySymbol()
125134
{
126135
return $this->currency->getCurrency($this->getCurrencyCode())->getSymbol();

Block/Configuration.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public function getConfiguration()
4444

4545
$algoliaHelper = $this->getAlgoliaHelper();
4646

47+
$persoHelper = $this->getPersonalizationHelper();
48+
4749
$baseUrl = rtrim($this->getBaseUrl(), '/');
4850

4951
$currencyCode = $this->getCurrencyCode();
@@ -209,6 +211,36 @@ public function getConfiguration()
209211
'addToCartSelector' => $config->getConversionAnalyticsAddToCartSelector(),
210212
'orderedProductIds' => $this->getOrderedProductIds($config, $request),
211213
],
214+
'isPersonalizationEnabled' => $persoHelper->isPersoEnabled(),
215+
'personalization' => [
216+
'enabled' => $persoHelper->isPersoEnabled(),
217+
'viewedEvents' => [
218+
'viewProduct' => [
219+
'eventName' => __('Viewed Product'),
220+
'enabled' => $persoHelper->isViewProductTracked(),
221+
'method' => 'viewedObjectIDs',
222+
],
223+
],
224+
'clickedEvents' => [
225+
'productClicked' => [
226+
'eventName' => __('Product Clicked'),
227+
'enabled' => $persoHelper->isProductClickedTracked(),
228+
'selector' => $persoHelper->getProductClickedSelector(),
229+
'method' => 'clickedObjectIDs',
230+
],
231+
'productRecommended' => [
232+
'eventName' => __('Recommended Product Clicked'),
233+
'enabled' => $persoHelper->isProductRecommendedTracked(),
234+
'selector' => $persoHelper->getProductRecommendedSelector(),
235+
'method' => 'clickedObjectIDs',
236+
],
237+
],
238+
'filterClicked' => [
239+
'eventName' => __('Filter Clicked'),
240+
'enabled' => $persoHelper->isFilterClickedTracked(),
241+
'method' => 'clickedFilters',
242+
],
243+
],
212244
'analytics' => $config->getAnalyticsConfig(),
213245
'now' => $this->getTimestamp(),
214246
'translations' => [
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Block\System\Form\Field;
4+
5+
class PersonalizationSelector extends \Magento\Config\Block\System\Config\Form\Field
6+
{
7+
/**
8+
* Retrieve label for the inheritance checkbox
9+
*
10+
* @param \Magento\Framework\Data\Form\Element\AbstractElement $element
11+
*
12+
* @return string
13+
*/
14+
protected function _getInheritCheckboxLabel(\Magento\Framework\Data\Form\Element\AbstractElement $element)
15+
{
16+
$checkboxLabel = __('Use default Magento selector');
17+
if ($element->getCanUseDefaultValue()) {
18+
$checkboxLabel = __('Use Default');
19+
}
20+
if ($element->getCanUseWebsiteValue()) {
21+
$checkboxLabel = __('Use Website');
22+
}
23+
24+
return $checkboxLabel;
25+
}
26+
}

Helper/Configuration/NoticeHelper.php

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\Framework\Module\Manager as ModuleManager;
1010
use Magento\Framework\ObjectManagerInterface;
1111
use Magento\Framework\UrlInterface;
12+
use Magento\Framework\View\Asset\Repository as AssetRepository;
1213

1314
class NoticeHelper extends \Magento\Framework\App\Helper\AbstractHelper
1415
{
@@ -35,6 +36,9 @@ class NoticeHelper extends \Magento\Framework\App\Helper\AbstractHelper
3536
/** @var UrlInterface */
3637
protected $urlBuilder;
3738

39+
/** @var AssetRepository */
40+
protected $assetRepository;
41+
3842
/** @var string[] */
3943
protected $noticeFunctions = [
4044
'getQueueNotice',
@@ -43,6 +47,16 @@ class NoticeHelper extends \Magento\Framework\App\Helper\AbstractHelper
4347
'getVersionNotice',
4448
'getClickAnalyticsNotice',
4549
'getQueryRulesNotice',
50+
'getPersonalizationNotice',
51+
];
52+
53+
/** @var array[] */
54+
protected $pagesWithoutQueueNotice = [
55+
'algoliasearch_cc_analytics',
56+
'algoliasearch_analytics',
57+
'algoliasearch_personalization',
58+
'algoliasearch_advanced',
59+
'algoliasearch_extra_settings',
4660
];
4761

4862
/** @var array[] */
@@ -56,7 +70,8 @@ public function __construct(
5670
ObjectManagerInterface $objectManager,
5771
ExtensionNotification $extensionNotification,
5872
JobCollectionFactory $jobCollectionFactory,
59-
UrlInterface $urlBuilder
73+
UrlInterface $urlBuilder,
74+
AssetRepository $assetRepository
6075
) {
6176
$this->configHelper = $configHelper;
6277
$this->proxyHelper = $proxyHelper;
@@ -65,6 +80,7 @@ public function __construct(
6580
$this->extensionNotification = $extensionNotification;
6681
$this->jobCollectionFactory = $jobCollectionFactory;
6782
$this->urlBuilder = $urlBuilder;
83+
$this->assetRepository = $assetRepository;
6884

6985
foreach ($this->noticeFunctions as $noticeFunction) {
7086
call_user_func([$this, $noticeFunction]);
@@ -79,6 +95,12 @@ public function getExtensionNotices()
7995

8096
protected function getQueueNotice()
8197
{
98+
foreach ($this->pagesWithoutQueueNotice as $page) {
99+
if (preg_match('/' . $page . '/', $this->urlBuilder->getCurrentUrl())) {
100+
return;
101+
}
102+
}
103+
82104
$jobCollection = $this->jobCollectionFactory->create();
83105
$size = $jobCollection->getSize();
84106
$maxJobsPerSingleRun = $this->configHelper->getNumberOfJobToRun();
@@ -237,6 +259,65 @@ protected function getQueryRulesNotice()
237259
];
238260
}
239261

262+
protected function getPersonalizationNotice()
263+
{
264+
if (! preg_match('/algoliasearch_personalization/', $this->urlBuilder->getCurrentUrl())) {
265+
return;
266+
}
267+
268+
$personalizationStatus = $this->getPersonalizationStatus();
269+
270+
// Adding header
271+
$docContent = '<h2 class="algolia-perso-title">Personalization</h2>';
272+
273+
if ($personalizationStatus < 2) {
274+
$docContent .= '<div class="perso-illustration">
275+
<img src="' . $this->assetRepository->getUrl('Algolia_AlgoliaSearch::images/illu-perso.svg') . '"/>
276+
</div>';
277+
}
278+
279+
$docContent .= '<div class="algolia_block icon-documentation algoblue">
280+
<div class="heading"></div>
281+
Personalization brings another level of relevant search results to your customers.<br/>
282+
Find out more in our <a href="https://www.algolia.com/doc/guides/getting-insights-and-analytics/personalization/what-is-personalization/" target="_blank`">Documentation</a>.
283+
</div>';
284+
285+
switch ($personalizationStatus) {
286+
// Activated
287+
case 2: $warningContent = 'Personalization is based on actions a user has performed in the past. We help you collect some of the data automatically.</br>
288+
After you\'ve collected a reasonable amount of data, Personlization can be applied.';
289+
$icon = 'icon-warning';
290+
break;
291+
// Available but not activated
292+
case 1: $warningContent = 'To start using this feature, please head over the <a href="https://www.algolia.com/dashboard" target="_blank`">Algolia Dashboard</a>,
293+
and make sure you\'ve enabled Personalization in your account, as well as agreed to the terms and conditions of using Personalization.';
294+
$icon = 'icon-warning';
295+
break;
296+
// Not Available
297+
default: $warningContent = 'To get access to this Algolia feature, please <a target="_blank" href="https://www.algolia.com/contact/enterprise/">contact us</a>.';
298+
$icon = 'icon-stars';
299+
break;
300+
}
301+
302+
$docContent .= $this->formatNotice('', $warningContent, $icon);
303+
304+
$this->notices[] = [
305+
'selector' => '.entry-edit',
306+
'method' => 'before',
307+
'message' => $docContent,
308+
];
309+
310+
// Adding footer
311+
$footerContent = '<div class="algolia-perso-footer"><br/><h2>Personlization preferences</h2>
312+
<p>Manage your Personalization further on the <a href="https://www.algolia.com/dashboard" target="_blank`">Algolia Dashboard</a></p></div>';
313+
314+
$this->notices[] = [
315+
'selector' => '#algoliasearch_personalization_personalization_group_personalization_conversion_events_group',
316+
'method' => 'after',
317+
'message' => $footerContent,
318+
];
319+
}
320+
240321
protected function formatNotice($title, $content, $icon = 'icon-warning')
241322
{
242323
return '<div class="algolia_block ' . $icon . '">
@@ -245,6 +326,33 @@ protected function formatNotice($title, $content, $icon = 'icon-warning')
245326
</div>';
246327
}
247328

329+
/**
330+
* 0 for non available
331+
* 1 for available but not activated
332+
* 2 for activated
333+
*
334+
* @return int
335+
*/
336+
public function getPersonalizationStatus()
337+
{
338+
$info = $this->proxyHelper->getInfo(ProxyHelper::INFO_TYPE_PERSONALIZATION);
339+
340+
$status = 2;
341+
342+
if ($info
343+
&& array_key_exists('personalization', $info)
344+
&& array_key_exists('personalization_enabled_at', $info)) {
345+
if (!$info['personalization']) {
346+
$status = 0;
347+
}
348+
if ($info['personalization_enabled_at'] === null) {
349+
$status = min(1, $status);
350+
}
351+
}
352+
353+
return $status;
354+
}
355+
248356
/** @return bool */
249357
public function isQueryRulesEnabled()
250358
{

0 commit comments

Comments
 (0)