Skip to main content

Creating assignment table from Unleash

Note: this guide assumes that you have already integrated Unleash feature flags into your code.

  1. Find all places in your code where your feature flags are being invoked.

Find the SDK that you're using in the Unleash SDK documentation and identify the syntax you're searching for.

Unleash supports feature toggle variants and typically exposes a getVariant() method for retrieving the variant the user is seeing.

const { initialize } = require('unleash-client');
const unleash = initialize({
url: 'http://unleash.herokuapp.com/api/',
appName: 'my-app-name',
instanceId: 'my-unique-instance-id',
customHeaders: {
Authorization: 'API token',
},
});

unleash.on('synchronized', () => {
// Unleash is ready to serve updated feature toggles.

// Check the variant
const variant = unleash.getVariant('button-color-experiment');

if (variant.name === "blue") {
// show blue button
} else if (variant.name === "red") {
// show red button
} else {
// show default
}
});
  1. Log all feature flag invocations.

In order to capture assignment data, every time the feature flag is invoked, you need to log the user, timestamp, and which experiment and variant they're seeing

function checkFeatureEnabled(experimentKey, defaultValue) {
// determine whether or not this feature is enabled for the current user
const isEnabled = unleash.isEnabled('button-color-experiment');


// determine the variant enabled for this current user
const variant = unleash.getVariant('button-color-experiment');

/// here you show the different variations
if (variant.name === "blue") {
// show blue button
} else if (variant.name === "red") {
// show red button
} else {
// show default
}

// eventService here is the system you use for logging arbitrary events
// which ultimately end up in your data warehouse. The format/structure
// of sending these events will vary based on the interface of that system.
eventService.logEvent({
event_name: "ExperimentAssignmentEvent",
event_params: {
timestamp: new Date().toISOString(),
user_id: ID_OF_THE_CURRENT_USER,
experiment: experimentKey,
variation: variant.name,
device_id: DEVICE_ID,
},
});

// return the variant
return variant;
}

You need to ensure that all usages of feature flags (or at least those you wish to run an experiment on) are wrapped with this new function you created.

It is possible that an engineer on your team could call this new function before showing the new feature to the user. Be sure that the assignment event is only sent once the user experiences the feature you are experimenting on.