Skip to content

Commit

Permalink
Personalization Frontend (algolia#994)
Browse files Browse the repository at this point in the history
  • Loading branch information
bsuravech authored Apr 8, 2020
1 parent 92c04d3 commit 6666813
Show file tree
Hide file tree
Showing 33 changed files with 1,530 additions and 260 deletions.
9 changes: 9 additions & 0 deletions Block/Algolia.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Algolia\AlgoliaSearch\Helper\AlgoliaHelper;
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
use Algolia\AlgoliaSearch\Helper\Configuration\PersonalizationHelper;
use Algolia\AlgoliaSearch\Helper\Data as CoreHelper;
use Algolia\AlgoliaSearch\Helper\Entity\CategoryHelper;
use Algolia\AlgoliaSearch\Helper\Entity\ProductHelper;
Expand Down Expand Up @@ -37,6 +38,7 @@ class Algolia extends Template implements CollectionDataSourceInterface
private $coreHelper;
private $categoryHelper;
private $landingPageHelper;
private $personalizationHelper;
private $checkoutSession;
private $date;

Expand All @@ -57,6 +59,7 @@ public function __construct(
CoreHelper $coreHelper,
CategoryHelper $categoryHelper,
LandingPageHelper $landingPageHelper,
PersonalizationHelper $personalizationHelper,
CheckoutSession $checkoutSession,
DateTime $date,
array $data = []
Expand All @@ -74,6 +77,7 @@ public function __construct(
$this->coreHelper = $coreHelper;
$this->categoryHelper = $categoryHelper;
$this->landingPageHelper = $landingPageHelper;
$this->personalizationHelper = $personalizationHelper;
$this->checkoutSession = $checkoutSession;
$this->date = $date;

Expand Down Expand Up @@ -121,6 +125,11 @@ public function getAlgoliaHelper()
return $this->algoliaHelper;
}

public function getPersonalizationHelper()
{
return $this->personalizationHelper;
}

public function getCurrencySymbol()
{
return $this->currency->getCurrency($this->getCurrencyCode())->getSymbol();
Expand Down
32 changes: 32 additions & 0 deletions Block/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public function getConfiguration()

$algoliaHelper = $this->getAlgoliaHelper();

$persoHelper = $this->getPersonalizationHelper();

$baseUrl = rtrim($this->getBaseUrl(), '/');

$currencyCode = $this->getCurrencyCode();
Expand Down Expand Up @@ -209,6 +211,36 @@ public function getConfiguration()
'addToCartSelector' => $config->getConversionAnalyticsAddToCartSelector(),
'orderedProductIds' => $this->getOrderedProductIds($config, $request),
],
'isPersonalizationEnabled' => $persoHelper->isPersoEnabled(),
'personalization' => [
'enabled' => $persoHelper->isPersoEnabled(),
'viewedEvents' => [
'viewProduct' => [
'eventName' => __('Viewed Product'),
'enabled' => $persoHelper->isViewProductTracked(),
'method' => 'viewedObjectIDs',
],
],
'clickedEvents' => [
'productClicked' => [
'eventName' => __('Product Clicked'),
'enabled' => $persoHelper->isProductClickedTracked(),
'selector' => $persoHelper->getProductClickedSelector(),
'method' => 'clickedObjectIDs',
],
'productRecommended' => [
'eventName' => __('Recommended Product Clicked'),
'enabled' => $persoHelper->isProductRecommendedTracked(),
'selector' => $persoHelper->getProductRecommendedSelector(),
'method' => 'clickedObjectIDs',
],
],
'filterClicked' => [
'eventName' => __('Filter Clicked'),
'enabled' => $persoHelper->isFilterClickedTracked(),
'method' => 'clickedFilters',
],
],
'analytics' => $config->getAnalyticsConfig(),
'now' => $this->getTimestamp(),
'translations' => [
Expand Down
26 changes: 26 additions & 0 deletions Block/System/Form/Field/PersonalizationSelector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Algolia\AlgoliaSearch\Block\System\Form\Field;

class PersonalizationSelector extends \Magento\Config\Block\System\Config\Form\Field
{
/**
* Retrieve label for the inheritance checkbox
*
* @param \Magento\Framework\Data\Form\Element\AbstractElement $element
*
* @return string
*/
protected function _getInheritCheckboxLabel(\Magento\Framework\Data\Form\Element\AbstractElement $element)
{
$checkboxLabel = __('Use default Magento selector');
if ($element->getCanUseDefaultValue()) {
$checkboxLabel = __('Use Default');
}
if ($element->getCanUseWebsiteValue()) {
$checkboxLabel = __('Use Website');
}

return $checkboxLabel;
}
}
110 changes: 109 additions & 1 deletion Helper/Configuration/NoticeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Magento\Framework\Module\Manager as ModuleManager;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\UrlInterface;
use Magento\Framework\View\Asset\Repository as AssetRepository;

class NoticeHelper extends \Magento\Framework\App\Helper\AbstractHelper
{
Expand All @@ -35,6 +36,9 @@ class NoticeHelper extends \Magento\Framework\App\Helper\AbstractHelper
/** @var UrlInterface */
protected $urlBuilder;

/** @var AssetRepository */
protected $assetRepository;

/** @var string[] */
protected $noticeFunctions = [
'getQueueNotice',
Expand All @@ -43,6 +47,16 @@ class NoticeHelper extends \Magento\Framework\App\Helper\AbstractHelper
'getVersionNotice',
'getClickAnalyticsNotice',
'getQueryRulesNotice',
'getPersonalizationNotice',
];

/** @var array[] */
protected $pagesWithoutQueueNotice = [
'algoliasearch_cc_analytics',
'algoliasearch_analytics',
'algoliasearch_personalization',
'algoliasearch_advanced',
'algoliasearch_extra_settings',
];

/** @var array[] */
Expand All @@ -56,7 +70,8 @@ public function __construct(
ObjectManagerInterface $objectManager,
ExtensionNotification $extensionNotification,
JobCollectionFactory $jobCollectionFactory,
UrlInterface $urlBuilder
UrlInterface $urlBuilder,
AssetRepository $assetRepository
) {
$this->configHelper = $configHelper;
$this->proxyHelper = $proxyHelper;
Expand All @@ -65,6 +80,7 @@ public function __construct(
$this->extensionNotification = $extensionNotification;
$this->jobCollectionFactory = $jobCollectionFactory;
$this->urlBuilder = $urlBuilder;
$this->assetRepository = $assetRepository;

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

protected function getQueueNotice()
{
foreach ($this->pagesWithoutQueueNotice as $page) {
if (preg_match('/' . $page . '/', $this->urlBuilder->getCurrentUrl())) {
return;
}
}

$jobCollection = $this->jobCollectionFactory->create();
$size = $jobCollection->getSize();
$maxJobsPerSingleRun = $this->configHelper->getNumberOfJobToRun();
Expand Down Expand Up @@ -237,6 +259,65 @@ protected function getQueryRulesNotice()
];
}

protected function getPersonalizationNotice()
{
if (! preg_match('/algoliasearch_personalization/', $this->urlBuilder->getCurrentUrl())) {
return;
}

$personalizationStatus = $this->getPersonalizationStatus();

// Adding header
$docContent = '<h2 class="algolia-perso-title">Personalization</h2>';

if ($personalizationStatus < 2) {
$docContent .= '<div class="perso-illustration">
<img src="' . $this->assetRepository->getUrl('Algolia_AlgoliaSearch::images/illu-perso.svg') . '"/>
</div>';
}

$docContent .= '<div class="algolia_block icon-documentation algoblue">
<div class="heading"></div>
Personalization brings another level of relevant search results to your customers.<br/>
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>.
</div>';

switch ($personalizationStatus) {
// Activated
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>
After you\'ve collected a reasonable amount of data, Personlization can be applied.';
$icon = 'icon-warning';
break;
// Available but not activated
case 1: $warningContent = 'To start using this feature, please head over the <a href="https://www.algolia.com/dashboard" target="_blank`">Algolia Dashboard</a>,
and make sure you\'ve enabled Personalization in your account, as well as agreed to the terms and conditions of using Personalization.';
$icon = 'icon-warning';
break;
// Not Available
default: $warningContent = 'To get access to this Algolia feature, please <a target="_blank" href="https://www.algolia.com/contact/enterprise/">contact us</a>.';
$icon = 'icon-stars';
break;
}

$docContent .= $this->formatNotice('', $warningContent, $icon);

$this->notices[] = [
'selector' => '.entry-edit',
'method' => 'before',
'message' => $docContent,
];

// Adding footer
$footerContent = '<div class="algolia-perso-footer"><br/><h2>Personlization preferences</h2>
<p>Manage your Personalization further on the <a href="https://www.algolia.com/dashboard" target="_blank`">Algolia Dashboard</a></p></div>';

$this->notices[] = [
'selector' => '#algoliasearch_personalization_personalization_group_personalization_conversion_events_group',
'method' => 'after',
'message' => $footerContent,
];
}

protected function formatNotice($title, $content, $icon = 'icon-warning')
{
return '<div class="algolia_block ' . $icon . '">
Expand All @@ -245,6 +326,33 @@ protected function formatNotice($title, $content, $icon = 'icon-warning')
</div>';
}

/**
* 0 for non available
* 1 for available but not activated
* 2 for activated
*
* @return int
*/
public function getPersonalizationStatus()
{
$info = $this->proxyHelper->getInfo(ProxyHelper::INFO_TYPE_PERSONALIZATION);

$status = 2;

if ($info
&& array_key_exists('personalization', $info)
&& array_key_exists('personalization_enabled_at', $info)) {
if (!$info['personalization']) {
$status = 0;
}
if ($info['personalization_enabled_at'] === null) {
$status = min(1, $status);
}
}

return $status;
}

/** @return bool */
public function isQueryRulesEnabled()
{
Expand Down
Loading

0 comments on commit 6666813

Please sign in to comment.