This is a server-side A/B testing and feature flags code that allows you to integrate with the Mida platform. The code is written in JavaScript and can be used in a Node.js environment.
Before using this code, make sure you have the following set up:
- Node.js installed on your machine
- A Mida.so account with project and experiment key
- Install the necessary dependencies by running the following command:
npm install mida-node
To use the server-side A/B testing and feature flags code, follow these steps:
- Import the
Midaclass into your code:
const Mida = require('mida-node');- Create an instance of the
Midaclass by providing your Mida project key:
const mida = new Mida('YOUR_PROJECT_KEY');- Use the
getExperimentmethod to retrieve the current version of an experiment for a user. You need to provide the experiment key and the distinct ID of the user:
const experimentKey = 'EXPERIMENT_KEY';
const distinctId = 'USER_DISTINCT_ID';
const version = await mida.getExperiment(experimentKey, distinctId);
if (version === 'Control') {
// Handle Control logic
}
if (version === 'Variant 1') {
// Handle Variant 1 logic
}
// Depending on how many variants you have created
if (version === 'Variant 2') {
// Handle Variant 2 logic
}- Use the
setEventmethod to log an event for a user. You need to provide the event name and the distinct ID of the user:
const eventName = 'EVENT_NAME';
const distinctId = 'USER_DISTINCT_ID';
await mida.setEvent(eventName, distinctId);For revenue tracking, you can use the setEvent method with the event name as 'Purchase' and include additional attributes such as revenue, quantity, and currency. Here's an example:
const eventName = 'Purchase';
const distinctId = 'USER_DISTINCT_ID';
const properties = {
revenue: 99.99,
quantity: 1,
currency: 'USD'
};
await mida.setEvent(eventName, distinctId, properties);In this example, the setEvent method is called with the event name 'Purchase' and the distinctId of the user. The properties object contains the additional attributes related to the purchase event:
revenue: The total revenue amount of the purchase (e.g., 99.99).quantity: The quantity of items purchased (e.g., 1).currency: The currency of the revenue amount (e.g., 'USD').
By including these attributes, you can track revenue-related information along with the purchase event.
- Use the
setAttributemethod to set user attributes for a specific user. You need to provide the distinct ID of the user and an object containing the attribute key-value pairs:
const distinctId = 'USER_DISTINCT_ID';
const attributes = {
gender: 'male',
company_name: 'Apple Inc'
};
await mida.setAttribute(distinctId, attributes);- Use the
isFeatureEnabledmethod to check if a feature flag is enabled for the current user:
const featureFlagKey = 'FEATURE_FLAG_KEY';
const isEnabled = await mida.isFeatureEnabled(featureFlagKey);
if (isEnabled) {
// Feature flag is enabled, perform corresponding actions
} else {
// Feature flag is disabled, perform alternative actions
}- Use the
onFeatureFlagsmethod to reload the feature flags for the current user:
await mida.onFeatureFlags();projectKey: (required) Your Mida project key.options: (optional) An object of additional options.
experimentKey: (required) The key of the experiment you want to get the version of.distinctId: (required) The distinct ID of the user. Returns a Promise that resolves to the version of the experiment.
eventName: (required) The name of the event you want to log.distinctId: (required) The distinct ID of the user. Returns a Promise that resolves when the event is successfully logged.
distinctId: (required) The distinct ID of the user.properties: (required) An object containing the attribute key-value pairs. Returns a Promise that resolves when the attributes are successfully set.
key: (required) The key of the feature flag you want to check. Returns a Promise that resolves to a boolean indicating whether the feature flag is enabled or not.
distinctId: (optional) The distinct ID of the user. Returns a Promise that resolves when the feature flags are successfully reloaded.
Contributions are welcome! If you find any issues or have suggestions for improvement, please create a pull request.
This code is open source and available under the MIT License.