Skip to content

Mida server-side A/B testing for Javascript NodeJS. Start for free.

License

Notifications You must be signed in to change notification settings

mida-so/mida-node

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Mida.so - Server-side A/B Testing and Feature Flags

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.

Prerequisites

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

Installation

  1. Install the necessary dependencies by running the following command:
npm install mida-node

Usage

To use the server-side A/B testing and feature flags code, follow these steps:

  1. Import the Mida class into your code:
const Mida = require('mida-node');
  1. Create an instance of the Mida class by providing your Mida project key:
const mida = new Mida('YOUR_PROJECT_KEY');

A/B Testing

  1. Use the getExperiment method 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
}
  1. Use the setEvent method 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.

User Attributes

  1. Use the setAttribute method 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);

Feature Flags

  1. Use the isFeatureEnabled method 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
}
  1. Use the onFeatureFlags method to reload the feature flags for the current user:
await mida.onFeatureFlags();

API Reference

Mida(projectKey, options)

  • projectKey: (required) Your Mida project key.
  • options: (optional) An object of additional options.

getExperiment(experimentKey, distinctId)

  • 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.

setEvent(eventName, distinctId)

  • 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.

setAttribute(distinctId, properties)

  • 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.

isFeatureEnabled(key)

  • 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.

onFeatureFlags(distinctId)

  • distinctId: (optional) The distinct ID of the user. Returns a Promise that resolves when the feature flags are successfully reloaded.

Contributing

Contributions are welcome! If you find any issues or have suggestions for improvement, please create a pull request.

License

This code is open source and available under the MIT License.