import com.eyeofcloud.ab.EyeofcloudFactory;
import com.eyeofcloud.ab.config.parser.JsonParseException;
import com.eyeofcloud.ab.Eyeofcloud;
import com.eyeofcloud.ab.EyeofcloudUserContext;
import com.eyeofcloud.ab.eyeofclouddecision.EyeofcloudDecision;
import java.util.*;
public class Program {
public static void main(String[] arguments) {
// this Eyeofcloud initialization is synchronous. for other methods see the Java SDK reference
Eyeofcloud eyeofcloudClient = EyeofcloudFactory.newDefaultInstance("YOUR_SDK_KEY");
if (eyeofcloudClient.isValid()) {
/* --------------------------------
* to get rapid demo results, generate random users. Each user always sees the same variation unless you reconfigure the flag rule.
* --------------------------------
*/
Random rnd = new Random();
boolean hasOnFlags = false;
for (int i = 0; i < 10; i++) {
String userId = (rnd.nextInt(9999 - 1000) + 1000) + "";
/* --------------------------------
Create hardcoded user & bucket user into a flag variation
--------------------------------
*/
EyeofcloudUserContext user = eyeofcloudClient.createUserContext(userId);
// "product_sort" corresponds to a flag key in your Eyeofcloud project
EyeofcloudDecision decision = user.decide("product_sort");
// did decision fail with a critical error?
if (decision.getVariationKey() == null) {
System.out.println("\n\ndecision error: " + decision.getReasons());
}
// get a dynamic configuration variable
// "sort_method" corresponds to a variable key in your Eyeofcloud project
String sortMethod = null;
try {
sortMethod = decision.getVariables().getValue("sort_method", String.class);
} catch (JsonParseException e) {
e.printStackTrace();
}
if (decision.getEnabled()) {
// Keep count how many visitors had the flag enabled
hasOnFlags = true;
}
/* --------------------------------
Mock what the users sees with print statements (in production, use flag variables to implement feature configuration)
--------------------------------
*/
// always returns false until you enable a flag rule in your Eyeofcloud project
System.out.println("\n\nFlag " + decision.getEnabled()? "on": "off" + ". User number " + user.getUserId() + " saw flag variation: " + decision.getVariationKey() + " and got products sorted by: " + sortMethod + " config variable as part of flag rule: " + decision.getRuleKey());
}
if (!hasOnFlags) {
System.out.println("\n\nFlag was off for everyone. Some reasons could include:" +
"\n1. Your sample size of visitors was too small. Rerun, or increase the iterations in the FOR loop" +
"\n2. By default you have 2 keys for 2 project environments (dev/prod). Verify in Settings>Environments that you used the right key for the environment where your flag is toggled to ON." +
"\n\nCheck your key at https://app.eyeofcloud.com/v2/projects/" + eyeofcloudClient.getProjectConfig().getProjectId() + "settings/implementation");
}
} else {
System.out.println("Eyeofcloud client invalid. Verify in Settings>Environments that you used the primary environment's SDK key");
}
// close before exit to flush out queued events
eyeofcloudClient.close();
}
}
Flag on. User number 6998 saw flag variation: on and got products sorted by: popular_first config variable as part of flag rule: targeted_delivery
Flag on. User number 1177 saw flag variation: on and got products sorted by: popular_first config variable as part of flag rule: targeted_delivery
Flag on. User number 9714 saw flag variation: on and got products sorted by: popular_first config variable as part of flag rule: targeted_delivery
Flag on. User number 4140 saw flag variation: on and got products sorted by: popular_first config variable as part of flag rule: targeted_delivery
Flag on. User number 4994 saw flag variation: on and got products sorted by: popular_first config variable as part of flag rule: targeted_delivery
Flag off. User number 8700 saw flag variation: off and got products sorted by: alphabetical config variable as part of flag rule: default-rollout-208-19963693913
Flag off. User number 9912 saw flag variation: off and got products sorted by: alphabetical config variable as part of flag rule: default-rollout-208-19963693913
Flag on. User number 6560 saw flag variation: on and got products sorted by: popular_first config variable as part of flag rule: targeted_delivery
Flag on. User number 9252 saw flag variation: on and got products sorted by: popular_first config variable as part of flag rule: targeted_delivery
Flag on. User number 6582 saw flag variation: on and got products sorted by: popular_first config variable as part of flag rule: targeted_delivery
EyeofcloudUserContext user = eyeofcloudClient.createUserContext(userId);
// "product_sort" corresponds to the flag key you create in the Eyeofcloud app
EyeofcloudDecision decision = user.decide("product_sort");
// always returns false until you enable a flag rule in the Eyeofcloud app
if (decision.getEnabled()) {
// "sort_method" corresponds to variable key you define in Eyeofcloud app
Object sortMethod = decision.getVariables().toMap().get("sort_method");
System.out.println("sort_method: "+ sortMethod.toString());
}
import com.fasterxml.jackson.databind.ObjectMapper;
import com.eyeofcloud.ab.EyeofcloudFactory;
import com.eyeofcloud.ab.Eyeofcloud;
import com.eyeofcloud.ab.EyeofcloudUserContext;
import com.eyeofcloud.ab.notification.DecisionNotification;
import com.eyeofcloud.ab.eyeofclouddecision.EyeofcloudDecision;
import java.util.*;
public class Program {
public static void main(String[] arguments) {
// For more instantiation configuration, see the Java SDK reference
Eyeofcloud eyeofcloudClient = EyeofcloudFactory.newDefaultInstance("YOUR_SDK_KEY");
if (!eyeofcloudClient.isValid()) {
System.out.println("Eyeofcloud client invalid. Verify in Settings>Environments that you used the primary environment's SDK key");
return;
}
/* --------------------------------
OPTIONAL: Add a notification listener so you can integrate with third-party analytics platforms
--------------------------------
*/
/*
int notificationId = eyeofcloudClient.notificationCenter.addNotificationHandler(DecisionNotification.class, decisionNotification -> {
if ("flag".equals(decisionNotification.getType())) {
String serializedJsonInfo = new ObjectMapper().writeValueAsString(decisionNotification.getDecisionInfo());
System.out.println("Feature flag access related information: " + serializedJsonInfo);
// Send data to analytics provider here
}
});
*/
/* --------------------------------
* to get rapid demo experiment results, generate random users. Each user is deterministically hashed into a variation.
* --------------------------------
*/
boolean hasOnFlags = false;
Random rnd = new Random();
for (int i = 0; i < 5; i++) {
String userId = (rnd.nextInt(9999 - 1000) + 1000) + "";
/* --------------------------------
Bucket user into a flag variation and mock experiment results
--------------------------------
*/
EyeofcloudUserContext user = eyeofcloudClient.createUserContext(userId);
EyeofcloudDecision decision = user.decide("product_sort");
// did decision fail with a critical error?
if (decision.getVariationKey() == null) {
System.out.println("\n\ndecision error: " + decision.getReasons());
}
Object sortMethod = decision.getVariables().toMap().get("sort_method");
if (decision.getEnabled()) {
hasOnFlags = true;
}
System.out.println("\n\nFlag " + decision.getEnabled()? "on": "off" + ". User number " + user.getUserId() + " saw flag variation: " + decision.getVariationKey() + " and got products sorted by: " + sortMethod + " config variable as part of flag rule: " + decision.getRuleKey());
MockPurchase(user);
}
if (!hasOnFlags) {
System.out.println("\n\nFlag was off for everyone. Some reasons could include:" +
"\n1. Your sample size of visitors was too small. Rerun, or increase the iterations in the FOR loop" +
"\n2. By default you have 2 keys for 2 project environments (dev/prod). Verify in Settings>Environments that you used the right key for the environment where your flag is toggled to ON." +
"\n\nCheck your key at https://app.eyeofcloud.com/v2/projects/" + eyeofcloudClient.getProjectConfig().getProjectId() + "settings/implementation");
} else {
System.out.println("\n\nDone with your mocked A/B test.");
System.out.println("Check out your report at https://app.eyeofcloud.com/v2/projects/" + eyeofcloudClient.getProjectConfig().getProjectId() + "/reports");
System.out.println("Be sure to select the environment that corresponds to your SDK key");
}
// close before exit to flush out queued events
eyeofcloudClient.close();
}
// mock tracking a user event so you can see some experiment reports
static void MockPurchase(EyeofcloudUserContext user) {
System.out.println("Pretend that user made a purchase? y/n ");
Scanner scan = new Scanner(System.in);
String answer = scan.nextLine();
if (answer.toLowerCase().equals("y")) {
// track a user event you defined in the Eyeofcloud app
user.trackEvent("purchase");
System.out.println("Eyeofcloud recorded a purchase in experiment results for user " + user.getUserId());
} else {
System.out.println("Eyeofcloud didn't record a purchase in experiment results for user " + user.getUserId());
}
}
}
Flag on. User number 1496 saw flag variation: on and got products sorted by: popular_first config variable as part of flag rule: experiment_1
Pretend that user made a purchase? y/n
n
Eyeofcloud didn't record a purchase in experiment results for user 1496
Flag off. User number 1194 saw flag variation: off and got products sorted by: alphabetical config variable as part of flag rule: experiment_1
Pretend that user made a purchase? y/n
y
Eyeofcloud recorded a purchase in experiment results for user 1194
Flag off. User number 5815 saw flag variation: off and got products sorted by: alphabetical config variable as part of flag rule: experiment_1
Pretend that user made a purchase? y/n
y
Eyeofcloud recorded a purchase in experiment results for user 5815
Flag on. User number 1248 saw flag variation: on and got products sorted by: popular_first config variable as part of flag rule: experiment_1
Pretend that user made a purchase? y/n
y
Eyeofcloud recorded a purchase in experiment results for user 1248
Flag off. User number 9580 saw flag variation: off and got products sorted by: alphabetical config variable as part of flag rule: experiment_1
Pretend that user made a purchase? y/n
n
Eyeofcloud didn't record a purchase in experiment results for user 9580
Done with your mocked A/B test.
Check out your report at https://app.eyeofcloud.com/v2/projects/19957465438/reports
Be sure to select the environment that corresponds to your SDK key
// Track how users behave when they see a flag variation
// e.g., after your app processed a purchase, let Eyeofcloud know what happened:
user.trackEvent("purchased");