---
title: Integrating a Feature Flag Server-Side using an Origin Experiment
description: 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.
---

[Skip to content](https://doc.sitespect.com/knowledge/integrating-a-feature-flag-server-side-using-an-origin-experiment#main-content)

English

Show submenu for translations

![Monetate-Logo-Full-Color-1](https://doc.sitespect.com/hs-fs/hubfs/Monetate-Logo-Full-Color-1.png?width=340&height=49&name=Monetate-Logo-Full-Color-1.png)

- [A/B Testing](https://www.sitespect.com/testing-and-experimentation/)
- [Personalization](https://www.sitespect.com/personalization-targeting/)
- [Recommendations](https://www.sitespect.com/recommendations/)

Open main navigation

Close main navigation

- [A/B Testing](https://www.sitespect.com/testing-and-experimentation/)
- [Personalization](https://www.sitespect.com/personalization-targeting/)
- [Recommendations](https://www.sitespect.com/recommendations/)
- English
  
  Show submenu for translations

 How can we help you?

- There are no suggestions because the search field is empty.

1. [Knowledge Base](https://doc.sitespect.com/knowledge?hsLang=en)
2. [Build](https://doc.sitespect.com/knowledge/build?hsLang=en)
3. [Create a Server-Side Campaign](https://doc.sitespect.com/knowledge/build?hsLang=en#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](https://doc.sitespect.com/knowledge/campaign-sets-vizualisation?hsLang=en).
     - Write a [hypothesis](https://doc.sitespect.com/knowledge/hypothesis?hsLang=en) 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](https://doc.sitespect.com/knowledge/origin-experiments?hsLang=en).** 
     - 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**](https://doc.sitespect.com/knowledge/defining-triggers?hsLang=en) as needed, to set specific rules for when the flag should be set.  
       ![](https://doc.sitespect.com/hs-fs/hubfs/image-png-Apr-23-2024-02-58-04-7284-PM.png?width=688&height=301&name=image-png-Apr-23-2024-02-58-04-7284-PM.png)
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](https://doc.sitespect.com/knowledge/creating-an-origin-experiment?hsLang=en).

### 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](https://doc.sitespect.com/knowledge/specifying-a-date-range?hsLang=en).

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

- [Get Started](https://doc.sitespect.com/knowledge/get-started?hsLang=en#main-content)

    - [SiteSpect Support](https://doc.sitespect.com/knowledge/get-started?hsLang=en#sitespect-support)
    - [Overview Information](https://doc.sitespect.com/knowledge/get-started?hsLang=en#overview-information)
    - [Intro to SiteSpect](https://doc.sitespect.com/knowledge/get-started?hsLang=en#intro-to-sitespect)
    - [Account Setup](https://doc.sitespect.com/knowledge/get-started?hsLang=en#account-setup)
    - [Artificial Intelligence](https://doc.sitespect.com/knowledge/get-started?hsLang=en#artificial-intelligence)
- [Dashboard](https://doc.sitespect.com/knowledge/dashboard?hsLang=en)
- [Build](https://doc.sitespect.com/knowledge/build?hsLang=en#main-content)

    - [Setting Up a Campaign](https://doc.sitespect.com/knowledge/build?hsLang=en#setting-up-a-campaign)
    - [Create a Client-Side Campaign](https://doc.sitespect.com/knowledge/build?hsLang=en#create-a-client-side-campaign)
    - [Create a Server-Side Campaign](https://doc.sitespect.com/knowledge/build?hsLang=en#create-a-server-side-campaign)
    - [Metrics](https://doc.sitespect.com/knowledge/build?hsLang=en#metrics)
    - [Audiences](https://doc.sitespect.com/knowledge/build?hsLang=en#audiences)
    - [Triggers](https://doc.sitespect.com/knowledge/build?hsLang=en#triggers)
    - [Variations](https://doc.sitespect.com/knowledge/build?hsLang=en#variations)
    - [Traffic Management](https://doc.sitespect.com/knowledge/build?hsLang=en#traffic-management)
    - [Preview](https://doc.sitespect.com/knowledge/build?hsLang=en#preview)
    - [Factors](https://doc.sitespect.com/knowledge/build?hsLang=en#factors)
    - [Advanced Settings](https://doc.sitespect.com/knowledge/build?hsLang=en#advanced-settings)
    - [Omnichannel](https://doc.sitespect.com/knowledge/build?hsLang=en#omnichannel)
    - [Regular Expressions (Regex)](https://doc.sitespect.com/knowledge/build?hsLang=en#regular-expressions-regex)
    - [WATTS](https://doc.sitespect.com/knowledge/build?hsLang=en#watts)
    - [Recommendations](https://doc.sitespect.com/knowledge/build?hsLang=en#recommendations)
- [Monitor](https://doc.sitespect.com/knowledge/monitor?hsLang=en)
- [Analyze](https://doc.sitespect.com/knowledge/analyze?hsLang=en#main-content)

    - [Data Export and Integration](https://doc.sitespect.com/knowledge/analyze?hsLang=en#data-export-and-integration)
    - [SiteSpect Reports](https://doc.sitespect.com/knowledge/analyze?hsLang=en#sitespect-reports)
- [Site](https://doc.sitespect.com/knowledge/site?hsLang=en#main-content)

    - [Tools](https://doc.sitespect.com/knowledge/site?hsLang=en#tools)
    - [Configuration](https://doc.sitespect.com/knowledge/site?hsLang=en#configuration)
- [Developers](https://doc.sitespect.com/knowledge/developers?hsLang=en#main-content)

    - [API Reference](https://doc.sitespect.com/knowledge/developers?hsLang=en#api-reference)
    - [Code Samples](https://doc.sitespect.com/knowledge/developers?hsLang=en#code-samples)
    - [Tips & Tricks](https://doc.sitespect.com/knowledge/developers?hsLang=en#tips-tricks)
- [Mobile](https://doc.sitespect.com/knowledge/mobile?hsLang=en)
- [Implementation, Deployment, and Security](https://doc.sitespect.com/knowledge/implementation-deployment-and-security?hsLang=en#main-content)

    - [How does SiteSpect work with CDNs?](https://doc.sitespect.com/knowledge/implementation-deployment-and-security?hsLang=en#how-does-sitespect-work-with-cdns)
    - [Single Tenant Implementations](https://doc.sitespect.com/knowledge/implementation-deployment-and-security?hsLang=en#single-tenant-implementations)
    - [Security Features](https://doc.sitespect.com/knowledge/implementation-deployment-and-security?hsLang=en#security-features)
    - [Deployment and Implementation Overview](https://doc.sitespect.com/knowledge/implementation-deployment-and-security?hsLang=en#deployment-and-implementation-overview)
    - [SiteSpect Engine & Admin API](https://doc.sitespect.com/knowledge/implementation-deployment-and-security?hsLang=en#sitespect-engine-admin-api)
- [Third-Party Integration](https://doc.sitespect.com/knowledge/third-party-integration?hsLang=en#main-content)

    - [Google](https://doc.sitespect.com/knowledge/third-party-integration?hsLang=en#google)
    - [Zuko](https://doc.sitespect.com/knowledge/third-party-integration?hsLang=en#zuko)
- [Troubleshooting](https://doc.sitespect.com/knowledge/troubleshooting?hsLang=en)
- [Releases](https://doc.sitespect.com/knowledge/releases?hsLang=en#main-content)

    - [Superfresh](https://doc.sitespect.com/knowledge/releases?hsLang=en#superfresh)
- [Feedback](https://doc.sitespect.com/knowledge/feedback?hsLang=en)

- [A/B Testing](https://www.sitespect.com/testing-and-experimentation/)
- [Personalization](https://www.sitespect.com/personalization-targeting/)
- [Recommendations](https://www.sitespect.com/recommendations/)

[![Monetate-Logo-Full-Color-1](https://doc.sitespect.com/hs-fs/hubfs/Monetate-Logo-Full-Color-1.png?width=200&height=29&name=Monetate-Logo-Full-Color-1.png "Monetate-Logo-Full-Color-1")](http://monetate.com)

<https://www.facebook.com/> <https://www.twitter.com/> <https://www.instagram.com/> <https://podcasts.apple.com/> [mailto:email@email.com](mailto:email@email.com)

Copyright © 2026, Monetate