/** * Returns current A/B-test context * * @return array|null */ public static function getContext() { global $USER, $APPLICATION; static $context; if (!defined('SITE_ID') || !SITE_ID) { return null; } if (empty($context)) { $activeTest = Helper::getActiveTest(); if ($USER->canDoOperation('view_other_settings') && !empty($_SESSION['ABTEST_MODE'])) { if ($_SESSION['ABTEST_MODE'] == 'reset') { if (!empty($activeTest)) { $context = Helper::context($activeTest, 'N'); } unset($_SESSION['ABTEST_MODE']); } else { if (preg_match('/^(\\d+)\\|(A|B|N)$/', $_SESSION['ABTEST_MODE'], $matches)) { if (!empty($activeTest) && $activeTest['ID'] == intval($matches[1])) { $context = Helper::context($activeTest, $matches[2]); unset($_SESSION['ABTEST_MODE']); } else { $abtest = ABTestTable::getList(array('filter' => array('=ID' => intval($matches[1]), 'ENABLED' => 'Y')))->fetch(); if (!empty($abtest) && $abtest['SITE_ID'] == SITE_ID) { $context = Helper::context($abtest, $matches[2]); } } } } } if (empty($context) && !empty($activeTest)) { $abtest = $activeTest; if ($cookie = $APPLICATION->get_cookie('ABTEST_' . SITE_ID)) { if (preg_match('/^' . intval($abtest['ID']) . '\\|(A|B|N)$/i', $cookie, $matches)) { $section = $matches[1]; } } if (empty($section)) { $dice = mt_rand(1, 100); if ($dice <= intval($abtest['PORTION']) / 2) { $section = 'A'; } else { if ($dice <= intval($abtest['PORTION'])) { $section = 'B'; } else { $section = 'N'; } } } $context = Helper::context($abtest, $section); } if (empty($activeTest)) { $APPLICATION->set_cookie('ABTEST_' . SITE_ID, null); } else { if ($activeTest['ID'] == $context['abtest']) { $APPLICATION->set_cookie('ABTEST_' . SITE_ID, intval($context['abtest']) . '|' . $context['section']); } } } return $context; }
/** * Sets A/B-test related attributes to conversion context * * @param \Bitrix\Conversion\DayContext $conversionContext Conversion context. * @return void */ public static function onConversionSetContextAttributes(\Bitrix\Conversion\DayContext $conversionContext) { if ($abtest = Helper::getActiveTest()) { if ($context = Helper::getContext()) { if ($context['abtest'] != $abtest['ID']) { return; } if (!in_array($context['section'], array('A', 'B'))) { return; } $conversionContext->setAttribute('abtest', $context['abtest']); $conversionContext->setAttribute('abtest_section', $context['section']); } } }