App Lifecycle Manager feature flag allocations let you split traffic across
variants to run A/B tests and evaluate feature impact across user cohorts.
You set percentage weights and a randomization attribute such as `userID` to
serve a consistent experience and measure results.

To simplify tracking in your application code and analytics pipelines, assign
descriptive string IDs such as `experimental` and `baseline` to each variant.

## Prerequisites

Before you begin, ensure you have:

1. Completed the [Deploy feature flags](https://docs.cloud.google.com/app-lifecycle-manager/flags/flags-console-quickstart.md.txt), [Deploy with the gcloud CLI](https://docs.cloud.google.com/app-lifecycle-manager/flags/flags-quickstart.md.txt), or [Use feature flags standalone](https://docs.cloud.google.com/app-lifecycle-manager/flags/flags-standalone-quickstart.md.txt) quickstart.
2. A Google Cloud CLI environment configured to manage App Lifecycle Manager resources.

## Configure a feature flag allocation

To configure an experimental feature flag:

1. Define the randomized attribute (e.g., `userID`) which will be used for
   sticky bucketing to ensure a consistent experience for each user.

   ```bash
     gcloud beta app-lifecycle-manager flags attributes create "user-id-attr" \
         --key="userID" \
         --attribute-value-type="STRING" \
         --location=global
   ```
2. Use an allocation to define the split (50/50, for example), referencing
   your descriptive variant IDs.

   ```bash
   # Create a flag with explicitly named variants for the experiment and a 50/50 allocation referencing the custom IDs
   gcloud beta app-lifecycle-manager flags create "search-algo-test" \
     --key="search-algo-test" \
     --flag-value-type=BOOL \
     --location="global" \
     --unit-kind="demo-test-unitkind" \
     --variants='[
         {
         "id": "experimental",
         "booleanValue": true
         },
         {
         "id": "baseline",
         "booleanValue": false
         }
     ]'\
     --evaluation-spec='{
         "allocations": [{
             "id": "search-split-50-50",
             "randomizedOn": "userID",
             "slots": [
             {"variant": "baseline", "weight": 50},
             {"variant": "experimental", "weight": 50}
             ]
         }],
         "defaultAllocation": "search-split-50-50",
         "attributes": ["projects/PROJECT_ID/locations/global/flagAttributes/user-id-attr"]
     }'
   ```
3. Create a `FlagRelease` and deploy it to your units using a `Rollout`:

   ```bash
   gcloud beta app-lifecycle-manager flags releases create "search-algo-experiment-v1" \
       --location="global" \
       --unit-kind="demo-test-unitkind" \
       --flags="search-algo-test"

   gcloud beta app-lifecycle-manager rollout-kinds create "rollout-kind-of-search-algo" \
       --unit-kind="demo-test-unitkind" \
       --rollout-orchestration-strategy="Google.Cloud.Simple.AllAtOnce" \
       --location="global"

   gcloud beta app-lifecycle-manager rollouts create "rollout-search-algo-experiment" \
       --flag-release="search-algo-experiment-v1" \
       --rollout-kind="rollout-kind-of-search-algo" \
       --location="global"
   ```
4. In your backend service, initialize the OpenFeature SDK and inject the
   `userID` into the evaluation context. Use the `BooleanValueDetails`
   (or equivalent for your language) method to retrieve the `variantID` (string)
   in your application. This lets you switch your backend logic based on the
   descriptive name rather than only a boolean value.

   ```go
   // 1. Prepare evaluation context
   evalCtx := openfeature.NewEvaluationContext(
       currentUser.ID,
       map[string]any{"userID": currentUser.ID},
   )

   // 2. Fetch evaluation details to get the variant name
   details, err := client.BooleanValueDetails(ctx, "search-algo-test", false, evalCtx)

   // 3. Execute logic based on the Variant ID (string name)
   if details.Variant == "experimental" {
       results = search.ExperimentalV2(query)
   } else {
       results = search.BaselineV1(query)
   }
   ```
5. Use descriptive variant names so that your auditing and analysis generate
   documentation automatically. To audit evaluations, log the `details.Variant`
   string alongside your performance metrics:

   ```go
    startTime := time.Now()
    // ... perform search ...
    duration := time.Since(startTime)

    // Audit: Log the descriptive variant name ("experimental" or "baseline")
    logger.Info("Search performed",
       "variant", details.Variant,
       "latency_ms", duration.Milliseconds(),
    )
   ```

   By comparing metrics for the "Experimental" group versus the
   "Baseline" group, you can manually analyze whether the new algorithm
   improves backend efficiency or search relevance before proceeding with a
   full rollout.

## What's next

- Learn about [Troubleshooting](https://docs.cloud.google.com/app-lifecycle-manager/flags/flags-troubleshooting.md.txt).