Skip to content

Commit

Permalink
Merge pull request Smile-SA#47 from Elastic-Suite/feat-ES-19-ab-testi…
Browse files Browse the repository at this point in the history
…ng-for-optimizers

[Campaign] Apply the current scenario optimizers #ESP-95
  • Loading branch information
rbayet authored Sep 23, 2021
2 parents a44830e + 783c986 commit eadcc84
Show file tree
Hide file tree
Showing 29 changed files with 1,704 additions and 106 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?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\ElasticsuiteAbCampaign
* @author Richard Bayet <richard.bayet@smile.fr>
* @copyright 2021 Smile
* @license Licensed to Smile-SA. All rights reserved. No warranty, explicit or implicit, provided.
* Unauthorized copying of this file, via any medium, is strictly prohibited.
*/


namespace Smile\ElasticsuiteAbCampaign\Api;

/**
* Interface CampaignCookieManagerInterface
*
* @category Smile
* @package Smile\ElasticsuiteAbCampaign
*/

interface CampaignCookieManagerInterface
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?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\ElasticsuiteAbCampaign
* @author Richard Bayet <richard.bayet@smile.fr>
* @copyright 2021 Smile
* @license Licensed to Smile-SA. All rights reserved. No warranty, explicit or implicit, provided.
* Unauthorized copying of this file, via any medium, is strictly prohibited.
*/

namespace Smile\ElasticsuiteAbCampaign\Api;

use Smile\ElasticsuiteAbCampaign\Api\Data\CampaignInterface;

/**
* Campaign service interface.
*
* @category Smile
* @package Smile\ElasticsuiteAbCampaign
*/
interface CampaignServiceInterface
{
const CONTEXT_AB_CAMPAIGN = 'es_ab_campaign';

const NO_SCENARIO = 'N';

/**
* Returns true if there is any active campaign.
*
* @return bool
*/
public function hasActiveCampaigns(): bool;

/**
* Returns the active campaigns if there are some.
*
* @return CampaignInterface[]
*/
public function getActiveCampaigns(): array;

/**
* Returns randomized scenarios identifier for active campaigns.
*
* @return array
*/
public function getCampaignScenarios(): array;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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\ElasticsuiteAbCampaign
* @author Richard Bayet <richard.bayet@smile.fr>
* @copyright 2021 Smile
* @license Licensed to Smile-SA. All rights reserved. No warranty, explicit or implicit, provided.
* Unauthorized copying of this file, via any medium, is strictly prohibited.
*/

namespace Smile\ElasticsuiteAbCampaign\Api;

/**
* Interface CampaignUserInterface
*
* @category Smile
* @package Smile\ElasticsuiteAbCampaign
*/
interface CampaignUserContextInterface
{
/**
* Extract user scenarios context from environment.
*
* @return array
*/
public function getUserScenarios(): array;
}
81 changes: 81 additions & 0 deletions src/module-elasticsuite-ab-campaign/Block/Config.php
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\ElasticsuiteAbCampaign
* @author Richard Bayet <richard.bayet@smile.fr>
* @copyright 2021 Smile
* @license Licensed to Smile-SA. All rights reserved. No warranty, explicit or implicit, provided.
* Unauthorized copying of this file, via any medium, is strictly prohibited.
*/

namespace Smile\ElasticsuiteAbCampaign\Block;

use Smile\ElasticsuiteTracker\Block\Config as TrackerConfig;
use Magento\Framework\UrlInterface;

/**
* Class Config
*
* @category Smile
* @package Smile\ElasticsuiteAbCampaign
* @author Richard Bayet <richard.bayet@smile.fr>
*/
class Config extends TrackerConfig
{
/** @var string get/set Campaign Scenarios */
const BASE_BEACON_URL = 'elasticsuite/campaign/scenarios';

/** @var string Campaign(s) scenario cookie name */
const CAMPAIGNS_COOKIE_NAME = 'AB-CAMPAIGN';

/** @var UrlInterface */
private $urlBuilder;

/**
* Config constructor.
* @param \Magento\Framework\View\Element\Template\Context $context Template context.
* @param \Magento\Framework\Json\Helper\Data $jsonHelper Json helper.
* @param \Smile\ElasticsuiteTracker\Helper\Data $trackerHelper Tracker helper.
* @param \Magento\Framework\UrlInterface $urlBuilder URL Builder.
* @param array $data Additional data.
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Framework\Json\Helper\Data $jsonHelper,
\Smile\ElasticsuiteTracker\Helper\Data $trackerHelper,
\Magento\Framework\UrlInterface $urlBuilder,
array $data = []
) {
parent::__construct($context, $jsonHelper, $trackerHelper, $data);
$this->urlBuilder = $urlBuilder;
}

/**
* Get generic tracker and specific campaigns cookie config.
*
* @return array
*/
public function getCookieConfig()
{
$config = parent::getCookieConfig();

$config['campaigns_cookie_name'] = self::CAMPAIGNS_COOKIE_NAME;

return $config;
}

/**
* Retrieve beacon Url
*
* @return string
*/
public function getBeaconUrl()
{
return $this->urlBuilder->getRouteUrl(self::BASE_BEACON_URL);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?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\ElasticsuiteAbCampaign
* @author Botis <botis@smile.fr>
* @copyright 2021 Smile
* @license Open Software License ("OSL") v. 3.0
*/

namespace Smile\ElasticsuiteAbCampaign\Block\Tracker\Variables\Page;

use Magento\Framework\App\Http\Context as HttpContext;
use Magento\Framework\View\Element\Template;
use Smile\ElasticsuiteAbCampaign\Api\CampaignServiceInterface;
use Smile\ElasticsuiteTracker\Block\Variables\Page\AbstractBlock;

/**
* Campaign variables block for page tracking, exposes all campaign tracking variables.
*
* @category Smile
* @package Smile\ElasticsuiteAbCampaign
* @author Botis <botis@smile.fr>
*/
class Campaign extends AbstractBlock
{
/** @var HttpContext */
private $httpContext;

/**
* Set the default template for page variable blocks
*
* @param Template\Context $context The template context
* @param \Magento\Framework\Json\Helper\Data $jsonHelper The Magento's JSON Helper
* @param \Smile\ElasticsuiteTracker\Helper\Data $trackerHelper The Smile Tracker helper
* @param \Magento\Framework\Registry $registry Magento Core Registry
* @param HttpContext $httpContext HTTP context
* @param array $data The block data
*/
public function __construct(
Template\Context $context,
\Magento\Framework\Json\Helper\Data $jsonHelper,
\Smile\ElasticsuiteTracker\Helper\Data $trackerHelper,
\Magento\Framework\Registry $registry,
HttpContext $httpContext,
array $data = []
) {
parent::__construct($context, $jsonHelper, $trackerHelper, $registry, $data);
$this->httpContext = $httpContext;
}

/**
* Append the campaign data to the tracked variables list.
*
* @return array
*/
public function getVariables(): array
{
$campaignVariables = [];

$scenarios = $this->httpContext->getValue(CampaignServiceInterface::CONTEXT_AB_CAMPAIGN) ?? [];
$itemId = 0;
foreach ($scenarios as $scenario) {
$prefix = 'ab_campaign.' . $itemId;
$campaignVariables[$prefix . '.id'] = $scenario['campaign_id'];
$campaignVariables[$prefix . '.scenario'] = $scenario['scenario_id'];
$itemId++;
}

return $campaignVariables;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?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\ElasticsuiteAbCampaign
* @author Richard Bayet <richard.bayet@smile.fr>
* @copyright 2021 Smile
* @license Licensed to Smile-SA. All rights reserved. No warranty, explicit or implicit, provided.
* Unauthorized copying of this file, via any medium, is strictly prohibited.
*/

namespace Smile\ElasticsuiteAbCampaign\Controller\Campaign;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Http\Context as HttpContext;
use Magento\Framework\Controller\ResultFactory;
use Smile\ElasticsuiteAbCampaign\Api\CampaignServiceInterface;

/**
* Class Scenarios
*
* @category Smile
* @package Smile\ElasticsuiteAbCampaign
* @author Richard Bayet <richard.bayet@smile.fr>
*/
class Scenarios extends Action
{
/** @var HttpContext */
private $httpContext;

/** @var CampaignServiceInterface */
private $campaignService;

/**
* Scenarios constructor.
*
* @param Context $context Context.
* @param HttpContext $httpContext Http context.
* @param CampaignServiceInterface $campaignService Campaigns service.
*/
public function __construct(Context $context, HttpContext $httpContext, CampaignServiceInterface $campaignService)
{
parent::__construct($context);
$this->httpContext = $httpContext;
$this->campaignService = $campaignService;
}

/**
* {@inheritDoc}
*/
public function execute()
{
$scenarios = $this->campaignService->getCampaignScenarios();
$this->httpContext->setValue(CampaignServiceInterface::CONTEXT_AB_CAMPAIGN, $scenarios, []);

return $this->resultFactory->create(ResultFactory::TYPE_JSON)->setData(array_values($scenarios));
}
}
Loading

0 comments on commit eadcc84

Please sign in to comment.