Affected: REST API
Overview
This article explains how to enable detailed event tracking (trackEvents
) for multiple feature flags in a project using the LaunchDarkly REST API.
When you enable the Send detailed events to data export destinations setting for a new environment, trackEvents
is automatically enabled for newly created flags in that environment. However, this setting does not apply retroactively to existing flags, and bulk updates are not currently supported in the LaunchDarkly UI.
For projects with many existing flags, manually updating each flag can be time-consuming. You can automate this process using the REST API to save time and ensure consistent event tracking across all flags.
Solution
To enable trackEvents
across multiple flags, follow these steps using the REST API:
- List all feature flags using
GET /flags/{projectKey}
to retrieve the flags in your project. - Enable detailed event tracking by iterating through each flag and using
PATCH /flags/{projectKey}/{featureFlagKey}
to settrackEvents
to true for each flag in the desired environment.
Example Bash script to enable trackEvents
for all flags in production
:
#!/bin/bash
# Variables
API_KEY="YOUR_API_KEY_HERE"
PROJECT_KEY="your-project-key"
BASE_URL="https://app.launchdarkly.com/api/v2"
# List all flags
FLAGS=$(curl -s -i -X GET \
"$BASE_URL/flags/$PROJECT_KEY" \
-H "Authorization: $API_KEY" | jq -r '.items[] | .key')
# Enable trackEvents for each flag in the production environment
for FLAG_KEY in $FLAGS; do
PATCH_PAYLOAD='[
{
"op": "replace",
"path": "/environments/production/trackEvents",
"value": true
}
]'
# Make PATCH request to update the flag
RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" -X PATCH "$BASE_URL/flags/$PROJECT_KEY/$FLAG_KEY" \
-H "Authorization: $API_KEY" \
-H "Content-Type: application/json" \
-d "$PATCH_PAYLOAD")
if [ "$RESPONSE" -eq 200 ]; then
echo "trackEvents enabled for flag: $FLAG_KEY"
else
echo "Failed to update flag: $FLAG_KEY (HTTP $RESPONSE)"
fi
done
To verify the updates, call the GET /flags/{projectKey}
endpoint again and inspect each flag’s environments.{envKey}.trackEvents
value.