|
| 1 | +# LaunchDarkly Flags SDK Example |
| 2 | + |
| 3 | +This example uses [LaunchDarkly](https://vercel.com/marketplace/launchdarkly) for feature flags with the [Flags SDK](https://flags-sdk.dev) along with the `@flags-sdk/launchdarkly` [LaunchDarkly adapter](https://flags-sdk.dev/docs/api-reference/adapters/launchdarkly) and the [Flags Explorer](https://vercel.com/docs/workflow-collaboration/feature-flags/using-vercel-toolbar). |
| 4 | + |
| 5 | +## Demo |
| 6 | + |
| 7 | +TODO |
| 8 | + |
| 9 | +## How it works |
| 10 | + |
| 11 | +This demo uses two feature flags on LaunchDarkly to control the visibility of two banners on the page. |
| 12 | +Both flags are configured to show/hide each banner 50% of the time. |
| 13 | + |
| 14 | +Once you visit the page, you can see a variation of both/one/none of the banners. |
| 15 | +Since this example is using a stable id to identify users, you will see the same variation until you reset your id. |
| 16 | + |
| 17 | +To test different variations, you can use the Dev Tools at the bottom to reset the stable id and reload the page. |
| 18 | +This allows you to test different variations of the banners. |
| 19 | + |
| 20 | +If you deployed your own and configured the feature flags on LaunchDarkly, you can also use the [Flags Explorer](https://vercel.com/docs/workflow-collaboration/feature-flags/using-vercel-toolbar) to test different variations by creating overrides. |
| 21 | + |
| 22 | +## Deploy this template |
| 23 | + |
| 24 | +The easiest way to get started with LaunchDarkly is through the integration in [Vercel Marketplace](https://vercel.com/marketplace/launchdarkly). |
| 25 | + |
| 26 | +[](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fvercel%2Fexamples%2Ftree%2Fmain%2Fflags-sdk%2Flaunchdarkly&env=FLAGS_SECRET&envDescription=The+FLAGS_SECRET+will+be+used+by+the+Flags+Explorer+to+securely+overwrite+feature+flags.+Must+be+32+random+bytes%2C+base64-encoded.+Use+the+generated+value+or+set+your+own.&envLink=https%3A%2F%2Fvercel.com%2Fdocs%2Fworkflow-collaboration%2Ffeature-flags%2Fsupporting-feature-flags%23flags_secret-environment-variable&project-name=launchdarkly-flags-sdk&repository-name=launchdarkly-flags-sdk) |
| 27 | + |
| 28 | +### Step 1: Link the project |
| 29 | + |
| 30 | +In order to use the Flags Explorer, you need to link the project on your local machine. |
| 31 | + |
| 32 | +```bash |
| 33 | +vercel link |
| 34 | +``` |
| 35 | + |
| 36 | +Select the project from the list you just deployed. |
| 37 | + |
| 38 | +### Step 2: Pull all environment variables |
| 39 | + |
| 40 | +This allows the Flags SDK and the Flags Explorer to work correctly, by getting additional metadata. |
| 41 | + |
| 42 | +```bash |
| 43 | +vercel env pull |
| 44 | +``` |
| 45 | + |
| 46 | +### Step 3: Create Feature Flags |
| 47 | + |
| 48 | +Head over to the [LaunchDarkly Console](https://app.launchdarkly.com) and create the feature flags and experiments required by this template. |
| 49 | + |
| 50 | +Be sure to enable the `SDKs using Client-side ID` option for each Feature Flag. |
| 51 | + |
| 52 | +Feature Flags: |
| 53 | + |
| 54 | +- `Summer Sale` (type boolean) with the key `summer-sale` and the variations `true` and `false`. Edit the default targeting rule to serve a percentage rollout with a 50/50 split by `user.key`. |
| 55 | +- `Free Delivery` (type boolean) with the key `free-delivery` and the variations `true` and `false`. Edit the default targeting rule to serve a percentage rollout with a 50/50 split by `user.key`. |
| 56 | +- `Proceed to Checkout` (type string) with the key `proceed-to-checkout` and the following variations: |
| 57 | + - Name: `Control`, Value: `blue` |
| 58 | + - Name: `Test`, Value: `green` |
| 59 | + - Name: `Test #2`, Value: `red` |
| 60 | + |
| 61 | +You can also find the flag keys in the `flags.ts` file. |
| 62 | + |
| 63 | +Ensure all Flags are ON. |
| 64 | + |
| 65 | +### Step 4: Configure the Experiment |
| 66 | + |
| 67 | +Create the `Proceed to Checkout` experiment: |
| 68 | + |
| 69 | +- Name: `Proceed to Checkout` |
| 70 | +- Hypothesis: `Button color influences rate at which customers proceed to checkout` |
| 71 | +- Type: `Feature change` |
| 72 | +- Randomization Unit: `user` |
| 73 | +- Randomization Attribute: `key` |
| 74 | +- Metric: Create a new metric: |
| 75 | + - Event kind: `Custom` |
| 76 | + - Event key: `proceed-to-checkout-clicked` |
| 77 | + - What do you want to measure? `Occurence (conversion: binary)` |
| 78 | + - Metric definition: `Percentage of user units that sent the event, where higher is better` |
| 79 | + - Metric name: `Proceed to Checkout Clicked` |
| 80 | +- Varaiations: Choose flag `proceed-to-checkout` |
| 81 | +- Audience: |
| 82 | + - In this experiment: Choose `Custom` and enter `100`% |
| 83 | + - Split audience: Choose `Split equally` |
| 84 | +- Statistical approach: Default values |
| 85 | + |
| 86 | +After that, start the Experiment. |
| 87 | + |
| 88 | +### Step 6: Set environment variables |
| 89 | + |
| 90 | +See `.env.example` for a template. |
| 91 | + |
| 92 | +- [`FLAGS_SECRET`](https://vercel.com/docs/feature-flags/flags-explorer/reference#flags_secret-environment-variable) |
| 93 | +- `EDGE_CONFIG` (Vercel Edge Config connection string) |
| 94 | +- `LAUNCHDARKLY_PROJECT_SLUG` |
| 95 | +- `LAUNCHDARKLY_CLIENT_SIDE_ID` |
| 96 | +- `NEXT_PUBLIC_LAUNCHDARKLY_CLIENT_SIDE_ID` (set to same value as `LAUNCHDARKLY_CLIENT_SIDE_ID`) |
| 97 | + |
| 98 | +_(Optional)_ If you provide the `LAUNCHDARKLY_API_KEY`, `LAUNCHDARKLY_PROJECT_KEY` and `LAUNCHDARKLY_ENVIRONMENT` environment variables, the Flags Explorer will fetch additional metadata from the LaunchDarkly API. |
| 99 | + |
| 100 | +This will show the description (if set) and displays a link to the feature flag on the LaunchDarkly Console. |
| 101 | + |
| 102 | +You can create an API key and find project and environment values in the [LaunchDarkly Console](https://app.launchdarkly.com/settings/projects). |
0 commit comments