Create events
How to create and add an event in Optimizely Feature Experimentation.
Events track the success of your experiment by recording specific user actions, such as clicks, pageviews, form submissions, purchases, and scroll depth. To integrate events into your application code using the SDK, see Track events.
Create event
With the Feature Experimentation REST API
Use the Create a custom event endpoint.
Endpoint – POST https://api.optimizely.com/v2/projects/PROJECT_ID/custom_events
Authentication – Include your API key in the Authorization
header as a Bearer token. See Generate tokens and use the REST APIs.
Request example
curl --request POST \
--url https://api.optimizely.com/v2/projects/PROJECT_ID/custom_events \
--header 'accept: application/json' \
--header 'authorization: Bearer TOKEN' \
--header 'content-type: application/json' \
--data '
{
"archived": false,
"description": "Event used to track users that have added the item to the cart.",
"key": "add_to_cart"
}
'
Response example
{
"archived": false,
"category": "other",
"created": "2025-09-12T16:35:31.697630Z",
"description": "Event used to track users that have added the item to the cart."
"event_properties": [],
"event_type": "custom",
"id": ID,
"is_classic": false,
"key": "add_to_cart",
"name": "add_to_cart",
"project_id": PROJECT_ID
}
See the Create a custom event API endpoint reference documentation for information on creating an event with the Feature Experimentation API. You can also follow the REST API cookbookand see step two to create a flag variation.
With the Feature Experimentation UI
See the Create event section in the user documentation for a step-by-step guide to create events using the Feature Experimentation UI.
Add events to your experiments
With the REST API
Use the Update the Ruleset for a Flag in an Environment endpoint to update your A/B test or a multi-armed bandit optimization with your event.
Endpoint – PATCH https://api.optimizely.com/flags/v1/projects/PROJECT_ID/flags/FLAG_KEY/environments/ENVIRONMENT_KEY/ruleset
ImportantFor
PATCH
requests, firstGET
the current object, merge only the fields you need to change, and then send thePATCH
. This helps prevent overwriting existing settings you did not intend to modify.
Authentication – Include your API key in the Authorization
header as a Bearer token. See Generate tokens and use the REST APIs.
Request example
curl --request PATCH \
--url "https://api.optimizely.com/flags/v1/projects/24566260139/flags/scheduled_changes_flag/environments/development/ruleset" \
--header "Authorization: Bearer TOKEN" \
--header "Content-Type: application/json" \
--data '[
{
"op": "add",
"path": "/rules/a-b-test/metrics/-",
"value": {
"event_id": 27189690020,
"event_type": "click",
"scope": "visitor",
"aggregator": "unique",
"winning_direction": "increasing",
"display_title": "Add to cart clicks"
}
}
]'
Response example
{
"url": "/projects/24566260139/flags/scheduled_changes_flag/environments/development/ruleset",
"update_url": "/projects/24566260139/flags/scheduled_changes_flag/environments/development/ruleset",
"enable_url": "/projects/24566260139/flags/scheduled_changes_flag/environments/development/ruleset/enabled",
"rules": {
"a-b-test": {
"key": "a-b-test",
"name": "a-b-test",
"description": "",
"variations": {
"off": {
"key": "off",
"name": "Off",
"percentage_included": 5000,
"variation_id": 1502017,
"id": 6688991
},
"on": {
"key": "on",
"name": "On",
"percentage_included": 5000,
"variation_id": 1502018,
"id": 6688992
}
},
"baseline_variation_id": 1502017,
"type": "a/b",
"distribution_mode": "manual",
"id": 1599695,
"urn": "rules.flags.optimizely.com::1599695",
"archived": false,
"enabled": false,
"created_time": "2025-09-15T19:52:05.908420Z",
"updated_time": "2025-09-15T22:03:43.078372Z",
"audience_conditions": [],
"audience_ids": [],
"percentage_included": 10000,
"metrics": [
{
"event_id": 30097930171,
"event_type": "custom",
"scope": "visitor",
"aggregator": "unique",
"winning_direction": "increasing",
"display_title": "New event with properties"
},
{
"event_id": 27189690020,
"event_type": "click",
"scope": "visitor",
"aggregator": "unique",
"winning_direction": "increasing",
"display_title": "Add to cart clicks"
}
],
"allow_list": {},
"group_rule": {
"group_id": 0,
"traffic_allocation": 0
},
"group_remaining_traffic_allocation": 100,
"layer_id": 9300001987253,
"layer_experiment_id": 9300002732965,
"status": "draft"
}
},
"rule_priorities": [
"a-b-test"
],
"id": 1882548,
"urn": "rulesets.flags.optimizely.com::1882548",
"archived": false,
"enabled": false,
"updated_time": "2025-09-15T22:03:43.149140Z",
"flag_key": "scheduled_changes_flag",
"environment_key": "development",
"environment_name": "Development",
"environment_id": 24540890184,
"default_variation_key": "off",
"default_variation_name": "Off",
"revision": 4,
"status": "draft",
"role": "admin"
}
In the Feature Experimentation UI
See the Add events to your experiment section in the Manage events in Feature Experimentation user documentation for a step-by-step guide.
Track events in your application code
See the track event documentation in your SDK to implement event tracking in your code.
- Android
- C#
- Flutter
- Go
- Java
- JavaScript – SDK versions 6.0.0 and later.
- JavaScript (Browser) – SDK versions 5.3.5 and earlier.
- JavaScript (Node) – SDK versions 5.3.5 and earlier.
- PHP
- Ruby
- Python
- Swift
Create metrics
Metrics measure the success of your experiment by telling you whether an experiment is winning, losing, or inconclusive. Metrics are derived from events, which directly track actions like clicks, pageviews, form submissions, purchases, and scroll depth.
Optimizely Feature Experimentation uses a natural-language metrics builder in the UI that lets you define and add metrics to your experiments and campaigns. See Create a metric in Optimizely using the metric builder for instructions on creating metrics in Feature Experimentation.
Updated 13 days ago