Skip to main content

Show different information to different users

We'll guide you to use FeatureProbe platform to create a custom feature-controlled toggle. Then we write a back-end program to verify that the back-end program requests the FeatureProbe SDK with different user information, and will get different results.

Suppose we want to implement the following scenario:

  • For "VIP" users of "Shanghai", it will display "Welcome VIP customers from Shanghai"
  • For "Beijing" users (without distinguishing levels), display "Welcome customers from Beijing"
  • For users who do not meet the above two conditions, display "Welcome"
  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 +Toggle to create a new toggle.
  • Fill in the name and key.
  • Select the string type as the return variation type.
  • Add three variations, fill in the name and value respectively, the value is the three different prompts we want to return.
  • Select a variation when toggle is disabled.
  • Click Create and publish.

create toggle

  1. Click to enter the targeting page of the newly created tutorial_variation toggle
  • Click + Add rule to add a new rule
  • Add two conditions to the rules, the city is shanghai, and the rank is VIP
  • The return value is selected as variation1 (that is, the value is: welcome VIP customers from Shanghai)

rule one

  • Click + Add Rule again
  • Add a condition to the rule, the city is Beijing, and the return variation is selected as variation2

rule two

  • The return variation when no rules is matched is selected as variation3

default

  • Change the toggle status to enabled, click publish below

publish

  • Click Confirm to release the toggle.

confirm

  1. At this point, you should see the prompt of Published successfully

publish success

The operation on the platform is over here, the toggle has been successfully created and can be accessed. Below we introduce how to get these variation values by SDK.

Control the back-end program

We provide a backend code example, from which you can start to experience how the backend code uses the toggle.

Write code

  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 projects page, you can click Projects on the toggle targeting page to open: project

  2. Copy Server SDK Key sdk 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 = // Fill in the server SDK key
  1. Add the following code to simulate five users accessing this toggle
    1. VIP users from Shanghai
    2. Gold users form Shanghai
    3. VIP users form Beijing
    4. Gold users form Beijing
    5. VIP users form Tianjin
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("city", "Shanghai").with("rank", "VIP"),
new FPUser().with("city", "Shanghai").with("rank", "Gold"),
new FPUser().with("city", "Beijing").with("rank", "VIP"),
new FPUser().with("city", "Beijing").with("rank", "Gold"),
new FPUser().with("city", "Tianjin").with("rank", "VIP"),
};

for (FPUser user:users) {
String greeting = fpClient.stringValue("tutorial_variation", user, "Welcome");
System.out.println(greeting);
}

fpClient.close();
}
tip

The user's attributes (for example: city, rank) need to be obtained by the developer in the business code in other ways, and then passed to the FeatureProbe SDK through the with method. The SDK is only responsible for applying rules to these users, The SDK cannot infer the user's attributes internally by itself.

  1. Run the program
mvn package
java -jar ./target/server-sdk-java-1.4.0.jar

Validate result

As can be seen from the command line log, for the following five test users:

  1. VIP users from Shanghai
  2. Gold users form Shanghai
  3. VIP users form Beijing
  4. Gold users form Beijing
  5. VIP users form Tianjin

apply rules

  • For "VIP" users of "Shanghai", it will display "Welcome VIP customers from Shanghai"
  • For "Beijing" users (without distinguishing levels), display "Welcome customers from Beijing"
  • For users who do not meet the above two conditions, display "Welcome"

After that, the following results were obtained:

Welcome VIP customers from Shanghai
welcome
Welcome customers from Beijing
Welcome customers from Beijing
welcome

You can change the code, try more combinations of user attributes, and see if the results displayed in the log meet the conditions.

Control the front-end program

We provide a front-end js code example, and you can start to experience how the front-end code uses toggles from here.

Write code

  1. Download the sample code
git clone https://github.com/FeatureProbe/client-sdk-js.git
cd client-sdk-js
  1. Open platform to get client sdk key
info

Click the "Projects" tab to enter the "Projects" list, obtain various SDK keys, and modify project and environment information.

client sdk key

  1. Open example/index.html and fill in Client SDK Key and FeatureProbe URL ("https://featureprobe.io/server")
example/index.html
const fpClient = new featureProbe. FeatureProbe({
remoteUrl: "https://featureprobe.io/server",
clientSdkKey: // Paste client sdk key here,
user,
refreshInterval: 5000,
});
  1. Simulate the "VIP" user from "Shanghai" to access the toggle tutorial_variation and get the toggle result directly
example/index.html
<script>
const user = new featureProbe.FPUser().with("city", "Shanghai").with("rank", "VIP");

const fpClient = new featureProbe. FeatureProbe({
remoteUrl: "https://featureprobe.io/server",
clientSdkKey: // Paste client sdk key here,
user,
refreshInterval: 5000,
});

fpClient.start();
fpClient.on("ready", function() {
const stringValue = fpClient.stringValue("tutorial_variation", "Welcome");
document.getElementById("string-result").innerText = stringValue;
});
</script>

Validate result

Open index.html in the browser, and you can see that for the current "Welcome VIP customers from Shanghai".

string type
FeatureProbe evaluation string type toggle result is : Welcome VIP customers from Shanghai

You can go back to the index.html file and update the with parameter to see the difference in the return variation value.

example/index.html
const user = new featureProbe.FPUser().with("city", "Beijing").with("rank", "VIP");