Understanding targeting
LaunchDarkly uses the term targeting to describe configuring a flag so that it evaluates differently depending on its context. This is useful when the flag is controlling code that should be run only in specific conditions.
Here are some example uses for targeting:
- Show a website feature only when the visitor is using a specific browser
- Provide a user the option to upgrade their account when you know that their account is eligible
- Show the user a different greeting depending on the time of day in their timezone
To set up targeting in a flag, you need to provide context in the code, and rules in the flag configuration. Let's look at each of those in more depth.
Understanding Context
When you first added LaunchDarkly to the game code, you pasted these lines:
// Create the LaunchDarkly user object
var lduser = {"key": "user123"};
// Create the LaunchDarkly client, with the user object
const ldclient = LDClient.initialize('YOUR_CLIENT_SIDE_ID', lduser);
This code first creates an object named lduser
. The code then uses that object as a context by providing it to the LDClient.initialize()
constructor.
Context objects contain attributes, each of which has a name and a value. In this code snippet, the context only contains one attribute named key
. The key
is the standard attribute that LaunchDarkly uses as an ID for a context, so in this case, this context has the ID user123
.
LaunchDarkly lets you add add as many custom attributes as you like, and then use those attributes for your targeting rules. Attribute values can be either single strings (such as hello there
) or lists of strings (such as ["this", "list", "right", "here"]
).
In this lesson you will use one attribute, key
. But first, there's one more code change to make before using targeting.