1. Go to this page and download the library: Download rasuvaeff/yii3-ab-testing library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
rasuvaeff / yii3-ab-testing example snippets
use Rasuvaeff\Yii3AbTesting\ConfigExperimentProvider;
use Rasuvaeff\Yii3AbTesting\AbTesting;
use Rasuvaeff\Yii3AbTesting\WeightedHashAssignmentStrategy;
$provider = new ConfigExperimentProvider(config: [
'checkout-button' => [
'enabled' => true,
'salt' => 'checkout-v1',
'fallbackVariant' => 'control',
'variants' => ['control' => 50, 'green' => 50],
'targeting' => [
'type' => 'environment',
'values' => ['production'],
],
],
]);
$ab = new AbTesting(
provider: $provider,
strategy: new WeightedHashAssignmentStrategy(),
);
$assignment = $ab->assign(experiment: 'checkout-button', subjectId: (string) $userId);
if ($assignment->isVariant('green')) {
// Show green button.
}
// Quick check:
if ($ab->is(experiment: 'checkout-button', variant: 'green', subjectId: (string) $userId)) {
// Variant-specific logic.
}
use Rasuvaeff\Yii3AbTesting\AssignmentSource;
use Rasuvaeff\Yii3AbTesting\DecisionReason;
$assignment->reason === DecisionReason::FallbackTargetingMismatch;
$assignment->source === AssignmentSource::Store;
// Derived checks where the meaning is unambiguous:
$assignment->isForced();
$assignment->isFallback();
$assignment->isTargetingMismatch();
$assignment->isSticky();
// assign() does NOT auto-track. Call explicitly:
$exposure = $ab->trackExposure($assignment);
// On conversion in the same request, link it to the exposure:
$ab->trackConversion($assignment, goal: 'purchase', exposure: $exposure);
$receipt = $exposure->receipt();
$_SESSION['ab_receipt'] = $receipt->toArray(); // or a signed cookie
// … a later request …
$receipt = AssignmentReceipt::fromArray($_SESSION['ab_receipt']);
$ab->trackConversionForReceipt($receipt, goal: 'purchase', context: $context);
use Rasuvaeff\Yii3AbTesting\AllowListAnalyticsContextPolicy;
new AllowListAnalyticsContextPolicy(
allowedAttributes: ['country', 'plan', 'user'],
renamedAttributes: ['country' => 'geo'], // stored column name
redactedAttributes: ['user'], // kept as a column, value masked
);
use Rasuvaeff\Yii3AbTesting\AttributionWindow;
use Rasuvaeff\Yii3AbTesting\RepeatedConversionPolicy;
AttributionWindow::default(); // 7 days
AttributionWindow::ofDays(14);
RepeatedConversionPolicy::FirstOnly; // default: one per subject, goal, revision
RepeatedConversionPolicy::All; // count every conversion (value metrics)
use Rasuvaeff\Yii3AbTesting\ExposureTracker;
use Rasuvaeff\Yii3AbTesting\ConversionTracker;
return [
ExposureTracker::class => MyExposureTracker::class,
ConversionTracker::class => MyConversionTracker::class,
];
use Psr\Log\LoggerInterface;
use Rasuvaeff\Yii3AbTesting\ExposureTracker;
use Rasuvaeff\Yii3AbTesting\LoggerExposureTracker;
return [
ExposureTracker::class => static fn (LoggerInterface $logger): ExposureTracker
=> new LoggerExposureTracker($logger),
];
use Rasuvaeff\Yii3AbTesting\CompositeExposureTracker;
use Rasuvaeff\Yii3AbTesting\ExposureTracker;
return [
ExposureTracker::class => static fn (): ExposureTracker => new CompositeExposureTracker(
new OutboxExposureTracker(/* ... */),
new LoggerExposureTracker(/* ... */),
),
];
use Rasuvaeff\Yii3AbTesting\FlushableTracker;
if ($tracker instanceof FlushableTracker) {
$tracker->flush();
}
use Rasuvaeff\Yii3AbTesting\DeduplicatingExposureTracker;
$tracker = new DeduplicatingExposureTracker(tracker: $realExposureTracker);
$tracker->trackExposure($assignment);
$tracker->trackExposure($assignment); // suppressed in this request
use Rasuvaeff\Yii3AbTesting\AndTargetingRule;
use Rasuvaeff\Yii3AbTesting\AttributeTargetingRule;
use Rasuvaeff\Yii3AbTesting\EnvironmentTargetingRule;
use Rasuvaeff\Yii3AbTesting\Experiment;
use Rasuvaeff\Yii3AbTesting\OrTargetingRule;
$experiment = new Experiment(
name: 'checkout',
enabled: true,
salt: 'checkout-v1',
fallbackVariant: 'control',
variants: ['control' => 50, 'green' => 50],
targeting: new AndTargetingRule(rules: [
new EnvironmentTargetingRule(environments: ['production']),
new AttributeTargetingRule(attribute: 'plan', value: 'pro'),
]),
);
$assignment = $abTesting->assign(
experiment: 'checkout',
subjectId: $userId,
context: new AssignmentContext(environment: 'production', attributes: ['plan' => 'pro']),
);
if ($assignment->isTargetingMismatch()) {
// subject not in target segment — received fallback
}
interface AssignmentStore {
public function get(string $experiment, string $subjectId): ?string;
public function put(string $experiment, string $subjectId, string $variant): void;
}
interface ConfigurationAwareAssignmentStore extends AssignmentStore {
public function getForConfiguration(
string $experiment,
string $subjectId,
?string $configurationId,
): ?string;
public function putForConfiguration(
string $experiment,
string $subjectId,
string $variant,
?string $configurationId,
): void;
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.