HomeGuidesAPI Reference
Submit Documentation FeedbackJoin Developer CommunityLog In
Hey! These docs are for version 2.1.0-full-stack, which is no longer officially supported. Click here for the latest version, 1.0!

Set up Segment

If you're getting started, read the Segment documentation for an introduction and setup instructions, and the guides for FAQs and getting started information.

Android, Objective-C, and Swift

Segment has a semantic event that you can use to track A/B test variations for users. In the example code below, we add an activate listener block callback to send Segment's A/B event.

import com.segment.analytics.Analytics;
import com.segment.analytics.Properties;

OptimizelyClient optimizelyClient = optimizelyManager.getOptimizely();
// Add a Activate listener
int notificationId = optimizelyClient.getNotificationCenter().addNotificationListener(NotificationCenter.NotificationType.Activate, new ActivateNotificationListener() {
  public void onActivate(Experiment experiment,
                    String userId,
                    Map<String, String> attributes,
                    Variation variation) {

    String experimentId = experiment.getId();
    String experimentKey = experiment.getKey();
    String variationId = variation.getId();
    String variationKey = variation.getKey();

    Properties properties = new Properties();
    properties.put("experimentId", experimentId);
    properties.put("experimentName", experimentKey);
    properties.put("variationId", variationId);
    properties.put("variationName", variationKey);

    Analytics.with(getApplicationContext()).track("Experiment Viewed", properties);

#import <Analytics/SEGAnalytics.h>
[optimizely.notificationCenter addActivateNotificationListener:^(OPTLYExperiment *experiment, NSString *userId, NSDictionary<NSString *,NSString *> *attributes, OPTLYVariation *variation, NSDictionary<NSString *,NSString *> *event) {
       NSDictionary *properties = @{
           @"experimentId" : [experiment experimentId],
           @"experimentName" : [experiment experimentKey],
           @"variationId" : [variation variationId],
           @"variationName" : [variation variationKey]
       [[SEGAnalytics sharedAnalytics] track:@"Experiment Viewed" properties:properties];
import Analytics

let activateNotificationId = optimizely?.notificationCenter?.addActivateNotificationListener({ (experiment, userId, attributes, variation, logEvent) in
        let properties = ["experimentId": experiment.experimentId as Any,
                  "experimentName": experiment.experimentKey as Any,
                  "variationId": variation.variationId as Any,
                  "variationName": variation.variationKey as Any]
        SEGAnalytics.shared().track("Experiment Viewed", properties: properties)


See the example code below, and refer to Segment's documentation for instructions on integrating this SDK with your Segment analytics.

const SEGMENT_WRITE_KEY = 'key';
const datafile = {}; // YOUR DATAFILE HERE

// Load the Segment Lib

// Initialize Segment

const userId = 'user~1';

 * Identify user + add user attributes/traits.
 * These user traits are passed along to Optimizely as user attributes.
analytics.identify(userId, {
  age: 24,
  language: 'yes',

 * Initialize Optimizely and make sure it is appended to the global
 * variable that the Segment integration can use: window.optimizelySdk.
const optimizelyClientInstance = window.optimizelySdk.createInstance({

 * Metadata associated with the event
 * These are passed along to Optimizely as eventTags
var eventProperties = { category: 'shoes', purchasePrice: 42 };
analytics.track('page_loaded', eventProperties);

 * If you wish to override the userId and user attributes set during the
 * `.identify` call or you don't want to use the `.identify` call you
 * can pass the userId and userAttributes as options to the track call.

var options = { Optimizely: { userId: userId, attributes: userAttributes } };
analytics.track('page_loaded', eventProperties, options);

C#, Java, PHP, Python, and Ruby

Refer to Segment's documentation for instructions on integrating our SDKs with your Segment analytics.

Did this page help you?