1. Help Center
  2. Build
  3. Create a Server-Side Campaign

Integrating a Feature Flag Server-Side using an Origin Experiment

In this article, we'll guide you through integrating a server-side feature flag using a cookie to control the flag state. This setup is particularly useful for testing new features in a production environment with minimal risk.

Step 1: Creating the Origin Experiment in SiteSpect

Origin Experiments allow you to test variations directly from your server-side code by leveraging cookies, headers, or URL parameters. Here's how to set up an experiment using a cookie as the flag:

  1. Create a New Campaign:

    • Go to the Campaigns menu and select New Campaign.
    • Choose A/B Campaign since we're working with a binary feature flag (enabled/disabled).
  2. Define the Campaign:

    • Enter a descriptive name for your campaign that indicates it's using a feature flag.
    • In the General section, define the percentage of users that should be exposed to this campaign, and select a campaign set.
    • Write a hypothesis that describes what you believe your Campaign accomplishes.
  3. Set up the Origin Variation:

    • Go to the Variations tab, click New, and select Origin Factor.
    • Configure the variation to modify a request header or cookie. For a cookie-based feature flag, set the cookie name and values corresponding to your variations (e.g., "feature_enabled" with values "true" and "false").
  4. Create Variations:

    • In the Variations section, under Variation Group 1 > Variation 1, select Type  > Origin. 
    • Configure the variation to modify a request header or cookie. For a cookie-based feature flag, set the cookie name and values corresponding to your variations (e.g., "feature_enabled" with values "true" and "false").
    • The Original variation group represents the default or off-state.
    • The Variation Group represents the enabled state.
    • For the enabled state, set the cookie value to "true", and for the disabled state, set it to "false".
    • Rename the variation groups for easy-to-understand feature names and states.
    • Define Triggers as needed, to set specific rules for when the flag should be set.
  5. Select Metrics:

    • Go to the Metrics section to  what actions or behaviors you will measure to evaluate the impact of the feature flag.
  6. Define Audiences (if applicable):

    • If the feature should only be tested on a specific subset of users, configure your audiences under the Audiences tab.
  7. Save the Campaign:

    • Once all settings are configured, save the campaign.

For more details on creating and managing origin experiments in SiteSpect, you can visit the documentation here.

Step 2: Integrating Feature Flag Logic in Your Code Base

To utilize the feature flag within your server-side application, you need to check the value of the cookie.

Here are basic code snippet examples to demonstrate this.

Python

Using Flask, a popular web framework in Python, here's how you might check the cookie value:

from flask import request, Flask

app = Flask(__name__)

@app.route('/')
def home():
    feature_flag = request.cookies.get('feature_enabled', 'false')
    if feature_flag.lower() == 'true':
        return "Feature is enabled."
    else:
        return "Feature is disabled."

if __name__ == '__main__':
    app.run(debug=True)

Node.js

In Node.js, using the Express framework and the cookie-parser middleware to handle cookies:

const express = require('express');
const cookieParser = require('cookie-parser');
const app = express();

app.use(cookieParser());

app.get('/', (req, res) => {
    const featureEnabled = req.cookies.feature_enabled === 'true';
    if (featureEnabled) {
        res.send('Feature is enabled.');
    } else {
        res.send('Feature is disabled.');
    }
});

app.listen(3000, () => console.log('Server running on port 3000'));

PHP

In PHP, you can access cookies directly through the $_COOKIE superglobal array:

function isFeatureEnabled() {
    return isset($_COOKIE['feature_enabled']) && $_COOKIE['feature_enabled'] === 'true';
}

// Example usage
if (isFeatureEnabled()) {
    echo "Feature is enabled.";
} else {
    echo "Feature is disabled.";
}

Java

In Java, particularly within a servlet, you would check the cookie value like this:

import javax.servlet.ServletException;
import javax.servlet.http.*;

public class FeatureFlagServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cookie[] cookies = request.getCookies();
        boolean featureEnabled = false;
        
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if ("feature_enabled".equals(cookie.getName()) && "true".equals(cookie.getValue())) {
                    featureEnabled = true;
                    break;
                }
            }
        }

        response.setContentType("text/html");
        response.getWriter().print(featureEnabled ? "Feature is enabled." : "Feature is disabled.");
    }
}

These snippets provide the basic functionality required to integrate a feature flag based on a cookie in various programming environments. Adjust the code as necessary to fit into your specific application structure and coding standards.

Step 3: Activate the Campaign and Start Serving Your Feature

When ready, set the campaign status to Active - Running or schedule a start and end time.

The campaign will start setting the feature flag according to the traffic percentage, campaign set, audiences, and triggers defined.