Skip to main content

Use segment

Commonly used target rules can be set up as segment, which can be shared between toggles.

We'll guide you to use FeatureProbe's platform to create a segment and then use the segment in two toggles. And through a back-end program, the modification of the segment will take effect in the two toggles at the same time.

Suppose we want to implement the following scenario:

  • There is a whitelist of QAs emails that needs to be used in multiple toggles. so that QAs can test multiple toggles.
  • This whitelist of QAs needs to be modified uniformly, and the new QA added can take effect in multiple toggles.

Create a segment on the platform

  1. Log in to the FeatureProbe demo platform. If you log in for the first time, please enter your email address. You can continue to use your email to access your data in the future.
  2. Click Segments/+Segment to create a new segment.

create segment

  1. Fill in the name and key, click Create and publish.

publish segment

  1. Click the newly created segment from the list to enter editing.

segment_list_cn.png

  1. Add a rule, select the type, enter the attribute email,
  • Select string type
  • Select is one of operator
  • Then enter the emails of the two QAs
  • Click Publish

tutorial_segment_detail_en.png

  1. There is no toggle to use this "segment", click "next" and "confirm" to change.

tutorial_segment_publish_next_step_en.png

Use segment in toggle

Next, we come to Toggles list, create two toggles using the segment qa_email created above.

  1. Create a toggle feature1 with a return variation value of boolean type, use the default configuration, and then click Create and publish.

tutorial_toggle_create_use_segment_en.png

  1. Enter the targeting page of the toggle feature1, change the status to enabled, click + Add Rule, and select the segment type.

tutorial_toggle_use_segment_add_rule_en.png

  1. Edit rules
  • Select is in segments
  • Select the QA Email segment
  • Set the return variation value variation2 for the segment
  • Other return rules set the return variation value variation1

tutorial_toggle_use_segment_rule_detail_en.png

  1. Publish toggle feature1.
  2. Repeat steps 1-4 above to create another toggle feature2 using the same segment.

Validate segment

Backend code writing

  1. According to the language you are familiar with, download and open the corresponding back-end sample code.
git clone https://github.com/FeatureProbe/server-sdk-java.git
cd server-sdk-java

Open src/main/java/com/featureprobe/sdk/example/FeatureProbeDemo.java file with an editor.

  1. Open the FeatureProbe platform project list page, you can click Projects on the toggle details page to open project
  2. Copy Server SDK Keysdk key
  3. Fill in Server SDK Key and FeatureProbe remote URL ("https://featureprobe.io/server") into the corresponding variables of the backend code
src/main/java/com/featureprobe/sdk/example/FeatureProbeDemo.java
private static final String FEATURE_PROBE_SERVER_URL = "https://featureprobe.io/server";
private static final String FEATURE_PROBE_SERVER_SDK_KEY = // 填入 服务端SDK密钥 ;
  1. Add the following code to simulate three users with email attributes accessing these 2 toggles.
src/main/java/com/featureprobe/sdk/example/FeatureProbeDemo.java

public static void main(String[] args) throws IOException {

Logger root = (Logger)LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
root.setLevel(Level.WARN);

final FPConfig config = FPConfig.builder()
.remoteUri(FEATURE_PROBE_SERVER_URL)
.build();

// Init FeatureProbe, share this FeatureProbe instance in your project.
final FeatureProbe fpClient = new FeatureProbe(FEATURE_PROBE_SERVER_SDK_KEY, config);

FPUser[] users = {
new FPUser().with("email", "tester_a@company.com"),
new FPUser().with("email", "tester_b@company.com"),
new FPUser().with("email", "tester_c@company.com"),
};

for (FPUser user:users) {
if (fpClient.boolValue("feature1", user, false)) {
System.out.println(user.getAttr("email") + " see the new feature1");
} else {
System.out.println(user.getAttr("email") + " see nothing");
}
}

for (FPUser user:users) {
if (fpClient.boolValue("feature2", user, false)) {
System.out.println(user.getAttr("email") + " see the new feature2");
} else {
System.out.println(user.getAttr("email") + " see nothing");
}
}

fpClient.close();
}

tip

In addition to the user attributes explicitly used in the rules of toggle, which need to be passed to the SDK through the with method, the user attributes (for example: email) that need to be used in the segment used in toggle also need to be passed to the FeatureProbe SDK through the with method.

Run the code

Run the program.

mvn package
java -jar ./target/server-sdk-java-1.4.0.jar

Check the log verification results, you can see that the two test emails (tester_a and tester_b) in the segment can see the two new features, but the email (tester_c) not in the segment cannot see them.

tester_a@company.com see the new feature1
tester_b@company.com see the new feature1
tester_c@company.com see nothing
tester_a@company.com see the new feature2
tester_b@company.com see the new feature2
tester_c@company.com see nothing

Update segment

Next, let's update the rules of the segment, and then verify that the updated results can take effect on the two toggles at the same time.

Update segments on the page

  1. Enter the edit page of the segment qa_email.
  2. Delete email account test_b and add email account test_c.

tutorial_segment_after_update_en.png

  1. Publish segment.

Rerun the program to see the result

Run the program again according to the above operation method to view the log

tester_a@company.com see the new feature1
tester_b@company.com see nothing
tester_c@company.com see the new feature1
tester_a@company.com see the new feature2
tester_b@company.com see nothing
tester_c@company.com see the new feature2

You can see that the modification has taken effect on both toggles.