Dev guideAPI Reference
Dev guideAPI ReferenceUser GuideGitHubNuGetDev CommunitySubmit a ticketLog In
GitHubNuGetDev CommunitySubmit a ticket

Set up Amplitude

This topic describes how to enrich Amplitude reports with Optimizely test information.

🚧

Important

If you are getting started, read the Amplitude Quick Start Guide first.

Amplitude features two types of properties that you can use to enrich and segment their reports with Optimizely test information:

  • Event properties – Describes a single event, so they must be added to each logged event. The Amplitude SDK and backend do not automatically add event properties to future events. For this reason, user properties are less complex to work with than event properties.

  • User properties – Describes a user across all logged events. After you set a user property, Amplitude applies it in their backend to subsequent recorded events until someone changes the property.

The following example code demonstrates a Decide callback. In the callback, the code shows Amplitude's identify interface setting a new user property, which includes the flag key and variation key. The user property should immediately propagate to the Amplitude backend and be visible in their dashboard. To compare the performances of variations in a test, create a new segment for each variation.

import Amplitude_iOS
let activateNotificationId =  notificationCenter.addDecisionNotificationListener(decisionListener: { (type, userId, attributes, decisionInfo) in
  let isEventDispatched = decisionInfo["decisionEventDispatched"] as? Bool
    if type == "flag", isEventDispatched == true {
        let variationKey = decisionInfo["variationKey"] as! String
        
        let propertyKey : String! = "[Optimizely] " + decisionInfo["flagKey"]
        let identify : AMPIdentify = AMPIdentify()
        identify.set(propertyKey, value: variationKey)

        // Track impression event (optional)
        let eventIdentifier : String = "[Optimizely] "
        + decisionInfo["flagKey"] + " - " + variationKey
        Amplitude.instance().logEvent(eventIdentifier)
    }
})

import com.amplitude.api.Amplitude;
import com.amplitude.api.AmplitudeClient;
import com.amplitude.api.Identify;
import com.optimizely.ab.config.Experiment;
import com.optimizely.ab.config.Variation;
import com.optimizely.ab.notification.DecisionNotification;
import com.optimizely.ab.notification.NotificationCenter;

OptimizelyClient optimizelyClient = optimizelyManager.getOptimizely();
// Add a Decision listener
int notificationId = optimizelyClient.getNotificationCenter().addNotificationHandler(DecisionNotification.class, decisionNotificationHandler -> {
            Map<String, ?> decisionInfo = decisionNotificationHandler.getDecisionInfo();
            Boolean isEventDispatched = (Boolean) decisionInfo.get("decisionEventDispatched");
            if (decisionNotificationHandler.getType().equals("flag") && (isEventDispatched != null && isEventDispatched)) {
                String variationKey = (String) decisionInfo.get("variationKey");

                String propertyKey = "[Optimizely] " + decisionInfo.get("flagKey");

                // Set "user property" for the user
                Identify identify = new Identify().set(
                        propertyKey, variationKey);
                AmplitudeClient amplitudeClient = Amplitude.getInstance();
                amplitudeClient.identify(identify);

                // Track impression event (optional)
                amplitudeClient.logEvent(
                        propertyKey + " - " + variationKey);
            }
        });
import com.amplitude.api.Amplitude;
import com.amplitude.api.AmplitudeClient;
import com.amplitude.api.Identify;
import com.optimizely.ab.config.Experiment;
import com.optimizely.ab.config.Variation;
import com.optimizely.ab.notification.DecisionNotification;
import com.optimizely.ab.notification.NotificationCenter;

var notificationId = optimizelyClient.notificationCenter.addNotificationHandler(
    DecisionNotification::class.java,
    NotificationHandler{ handler: DecisionNotification? ->
        var decisionInfo = handler?.decisionInfo
        var type = handler?.type

        var isEventDispatched: Boolean? = decisionInfo?.get("decisionEventDispatched") as? Boolean
        if  ((isEventDispatched == true) && (type == "flag")) {
            var variationKey: String?  = decisionInfo?.get("variationKey") as? String
            var propertyKey: String?  = decisionInfo?.get("propertyKey") as? String

            val identify = Identify().set(propertyKey, variationKey)
            val amplitudeClient = Amplitude.getInstance()
            amplitudeClient.identify(identify)

            // Track impression event (optional)
            amplitudeClient.logEvent(propertyKey + " - " + variationKey)
        }
    }
)

import { createInstance, enums as optimizelyEnums } from '@optimizely/optimizely-sdk';
import * as amplitude from '@amplitude/analytics-browser';

amplitude.init('<AMPLITUDE_API_KEY>');

const optimizely = createInstance({
  sdkKey: '<YOUR_SDK_KEY>',
});

const onDecision = ({ type, decisionInfo }) => {
  const { decisionEventDispatched, variationKey, flagKey } =  decisionInfo;
  
  if (type === 'flag' && decisionEventDispatched) {
    const propertyKey = `[Optimizely] ${flagKey}`;
    const identify = new amplitude.Identify().set(propertyKey, variationKey);
    amplitude.identify(identify);

    // Track impression event (optional)
    amplitude.logEvent(`[Optimizely] ${flagKey} - ${variationKey}`);
  }
};

// Add a DECISION notification listener
const notificationId = optimizely.notificationCenter.addNotificationListener(
  optimizelyEnums.NOTIFICATION_TYPES.DECISION,
  onDecision
);

In the preceding example code, look for the following items.

LanguageActivate CallbackIdentify interface
Android (Java and Kotlin)decisionNotificationHandlerIdentify
JavaScriptonActivateIdentify
SwiftactivateAMPIdentify

If you want, you can log an impression event in the callback to signify that the Optimizely test was activated for the current user. You can later use this event (or another event you may already be tracking) to calculate a conversion rate.

Compare results

When comparing numbers between Optimizely and Amplitude results, remember to apply a date filter in Amplitude that corresponds with the dates your Optimizely test was running.

📘

Note

User properties remain set in Amplitude, regardless of whether your Optimizely test is running.

Unsupported SDKs

Optimizely does not have a suggested solution for integrating Amplitude with our SDKs for these platforms:

  • Agent
  • C#
  • Go
  • Java
  • Node
  • PHP
  • Python
  • Ruby