VercelLogotypeVercelLogotype
LoginSign Up

Feature Flags with Optimizely

Learn how to use optimizely to conduct experiments at the edge.

DeployView Demo

Feature Flags with Optimizely

This example shows how to use Optimizely to conduct experiments using Edge Middleware.

Demo

https://feature-flag-optimizely.vercel.app

How to Use

You can choose from one of the following two methods to use this repository:

One-Click Deploy

Note: Before clicking Deploy, follow the section Set up Optimizely to create an account, set up feature flags and obtain the Optimizely SDK Key.

Clone and Deploy

Execute create-next-app with pnpm to bootstrap the example:

pnpm create-next-app --example https://github.com/vercel/examples/tree/main/edge-middleware/feature-flag-optimizely feature-flag-optimizely

Note: Before running locally, follow the section Set up Optimizely to create an account, set up feature flags and obtain the Optimizely SDK Key.

Create a local environment file from example and add your Optimizely SDK Key.

cp .env.example .env.local

Next, run Next.js in development mode:

pnpm dev

Deploy it to the cloud with Vercel (Documentation).

Optimizely Full Stack Feature Flags and Experimentation

Optimizely Full Stack is a feature flagging and experimentation platform for websites, mobile apps, chatbots, APIs, smart devices, and anything else with a network connection.

You can deploy code behind feature flags, experiment with A/B tests, and roll out or roll back features immediately. All of this functionality is available with minimal performance impact through easy-to-use, open source SDKs.

You can use Optimizely feature flags at the edge with NextJS applications deployed on Vercel. This example uses Optimizely Javascript SDK inside a Edge Middleware as a starting point for you to implement experimentation and feature flagging for your experiences at the edge. For a guide to getting started with our platform more generally, this can be combined with the steps outlined in our Javascript Quickstart.

Identity Management

Out of the box, Optimizely's Full Stack SDKs require a user-provided identifier to be passed in at runtime to drive experiment and feature flag decisions. This example generates a unique id, stores it in a cookie and reuses it to make the decisions sticky. Another common approach would be to use an existing unique identifier available within your application.

Bucketing

For more information on how Optimizely Full Stack SDKs assign users to feature flags and experiments, see the documentation on how bucketing works.

Auto update with Optimizely Webhooks

This example fetches the latest datafile from the Optimizely CDN during every build. Deploy Hooks from Vercel can be used with Optimizely Webhooks to keep the application up to date with the latest changes in the Optimizely project.

Create a Deploy Hook in Vercel

  1. Navigate to Settings tab in your Vercel deployment.
  2. Look for the Deploy Hooks section and create a Hook.
  3. Generate a URL that can be used to trigger a rebuild of the deployment.

Create a Webhook in Optimizely

  1. Follow the instructions here to create a Webhook in your Optimizely project.
  2. Use the Deploy Hook URL Generated from the previous section to create the Optimizely Webhook.

How it works

When a user makes a change to the Optimizely Project using the UI, the Webhook hits Vercel's Deploy Hook URL to trigger a new build on Vercel. Every new build fetches the latest version of the Optimizely datafile and uses it in the application.

Set up Optimizely

Get a free account

You need an account to follow this example. If you do not have an account, you can register for a free account. If you already have an account navigate to your Flags-enabled project.

Get your SDK Key

  1. Go to Settings > Primary Environment
  2. Copy and save the SDK Key for your primary environment. Note: Each environment has its own SDK key.
  3. Go back to the Vercel dashboard, click on your project and go to Settings > Environment Variables
  4. Create a new Environment Variable called OPTIMIZELY_SDK_KEY
  5. Paste your Optimizely SDK Key as its value and Save

Create the feature flag

A feature flag lets you control the users that are exposed to a new feature code in your app. For this example, imagine that you are rolling out a redesigned sorting feature for displaying products.

Create a flag in Optimizely named product_sort and give it a variable named sort_method:

  1. Go to Flags > Create Flag.
  2. Name the flag key product_sort and click Create Flag, which corresponds to the flag key in this example.
  3. Go to Default Variables and click New (+).
  4. Set the variable type to "String".
  5. Name the variable sort_method, which corresponds to the variable key in this example.
  6. Set the variable default value to alphabetical, which represents your old sorting method.
  7. Click Save at the lower right corner to save the variable.
  8. Go to Variations and click the default "on" variation. A variation is a wrapper for a collection of variable values.
  9. Set the sort_method variable value to popular_first, which represents your new sorting method.
  10. Click Save.

Create the flag delivery rule

Make a targeted delivery rule for the "on" variation for the product_sort flag. A targeted delivery lets you gradually release a feature flag to users, with the flexibility to roll it back if you encounter bugs.

  1. Verify that you are in your primary environment (since you are using the primary environment SDK key):

  2. Click Add Rule and select Targeted Delivery.

  3. Set the traffic slider to 50%. This delivers the flag to 50% of everyone who triggers the flag in this environment. You can roll out or roll back the product_sort flag to a percentage of traffic whenever you want.

  4. select On from the Deliver dropdown, .

  5. Click Save.

  6. Enable the flag for your flag rule:

GitHub Repovercel/examples
LicenseView License
Use Cases
Edge Middleware
Stack
Next.js
Tailwind

Related Templates

A/B Testing Simple

By A/B testing at the edge, you'll reduce CLS from client-loaded experiments and improve your site's performance with smaller JS bundles.
A/B Testing Simple thumbnail

Get Started

  • Templates
  • Supported frameworks
  • Marketplace
  • Domains

Build

  • Next.js on Vercel
  • Turborepo
  • v0

Scale

  • Content delivery network
  • Fluid compute
  • CI/CD
  • Observability
  • AI GatewayNew
  • Vercel AgentNew

Secure

  • Platform security
  • Web Application Firewall
  • Bot management
  • BotID
  • SandboxNew

Resources

  • Pricing
  • Customers
  • Enterprise
  • Articles
  • Startups
  • Solution partners

Learn

  • Docs
  • Blog
  • Changelog
  • Knowledge Base
  • Academy
  • Community

Frameworks

  • Next.js
  • Nuxt
  • Svelte
  • Nitro
  • Turbo

SDKs

  • AI SDK
  • Workflow SDKNew
  • Flags SDK
  • Chat SDK
  • Streamdown AINew

Use Cases

  • Composable commerce
  • Multi-tenant platforms
  • Web apps
  • Marketing sites
  • Platform engineers
  • Design engineers

Company

  • About
  • Careers
  • Help
  • Press
  • Legal
  • Privacy Policy

Community

  • Open source program
  • Events
  • Shipped on Vercel
  • GitHub
  • LinkedIn
  • X
  • YouTube

Loading status…

Select a display theme:
    • AI Cloud
      • AI Gateway

        One endpoint, all your models

      • Sandbox

        Isolated, safe code execution

      • Vercel Agent

        An agent that knows your stack

      • AI SDK

        The AI Toolkit for TypeScript

      • v0

        Build applications with AI

    • Core Platform
      • CI/CD

        Helping teams ship 6× faster

      • Content Delivery

        Fast, scalable, and reliable

      • Fluid Compute

        Servers, in serverless form

      • Workflow

        Long-running workflows at scale

      • Observability

        Trace every step

    • Security
      • Bot Management

        Scalable bot protection

      • BotID

        Invisible CAPTCHA

      • Platform Security

        DDoS Protection, Firewall

      • Web Application Firewall

        Granular, custom protection

    • Company
      • Customers

        Trusted by the best teams

      • Blog

        The latest posts and changes

      • Changelog

        See what shipped

      • Press

        Read the latest news

      • Events

        Join us at an event

    • Learn
      • Docs

        Vercel documentation

      • Academy

        Linear courses to level up

      • Knowledge Base

        Find help quickly

      • Community

        Join the conversation

    • Open Source
      • Next.js

        The native Next.js platform

      • Nuxt

        The progressive web framework

      • Svelte

        The web’s efficient UI framework

      • Turborepo

        Speed with Enterprise scale

    • Use Cases
      • AI Apps

        Deploy at the speed of AI

      • Composable Commerce

        Power storefronts that convert

      • Marketing Sites

        Launch campaigns fast

      • Multi-tenant Platforms

        Scale apps with one codebase

      • Web Apps

        Ship features, not infrastructure

    • Tools
      • Marketplace

        Extend and automate workflows

      • Templates

        Jumpstart app development

      • Partner Finder

        Get help from solution partners

    • Users
      • Platform Engineers

        Automate away repetition

      • Design Engineers

        Deploy for every idea

  • Enterprise
  • Pricing
Log InContact
Sign Up
Sign Up
DeployView Demo
AI Gateway

One endpoint, all your models

Sandbox

Isolated, safe code execution

Vercel Agent

An agent that knows your stack

AI SDK

The AI Toolkit for TypeScript

v0

Build applications with AI

CI/CD

Helping teams ship 6× faster

Content Delivery

Fast, scalable, and reliable

Fluid Compute

Servers, in serverless form

Workflow

Long-running workflows at scale

Observability

Trace every step

Bot Management

Scalable bot protection

BotID

Invisible CAPTCHA

Platform Security

DDoS Protection, Firewall

Web Application Firewall

Granular, custom protection

Customers

Trusted by the best teams

Blog

The latest posts and changes

Changelog

See what shipped

Press

Read the latest news

Events

Join us at an event

Docs

Vercel documentation

Academy

Linear courses to level up

Knowledge Base

Find help quickly

Community

Join the conversation

Next.js

The native Next.js platform

Nuxt

The progressive web framework

Svelte

The web’s efficient UI framework

Turborepo

Speed with Enterprise scale

AI Apps

Deploy at the speed of AI

Composable Commerce

Power storefronts that convert

Marketing Sites

Launch campaigns fast

Multi-tenant Platforms

Scale apps with one codebase

Web Apps

Ship features, not infrastructure

Marketplace

Extend and automate workflows

Templates

Jumpstart app development

Partner Finder

Get help from solution partners

Platform Engineers

Automate away repetition

Design Engineers

Deploy for every idea