Overview

Introduction

The Event API for Optimizely X allows you to send event data to Optimizely’s logging servers from all of your applications, whether they are a web, mobile, or server-side implementations. Once we have your data, we use it to calculate your campaign and experiment metrics and display them on the Results page. The event data is also available in raw format via the Data Export service.

We "consume" our own API: the Optimizely X clients and SDKs send event data with this API. This consistency helps ensure that your data is processed the same way, regardless of how we receive it.

A formal specification of the API can be found at: https://api.optimizely.com/swagger/v1/events.json

Use cases

With the Event API you can send event data from any application without using our native clients or SDKs. There are many reasons why you may need to do this. Here are some examples from Optimizely developers who are using the API today.

  • Offline conversions: You are collecting offline experiment data, such as phone call conversions tracked by third-party software. You would need to store this information locally until the Internet connection is restored. Our SDKs do not currently have this feature built-in.

  • Security: You are working on a restricted environment and are not allowed to load third-party Web snippets or install third-party SDKs. You would need to develop your own method of tracking events and sending them Optimizely.

  • Third-party sources: You want to combine data from third-party sources with your event data. You would need to combine the data before sending it to Optimizely.

  • Communication control: You want your application to have extra control over how events are batched and when they are sent to Optimizely.

  • Integrations and Extensions: You are building a custom Optimizely integration or extension. (Read the Integrations and Extensions sections of the Dev Docs for more information.

Features

  • Maintains data consistency with events sent through our native clients and SDKs
  • Designed to handle billions of events on a daily basis
  • Calculates campaign and experiment metrics in near real-time
  • Supports batching multiple events into a single network call instead of many, which increases application performance and battery life (on mobile devices) while reducing memory and CPU usage
  • Logs raw event data securely in Amazon S3, which can be later accessed via the Data Export service

Important concepts

Before you begin, you must understand the concepts outlined in the article How Optimizely counts conversions. It explains both the underlying theory and mechanism of how event data is connected to experiments and campaigns. If you don't connect your events to your campaigns and experiments correctly in your data, we won't be able to calculate your metrics accurately, and your results will be invalid.

Additional reading

You can learn more about the concepts used in this document by reading the following articles.

Getting Started

This guide will walk through building a data object for the event API. For a complete description of the Event API, consult the Event API reference.

1. Create a new experiment

Create a new experiment from your Optimizely project page and add at least one event and metric. Make sure you start your experiment before proceeding to the next step.

For Web experiments

For Full Stack, Mobile, and OTT experiments

2. Collect the required identifiers

The table shows the required fields to include in the JSON object described in the next step, Build the JSON data object.

Follow the instructions in Knowledge Base article How to locate IDs for the Optimizely X Event API to locate these fields.

Field Type Required to activate user Required to track event
accountId account identifier Y Y
visitorId visitor identifier Y Y
timestamp event timestamp Y Y
eventEntityId event entity identifier Y Y
campaignId campaign assignment(s) Y N
experimentId experiment assignment(s) Y N
variationId variation assignment(s) Y N

3. Build the JSON data object

The JSON data object structure differs depending on whether you want to activate a user to a variation or track conversion events.

Note: A single JSON object must be 10 MB or less.

Activate users

To activate a user to variation, send a JSON object that includes:

  • The decisions array, which contains the campaign_id, experiment_id, and variation_id.
  • The events array, which contains a special event of type campaign_activated that represents the visitor being bucketed to a particular variation. By convention, this event's entity_id is mapped to the corresponding variation's campaign_id as given in the decisions array.
  • The enrich_decisions field set to true to enable Optimizely's Easy Event Tracking functionality. For more information about this field, see the Event API reference. See also How Optimizely counts conversions.

Note that:

  • The campaign_activated event must have a timestamp equal to or older than other tracked conversion events, as explained in How Optimizely counts conversions. Any conversion events with timestamps older than the related decision event will not be counted in the Results page.
  • A single visitor can only be exposed to one variation of one experiment within a campaign. Ensure that each campaign_activated event for a given visitor is mapped to only one campaign_id, experiment_id, and variation_id triplet.
  • You don't need to activate a user if they have already been activated by a Full Stack or Web activate() call.

The example includes the optional client_name and client_version fields. We recommend that you include these fields for debugging purposes.

{
  "account_id": "1887578053",
  "visitors": [
    {
      "visitor_id": "java-lover@example.com",
      "snapshots": [
        {
          "decisions": [
            {
              "campaign_id": "9560823711",
              "experiment_id": "5733750339",
              "variation_id": "6630810318"
            }
          ],
          "events": [
            {
              "entity_id": "9560823711",
              "type": "campaign_activated",
              "timestamp": 1491519130343,
              "uuid": "3a427b02-7ae0-4b20-8f02-32cc8a067be4"
            }
          ]
        }
      ]
    }
  ],
  "anonymize_ip": true,
  "client_name": "Optimizely/event-api-demo",
  "client_version": "1.0.0",
  "enrich_decisions": true
}

Track conversion events

A conversion event represents a visitor taking a specific action on your site.

If enrich_decisions is true, Optimizely retains the activation information for each visitor (meaning, the campaign_id, experiment_id, and variation_id). Subsequent conversion events are automatically matched to the appropriate variation(s). See the Activate users section above for more information about the client_name, client_version, and enrich_decisions fields shown in the example below.

Note that:

  • You still need to pass an empty decisions array as shown in the example below.
  • The conversion event must have a timestamp equal to or more recent than the timestamp of the campaign_activated event, as explained in the How Optimizely counts conversions. Any conversion events with timestamps older than the related campaign_activated event will not be counted in the Results page.
{
    "account_id": "your_account_id",
    "visitors": 
     [
        {
            "visitor_id": "test_user",
            "attributes": {},
            "snapshots": [
              {
               "decisions": [],
               "events": [
                 { 
                    "entity_id": "123456",
                    "key": "test_event",
                    "timestamp": 1540996187279,
                    "revenue": 10000,
                    "uuid": "2314324123"
                 }
               ]
            }
         ]
       }
     ],
     "anonymize_ip": true,
     "client_name": "Optimizely/event-api-demo",
     "client_version": "1.0.0",
     "enrich_decisions": true
}

For more information about the required and optional fields, see the Event API Reference.

4. Send data object as POST call

After you build the JSON data object, send it to the Event API as a POST call. There is no Authorization token requirement to use the API. Below is a sample cURL request.

After the API has receives your complete call, it queues your JSON file for processing and returns a response with status code 204.

Important!: The response doesn't indicate that your JSON has been validated. Consult the Event API reference for more guidance.

See the Activate users section above for more information about the client_name, client_version, and enrich_decisions fields shown in the example below.

curl -X POST 
  -H "Content-Type: application/json" -d
  "https://logx.optimizely.com/v1/events"
{
    "account_id": "your_account_id",
    "visitors": 
     [
        {
            "visitor_id": "test_user",
            "attributes": {},
            "decisions": [],
            "events": [
               { 

                    "entity_id": "123456",
                    "key": "test_event",
                    "timestamp": 1540996187279,
                    "revenue": 10000,
                    "uuid": "2314324123"
               }
             ],
         }
     ],
     "anonymize_ip": true,
     "client_name": "Optimizely/event-api-demo",
     "client_version": "1.0.0",
     "enrich_decisions": true
}

5. Verify results

Within a few minutes, you will see the data reflected in the Results page. If you don't, read the troubleshooting section to debug common issues that can lead to unexpected results.

Bundle events and visitors

This topic shows how you can bundle events and visitors in a JSON object to improve performance.

The examples below include the optional client_name and client_version fields. We recommend that you include these fields for debugging purposes. For a complete description of the Event API, consult the Event API reference.

Note: A single JSON object must be 10 MB or less.

Bundle more events

This example shows that a visitor has been exposed to another variation and performs another tracked action on the site.

Note: The order of the events in the events array is not important.

{
  "account_id": "1887578053",
  "project_id": "7205579439",
  "visitors": [
    {
      "session_id": "",
      "visitor_id": "java-lover@example.com",
      "snapshots": [
        {
          "decisions": [],
          "events": [
            {
              "key": "4841578001_view_optimizely_dev_docs",
              "revenue": 100,
              "timestamp": 1491519130344,
              "uuid": "145a44a2-6320-48d6-80d6-bff20b32cd65"
            },
            {
              "key": "8766076075_click_view_docs",
              "revenue": 400,
              "timestamp": 1491519130352,
              "uuid": "3bb9371d-c245-4bf8-8086-5fef9f60cd06"
            }
          ]
        }
      ]
    }
  ],
  "anonymize_ip": true,
  "client_name": "Optimizely/event-api-demo",
  "client_version": "1.0.0",
  "enrich_decisions": true
}

Bundle more visitors

This example shows that a second visitor has been exposed to a different variation of the same campaign and experiment as the first visitor. The second visitor performs one of the same tracked actions as the first visitor.

{
  "account_id": "1887578053",
  "project_id": "7205579439",
  "visitors": [
    {
      "session_id": "",
      "visitor_id": "java-lover@example.com",
      "snapshots": [
          {
          "decisions": [],
          "events": [
            {
              "key": "4841578001_view_optimizely_dev_docs",
              "revenue": 100,
              "timestamp": 1491519130344,
              "uuid": "145a44a2-6320-48d6-80d6-bff20b32cd65"
            },
            {
              "key": "8766076075_click_view_docs",
              "revenue": 400,
              "timestamp": 1491519130352,
              "uuid": "3bb9371d-c245-4bf8-8086-5fef9f60cd06"
            }
          ]
        }
      ]
    },
    {
      "session_id": "",
      "visitor_id": "python-ftw@foo.co.uk",
      "snapshots": [
        {
          "decisions": [],
          "events": [
            {
              "key": "4841578001_view_optimizely_dev_docs",
              "revenue": 400,
              "timestamp": 1491519130362,
              "uuid": "899de30f-fa1b-440c-b8a8-75a18da104a2"
            }
          ]
        }
      ]
    }
  ],
  "anonymize_ip": true,
  "client_name": "Optimizely/event-api-demo",
  "client_version": "1.0.0",
  "enrich_decisions": true
}

Troubleshooting

I sent events via the API, but they are not showing up in the results page.

  • Experiment was/is not running
    Make sure the experiment is running and the events have timestamps within the bounds of the running experiment.

  • Event payload was malformed
    Inspect the payload to verify that all required identifiers are properly set to conform with the API's Swagger Specification. If the API returns with a 204 Response code, it means that the Optimizely Logging Service has received and persisted the payload. This response code is not an indication that the event payload has been validated.

  • Event had one or more incorrect or missing identifiers
    Inspect the payload and verify that all required identifiers are properly set.

  • Event was missing a decision
    When activating users, verify that you sent a decision event and that the decision event had an earlier timestamp than the conversion events. Read How Optimizely counts conversions for more information.

  • Decision was sent but not attributed to the correct campaign_id
    When activating users, make sure the entity_id matches the campaign_id for the variation the user was assigned to.

  • Event had an incorrect or out of bounds timestamp
    When activating and tracking users, make sure timestamps have the correct format (a 13-digit integer, representing time since epoch in milliseconds) and are within bounds of the running experiment.

I sent events with revenue values via the API, but the revenue is not calculated correctly in the results page.

  • Revenue is missing from event payload
    Make sure revenue is present under the correct conversion event.

  • Revenue has incorrect value
    Make sure revenue has an integer value and represents the revenue in cents.

I sent events via the API, I see some results but I don't get expected values.

  • Duplicate events are sent
    Optimizely detects events that have the same entity_id, uuid and timestamp and saves only one of them. Make sure each event is using a unique uuid or timestamp before sending.

  • Events have out-of-bounds timestamps
    Make sure timestamps have the correct format (a 13-digit integer, representing time since epoch in milliseconds) and are within bounds of the running experiment.

  • Conversion events have timestamps older than the decision event's timestamp
    Make sure the conversion events have timestamp values that are equal to or more recent than the related decision event's timestamp.