Skip to content

AbTesting ​

Rasuvaeff\Yii3AbTesting\AbTesting

Class · rasuvaeff/yii3-ab-testing · Source · implements AssignmentResolver

Entry point: resolves subjects into variants and turns those decisions into analytics events.

Event identity, timestamp and the context allow-list are applied here, once, so every delivery path serializes the same fields. Sinks receive a finished event and only transport it.

assign() and is() remain free of side effects — exposure stays an explicit act, because auto-tracking would invent impressions on prefetch, on hidden branches and on repeated calls.

Constructor ​

php
new AbTesting(
    ExperimentProvider $provider,
    AssignmentStrategy $strategy,
    ExposureTracker $exposureTracker = new NullExposureTracker(),
    ConversionTracker $conversionTracker = new NullConversionTracker(),
    Psr\Clock\ClockInterface $clock = new SystemClock(),
    EventIdGenerator $eventIds = new Uuid7EventIdGenerator(),
    AnalyticsContextPolicy $contextPolicy = new AllowListAnalyticsContextPolicy(),
)

Methods ​

assign() ​

php
assign(
    string $experiment,
    string $subjectId,
    ?string $forcedVariant = null,
    ?AssignmentContext $context = null,
): Assignment

resolve() ​

php
resolve(
    string $experiment,
    string $subjectId,
    ?string $forcedVariant = null,
    ?AssignmentContext $context = null,
): Assignment

is() ​

php
is(
    string $experiment,
    string $variant,
    string $subjectId,
    ?string $forcedVariant = null,
): bool

isWithContext() ​

php
isWithContext(
    string $experiment,
    string $variant,
    string $subjectId,
    AssignmentContext $context,
    ?string $forcedVariant = null,
): bool

trackExposure() ​

php
trackExposure(Assignment $assignment): ExposureEvent

Records that the subject saw the variant.

Returns the event so the caller can keep its identity — typically as $event->receipt() stored in a cookie or session, to attribute a conversion that happens in a later request.

trackConversion() ​

php
trackConversion(
    Assignment $assignment,
    string $goal,
    ?ExposureEvent $exposure = null,
): ConversionEvent

Records a conversion for an assignment resolved in this request.

Pass $exposure when the exposure happened in the same request, so the conversion is attributed to it exactly instead of by attribution window.

trackConversionForReceipt() ​

php
trackConversionForReceipt(
    AssignmentReceipt $receipt,
    string $goal,
    ?AssignmentContext $context = null,
): ConversionEvent

Records a conversion for an exposure from an earlier request.

The receipt carries the decision as it was, so a reweight or a salt change between the two requests cannot silently re-attribute the conversion to a different variant. $context describes the conversion request, not the original exposure.

getRegistry() ​

php
getRegistry(): ExperimentRegistry