Update
This commit is contained in:
@@ -14,6 +14,7 @@ enum Categories: string
|
||||
case CrmForms = 'crm_forms';
|
||||
case ExternalPictureEditor = 'external_picture_editor';
|
||||
case SiteGeneration = 'site_generation';
|
||||
case BlockEdition = 'block_edition';
|
||||
|
||||
public static function getBySiteType(string $siteType): Categories
|
||||
{
|
||||
|
||||
@@ -3,17 +3,22 @@ declare(strict_types=1);
|
||||
|
||||
namespace Bitrix\Landing\Metrika;
|
||||
|
||||
/**
|
||||
* Enum representing all possible Metrika events.
|
||||
*/
|
||||
enum Events: string
|
||||
{
|
||||
case open = 'open';
|
||||
case save = 'save';
|
||||
case cancel = 'cancel';
|
||||
case start = 'start';
|
||||
case select = 'select';
|
||||
case openStartPage = 'open_start_page';
|
||||
case openSettingsMain = 'open_settings_main';
|
||||
case openMarket = 'open_market';
|
||||
case previewTemplate = 'preview_template';
|
||||
case createTemplate = 'create_template';
|
||||
case createTemplateApi = 'create_template_api';
|
||||
case replaceTemplate = 'replace_template';
|
||||
case openEditor = 'open_editor';
|
||||
case publishSite = 'publish_site';
|
||||
@@ -21,4 +26,10 @@ enum Events: string
|
||||
case addWidget = 'add_widget';
|
||||
case deleteWidget = 'delete_widget';
|
||||
case clickOnButton = 'click_on_button';
|
||||
case dataGeneration = 'data_generation';
|
||||
case textsGeneration = 'texts_generation';
|
||||
case imagesGeneration = 'images_generation';
|
||||
case addFavourite = 'add_favourite';
|
||||
case deleteFavourite = 'delete_favourite';
|
||||
case unknown = 'unknown';
|
||||
}
|
||||
|
||||
@@ -11,8 +11,77 @@ class FieldsDto
|
||||
public ?Sections $section = null,
|
||||
public ?string $subSection = null,
|
||||
public ?string $element = null,
|
||||
/**
|
||||
* @var array|null - array of arrays [position, name, value]
|
||||
* F.e.
|
||||
* [
|
||||
* [1, foo, bar],
|
||||
* [2, baz, bat],
|
||||
* ]
|
||||
*/
|
||||
public ?array $params = null,
|
||||
public ?string $error = null,
|
||||
)
|
||||
{}
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'event' => $this->event,
|
||||
'type' => $this->type,
|
||||
'section' => $this->section,
|
||||
'subSection' => $this->subSection,
|
||||
'element' => $this->element,
|
||||
'params' => $this->params,
|
||||
'error' => $this->error,
|
||||
];
|
||||
}
|
||||
|
||||
public static function fromArray(array $data): self
|
||||
{
|
||||
if (isset($data['event']))
|
||||
{
|
||||
$event = Events::tryFrom($data['event']);
|
||||
}
|
||||
if (isset($data['type']))
|
||||
{
|
||||
$type = Types::tryFrom($data['type']);
|
||||
}
|
||||
if (isset($data['section']))
|
||||
{
|
||||
$section = Sections::tryFrom($data['section']);
|
||||
}
|
||||
$subSection = $data['subSection'] ?? null;
|
||||
$element = $data['element'] ?? null;
|
||||
$params = $data['params'] ?? null;
|
||||
$error = $data['error'] ?? null;
|
||||
|
||||
return new self(
|
||||
$event,
|
||||
$type,
|
||||
$section,
|
||||
$subSection,
|
||||
$element,
|
||||
$params,
|
||||
$error,
|
||||
);
|
||||
}
|
||||
|
||||
public function addFields(?FieldsDto $addFields): self
|
||||
{
|
||||
if ($addFields === null)
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
$this->event = $addFields->event ?? $this->event;
|
||||
$this->type = $addFields->type ?? $this->type;
|
||||
$this->section = $addFields->section ?? $this->section;
|
||||
$this->subSection = $addFields->subSection ?? $this->subSection;
|
||||
$this->element = $addFields->element ?? $this->element;
|
||||
$this->params = $addFields->params ?? $this->params;
|
||||
$this->error = $addFields->error ?? $this->error;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Bitrix\Landing\Metrika;
|
||||
|
||||
use Bitrix\AI\Tuning;
|
||||
use Bitrix\Landing\Connector\Ai;
|
||||
use Bitrix\Main\Loader;
|
||||
|
||||
/**
|
||||
* Service for managing and setting provider parameters in Metrika analytics events.
|
||||
*
|
||||
* This service encapsulates the logic for mapping Metrika events to AI provider codes,
|
||||
* retrieving provider codes from the Tuning manager, and setting provider-related parameters
|
||||
* in Metrika analytics objects.
|
||||
*/
|
||||
class MetrikaProviderParamService
|
||||
{
|
||||
/**
|
||||
* Position of the provider parameter in the Metrika params array.
|
||||
* @var int
|
||||
*/
|
||||
private const PROVIDER_PARAM_POSITION = 2;
|
||||
|
||||
/**
|
||||
* Name of the provider parameter in Metrika.
|
||||
* @var string
|
||||
*/
|
||||
private const PROVIDER_PARAM_NAME = 'provider';
|
||||
|
||||
/**
|
||||
* Tuning manager instance for retrieving provider codes.
|
||||
* @var Tuning\Manager
|
||||
*/
|
||||
private Tuning\Manager $tuningManager;
|
||||
|
||||
/**
|
||||
* MetrikaProviderParamService constructor.
|
||||
*
|
||||
* @param Tuning\Manager|null $tuningManager Optional tuning manager instance. If not provided, a new instance will
|
||||
* be created.
|
||||
*/
|
||||
public function __construct(?Tuning\Manager $tuningManager = null)
|
||||
{
|
||||
if (Loader::includeModule('ai'))
|
||||
{
|
||||
$this->tuningManager = $tuningManager ?? new Tuning\Manager();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets all relevant parameters in the Metrika analytics object for the specified event.
|
||||
* Implements MetrikaParamSetterInterface.
|
||||
*
|
||||
* @param Metrika $metrika Metrika analytics object.
|
||||
* @param Events $event Metrika event.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setParams(Metrika $metrika, Events $event): void
|
||||
{
|
||||
$this->setProviderParam($metrika, $event);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the provider parameter in the given Metrika analytics object for the specified event.
|
||||
*
|
||||
* @param Metrika $metrika Metrika analytics object.
|
||||
* @param Events $event Metrika event.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function setProviderParam(Metrika $metrika, Events $event): void
|
||||
{
|
||||
$providerCode = $this->getProviderCodeByEvent($event);
|
||||
if ($providerCode !== null)
|
||||
{
|
||||
$metrika->setParam(
|
||||
self::PROVIDER_PARAM_POSITION,
|
||||
self::PROVIDER_PARAM_NAME,
|
||||
$providerCode
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the provider code for a given Metrika event.
|
||||
*
|
||||
* @param Events $event Metrika event.
|
||||
*
|
||||
* @return string|null Provider code if found, null otherwise.
|
||||
*/
|
||||
private function getProviderCodeByEvent(Events $event): ?string
|
||||
{
|
||||
$providerSetting = self::getProviderSettingName($event->value);
|
||||
if (!isset($providerSetting))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
$item = $this->tuningManager->getItem($providerSetting);
|
||||
if ($item === null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
$providerCode = $item->getValue();
|
||||
if (empty($providerCode))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return $item->getOptions()[$providerCode] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get setting name for AI tuning
|
||||
* @param string $eventName
|
||||
* @return string|null
|
||||
*/
|
||||
public static function getProviderSettingName(string $eventName): ?string
|
||||
{
|
||||
$event = Events::tryFrom($eventName);
|
||||
|
||||
return match ($event)
|
||||
{
|
||||
Events::dataGeneration,
|
||||
Events::textsGeneration => Ai::TUNING_CODE_SITE_TEXT_PROVIDER,
|
||||
Events::imagesGeneration => Ai::TUNING_CODE_SITE_IMAGE_PROVIDER,
|
||||
default => null,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,13 @@ enum Statuses: string
|
||||
{
|
||||
case Success = 'success';
|
||||
case Error = 'error';
|
||||
case ErrorContentPolicy = 'error_content_policy';
|
||||
case ErrorB24 = 'error_b24';
|
||||
case ErrorProvider = 'error_provider';
|
||||
case ErrorLimitDaily = 'error_limit_daily';
|
||||
case ErrorLimitMonthly = 'error_limit_monthly';
|
||||
case ErrorLimitBaas = 'error_limit_baas';
|
||||
case ErrorMarket = 'error_market';
|
||||
case ErrorTurnedOff = 'error_turnedoff';
|
||||
case UnsupportedBlock = 'unsupported_block';
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user