Running Experiences

Run A/B tests and get variation assignments for visitors

Running an experience evaluates a visitor against the experience's targeting rules (audiences and locations) and deterministically assigns them to a variation. The SDK uses the visitor's unique ID and a MurmurHash-based bucketing algorithm to ensure the same visitor always sees the same variation, as long as the experience configuration has not changed.

Once bucketed, the returned BucketedVariation object tells you which variation the visitor was assigned to and what changes (feature flags, variables) are associated with it.

Run a Single Experience

Evaluate a single experience by its unique key. Returns the bucketed variation if the visitor qualifies, or null/undefined if they do not.

const variation: BucketedVariation = userContext.runExperience('experience-key');

// With attributes:
const variation: BucketedVariation = userContext.runExperience('experience-key', {
  locationProperties: { url: '/' },
  visitorProperties: { country: 'US' }
});
use ConvertSdk\DTO\BucketedVariation;
use OpenAPI\Client\BucketingAttributes;

/** @var BucketedVariation|null $variation */
$variation = $context->runExperience('experience-key');

// With attributes:
$variation = $context->runExperience('experience-key', new BucketingAttributes([
    'locationProperties' => ['url' => '/'],
    'visitorProperties' => ['country' => 'US'],
]));

Parameters:

ParameterTypeRequiredDescription
experienceKeystringYesThe experience's unique key
attributesobjectNoSee BucketingAttributes below

Returns: BucketedVariation -- the assigned variation, or null if the visitor was not bucketed (rule mismatch, inactive experience, etc.). See BucketedVariation below.

Run All Active Experiences

Loop through every active experience in the project, evaluate targeting rules for each, and return the selected variation for every experience the visitor qualifies for.

const variations: BucketedVariation[] = userContext.runExperiences();

// With attributes:
const variations: BucketedVariation[] = userContext.runExperiences({
  locationProperties: { url: '/pricing' },
  visitorProperties: { plan: 'pro' },
  enableTracking: true
});
use ConvertSdk\DTO\BucketedVariation;
use OpenAPI\Client\BucketingAttributes;

/** @var BucketedVariation[] $variations */
$variations = $context->runExperiences();

// With attributes:
$variations = $context->runExperiences(new BucketingAttributes([
    'locationProperties' => ['url' => '/pricing'],
    'visitorProperties' => ['plan' => 'pro'],
    'enableTracking' => true,
]));

Parameters:

ParameterTypeRequiredDescription
attributesobjectNoSee BucketingAttributes below

Returns: Array<BucketedVariation> -- one entry per experience the visitor was bucketed into.

BucketingAttributes

Every context method that runs experiences or features accepts an optional attributes object. In PHP this is a BucketingAttributes DTO; in JavaScript it is a plain object.

PropertyTypeDescription
locationPropertiesobjectKey-value pairs used for evaluating experience locations
visitorPropertiesobjectKey-value pairs used for evaluating experience audiences (overwrites same keys from context creation)
updateVisitorPropertiesbooleanWhether to permanently update in-memory visitor properties
enableTrackingbooleanWhether to track bucketing events immediately (default: true)
environmentstringOverride environment for this call

BucketedVariation

The object returned when a visitor is successfully bucketed into an experience variation.

PropertyTypeDescription
experienceIdstringThe experience ID
experienceKeystringThe experience key
variationIdstringThe variation ID
variationKeystringThe variation key
changesarrayThe variation changes (feature change data)

See the Data Model for the full type definition and how changes map to features.

Complete Example

A full flow: initialize the SDK, create a user context, run an experience, and use the result.

import type {
  ConvertInterface, ConvertConfig, ContextInterface, BucketedVariation
} from '@convertcom/js-sdk';
import ConvertSDK from '@convertcom/js-sdk';

const convertSDK: ConvertInterface = new ConvertSDK({
  sdkKey: 'xxx'
} as ConvertConfig);

convertSDK.onReady().then(() => {
  const context: ContextInterface = convertSDK.createContext('user-unique-id');
  const variation: BucketedVariation = context.runExperience('experience-key');
  console.log(variation);
});
use ConvertSdk\ConvertSDK;
use ConvertSdk\DTO\BucketedVariation;

$sdk = ConvertSDK::create([
    'sdkKey' => 'your-sdk-key',
]);

if ($sdk->isReady()) {
    $context = $sdk->createContext('visitor-unique-id');

    /** @var BucketedVariation|null $variation */
    $variation = $context->runExperience('experience-key');

    if ($variation !== null) {
        echo $variation->experienceKey; // 'experience-key'
        echo $variation->variationKey;  // e.g. 'variation-1'
    }
}