GuidesAPI Reference
Submit Documentation FeedbackJoin Developer CommunityLog In

Track user events

Introduction

Experiments let you track how users reacted to the different variations. So, you'll need to tell Optimizely which events to track in order to gauge the relative success of your experiment. After Optimizely aggregates enough events to produce meaningful metrics, you'll be able to judge your flag variations on the Results page.

As part of your experimental design process, you'll choose on how to aggregate your tracked events into metrics. User events that power metrics are referred to as conversion events. Conversion events can be big or small. You can implement conversion events to capture clicks, pageviews, form submissions, purchases, scroll depth, etc. In general, you want to track events that are closely associated in terms of user experience to the experimental changes you're making.

Setup overview

At a high level, you'll take these steps:

  1. In the Optimizely app, create an event key string. See Create events.
  2. Copy the sample integration code for the Track Event method that Optimizely generates.
  3. Use the Track Event method to label the event code you want to track. In order for your conversion event to populate experiment metrics on the Results page, track event code that a user is exposed to if they see a flag variation.
    Note: We recommend getting in the habit of tracking interesting events even if they're not behind feature flags, so you can more easily create future experiments.
    Now, every time a user triggers the event, Track Event passes a conversion event to Optimizely.
  4. (Recommended) Filter out third-party bots so they don't skew your experimental results. For more information see Filter out bots.
  5. Add your events to a flag rule. See Run A/B tests and Run flag deliveries.

You can track events server-side using one of the Full Stack SDKs, or client-side using the JavaScript SDK. You can also track events using our Events API where you do not have an Optimizely SDK implemented. For more information, see Track across platforms.

🚧

Troubleshooting empty results

The Results page only shows events that are tracked after a Decide method has been called for the user who triggered the event. If you do not see results on the Results page, make sure that you are evaluating the feature flag before tracking conversion events.

Example code

The example below shows the Track Event method called, including optional user attributes.

// Track a conversion event for the provided user with attributes
let user = optimizely.createUserContext(userId: "user123", attributes: ["logged_in":true])
  
// Track a user event
try? user.trackEvent(eventKey: "purchased")

Track events across platforms

In circumstances where you can't install our SDK (for example, for offline event tracking and other advanced use cases), you can instead use the Event API.

In circumstances where you can install our SDK, you can rest assured that all of our SDK languages have the same audience evaluation and targeting behavior, so you'll see the same output from experiment decisions and tracking as long as you are using the same datafile and user contexts. You can use any of our SDKs to track events, so you can run experiments that span multiple applications, services, or devices.

For example, you can make the experiment decisions using our server-side SDKs (Python, Java, Ruby, C#, Node, or PHP SDKs), but track user actions using our client-side SDKs (including JavaScript, React, iOS, or Android SDKs).

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

For more information, see Multiple languages.

Track numeric metrics

It’s possible to track nonbinary metrics on the Results page, such as revenue or other numeric values. For information on using reserved event tags to track nonbinary metrics, see Tag events.

Next Steps

After you Create events:

🚧

Important

The Easy Event Tracking functionality included in the Full Stack v3.0 release changes the results of the Optimizely Data Export.

When implementing Full Stack v3.0 SDKs (generally, SDKs released after February 2019), you must also upgrade to the Results Exports functionality to obtain reliable event attribution data. For more information, see the Important note in the product changelog


Did this page help you?