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
These docs are for v2.0. Click to read the latest docs for v3.0.

Track events

You can track conversion events from your code with theTrack method. This method requires an event key and a user ID, and also takes optional user attributes; see the example below.

The event key should match the event key you provided when creating events in the Optimizely app.

The user ID should match the ID provided in the Activate or Is Feature Enabled functions. You must pass the same user attributes you used when activating the experiment.

// Track a conversion event for the provided user with attributes
optimizelyClient.track(eventKey, userId, attributes);
// Track a conversion event for the provided user with attributes
OptimizelyClient.Track(eventKey, userId, attributes);
// Track a conversion event for the provided user with attributes
optimizelyClient.track(eventKey, userId, attributes);
// Track a conversion event for the provided user with attributes
optimizelyClient.track(eventKey, userId, attributes);
// Track a conversion event for the provided user with attributes
optimizelyClient.track("my_conversion", userId, {
  plan_type: "silver"
});
// Track a conversion event for the provided user with attributes
[optimizely track:@"my_conversion"
           userId:@"user123"
       attributes:attributes];
// Track a conversion event for the provided user with attributes
$optimizelyClient->track($eventKey, $userId, $attributes);
# Track a conversion event for the provided user with attributes
optimizely_client.track(event_key, user_id, attributes)
# Track a conversion event for the provided user with attributes
optimizely_client.track(event_key, user_id, attributes)
// Track a conversion event for the provided user with attributes
optimizely?.track("my_conversion", userId:"user123", attributes:attributes)

For more information about when and how to track events, see Event tracking and the other topics in this section.

Usage notes

The Track function can be used to track events across multiple experiments. It will be counted for each experiment only if Activate or Is Feature Enabled has previously been called for the current user.

Attributes are required for event tracking, not just for segmentation. Events are only tracked for users who meet the audience conditions of at least one running experiment with a metric defined for the matching event. When users do not meet those conditions, events are not tracked.

🚧

Important

The Results page only shows events that are tracked after Activate or Is Feature Enabled has been called. If you do not see results on the Results page, make sure that you are activating the experiment or evaluating the feature flag before tracking conversion events.

For information about implementing the User Profile Service purely for tracking purposes and not sticky bucketing, see Implement a Service.

Track events across platforms

For offline event tracking and other advanced use cases, you can also use the Events API.

You can use any of our SDKs to track events, so you can run experiments that span multiple applications, services, or devices. All of our SDKs have the same bucketing and targeting behavior, so you'll see the same output from experiment activation and tracking as long as you are using the same datafile and user IDs.

For example, if you're running experiments on your server, you can activate experiments with our Python, Java, Ruby, C#, Node, or PHP SDKs, but track user actions client-side using our JavaScript, Objective-C or Android SDKs.

If you plan to use multiple SDKs for the same project, make sure that all SDKs share the same datafile and user IDs.

For more information, see Multiple languages.