Configure Amplitude
Step to enrich Amplitude reports with Optimizely Feature Experimentation experiment and targeted delivery information.
Important
Start by reading the Amplitude Get Started documentation.
Amplitude offers two types of properties to enrich and segment reports with Feature Experimentation experiments and targeted delivery information.
- Event properties – Describes a single event that must be added to each logged event. Event properties are not automatically applied to future events, making them more complex to manage than user properties. See Event properties in the Amplitude documentation.
- User properties – Describes a user across all logged events. When set, Amplitude applies user properties to all subsequent recorded events until they are updated. See User properties in the Amplitude documentation.
Example Decide
callback
Decide
callbackThe following code example demonstrates how to set an Amplitude user property and track an optional impression event. This configuration sends the flag key and variation key to Amplitude as user properties and logs events in their dashboard. Click on the different languages in the heading to see that language.
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
);
Summary of key elements
Language | Activate Callback | Identify interface |
---|---|---|
Android (Java and Kotlin) | decisionNotificationHandler | Identify |
JavaScript | onActivate | Identify |
Swift | activate | AMPIdentify |
If needed, log an impression event in the callback to signify that the Feature Experimentation test was activated. Use these events to calculate conversion rates.
Compare results
To compare Feature Experimentation results with Amplitude, apply a date filter in Amplitude that matches the experiment or targeted delivery dates.
Note
User properties persist in Amplitude even when your Feature Experimentation test is not running.
Unsupported SDKs
The following SDKs do not have a suggested solution for integrating Feature Experimentation with Amplitude:
- Agent
- C#
- Go
- Java
- Node
- PHP
- Python
- Ruby
Updated 9 days ago