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

SDK v1.0 and higher

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.
  • Remembers variation assignments via the User Profile Service. (Coming soon!)
  • Triggers decision notifications, if subscribed to.

📘

Note

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

Parameters

The table below lists the required and optional parameters in Go.

ParameterTypeDescription
experiment key
required
stringThe experiment to activate.
userContext
required
entities.UserContextHolds information about the user, such as the userID and the user's attributes.

Returns

A variation key, or an empty string if no experiment was activated.

Example

attributes := map[string]interface{}{
        "DEVICE": "iPhone",
        "hey":    2,
}

user := entities.UserContext{
        ID:         "userId",
        Attributes: attributes,
}

variationKey, err := optlyClient.Activate("experiment_key",user)

Side effects

The table lists 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.
NotificationsInvokes the DECISION notification if this notification is subscribed to.

The example code below shows how to add and remove a decision listener.

import (
	"fmt"

	"github.com/optimizely/go-sdk/pkg/client"
	"github.com/optimizely/go-sdk/pkg/notification"
)

// Callback for decision notification
	callback := func(notification notification.DecisionNotification) {

		// Access type on decisionObject to get type of decision
		fmt.Print(notification.Type)
		// Access decisionInfo on decisionObject which
		// will have form as per type of decision.
		fmt.Print(notification.DecisionInfo)
	}

	optimizelyClient, err := optimizelyFactory.Client()

	// Add callback for decision notification
	id, err := optimizelyClient.DecisionService.OnDecision(callback)

	// Remove callback for decision notification
	err = optimizelyClient.DecisionService.RemoveOnDecision(id)

Notes

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 Go is Go.