Disclaimer: This website requires Please enable JavaScript in your browser settings for the best experience.

Optimizely will be sunsetting Full Stack Experimentation on July 29, 2024. See the recommended Feature Experimentation migration timeline and documentation.

Dev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideLegal TermsGitHubDev CommunityOptimizely AcademySubmit a ticketLog In
Dev Guide

Activate

This topic describes the Activate method, which activates an A/B test for the specified user to start an experiment.

Activates an A/B test for the specified user to start an experiment: determines whether they qualify for the experiment, buckets a qualified user into a variation, and sends an impression event to Optimizely.

Version

3.1.1

Description

This method requires an experiment key, user ID, and (optionally) attributes. The experiment key must match the experiment key you created when you set up the experiment in the Optimizely app. The user ID string uniquely identifies the participant in the experiment.

If the user qualifies for the experiment, the method returns the variation key that was chosen. If the user was not eligible—for example, because the experiment was not running in this environment or the user didn't match the targeting attributes and audience conditions—then the method returns null.

Activate respects the configuration of the experiment specified in the datafile. The method:

  • Evaluates the user attributes for audience targeting.
  • Includes the user attributes in the impression event to support results segmentation.
  • Hashes the user ID or bucketing ID to apply traffic allocation.
  • Respects forced bucketing and whitelisting.
  • Triggers an impression event if the user qualifies for the experiment.

Activate also respects customization of the SDK client. Throughout this process, this method:

  • Logs its decisions via the logger.
  • Triggers impressions via the event dispatcher.
  • Raises errors via the error handler.
  • Remembers variation assignments via the User Profile Service.
  • Alerts notification listeners, as applicable.

📘

Note

For more information on how the variation is chosen, see How bucketing works.

Parameters

The parameter names for Swift are listed below.

ParameterTypeDescription
experimentKey
required
stringThe experiment to activate.
userId
required
stringThe user ID.
attributes
optional
mapA map of custom key-value string pairs specifying attributes for the user that are used for audience targeting and results segmentation. Non-string values are only supported in the 3.0 SDK and above.

Returns

The key of the variation where the user is bucketed. It throws an OptimizelyError if the user does not qualify for the experiment or other error is detected.

Example

let attributes = [
  "device": "iPhone",
  "lifetime": 24738388,
  "is_logged_in": true,
]

let variationKey = try? optimizely.activate(experimentKey:"my_experiment_key",
                                            userId: "user_123",
                                            attributes: attributes)
NSDictionary *attributes = @{
  @"device": @"iPhone",
  @"lifetime": @24738388,
  @"is_logged_in": @true
};

NSString *variationKey = [optimizely activateWithExperimentKey: @"my_experiment_key"
                          userId:@"user_123"
                          attributes:attributes
                          error:nil];

Side effects

The table lists other other Optimizely functionality that may be triggered by using this method.

FunctionalityDescription
ImpressionsAccessing this method triggers an impression if the user is included in an active A/B test.

See Decisions and impressions for guidance on when to use Activate versus Get Variation.
Notification ListenersIn SDKs v3.0 and earlier: Activate invokes the ACTIVATE notification listener if the user is included in an active A/B test.

In SDKs v3.1 and later: Invokes the DECISION notification listener if this listener is enabled.

Notes

iOS 14 & IDFA Changes

Optimizely's SDKs do not access any user data or identifiers that are not explicitly passed in via instrumented API calls.

As a result, we do not have any direct dependency on or SDK changes planned to accommodate Apple's upcoming changes to its privacy disclosure and opt-in policy related to their ID for Advertisers (IDFA).

Optimizely does not link user data with third-party data for advertising or advertising measurement purposes or share data with data brokers. We recommend reviewing these requirements, other identifier options and your data use in detail to determine the correct path forward for your application disclosure and ID usage.

Activate versus Get Variation

For details on whether to use Activate or Get Variation, see Activate versus Get Variation.

Source files

The language/platform source files containing the implementation for Swift is OptimizelyClient.swift.