Last active
November 16, 2022 10:40
-
-
Save RidhamShah/b1de64fb62b8732bc1cf7cf0212b7c9a to your computer and use it in GitHub Desktop.
Updated UpgradeClient gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UpgradeClient from 'upgrade_client_lib'; | |
function async SampleAplication() { | |
// construct upgrade client for student1 | |
const userId = "student1"; | |
// set host url | |
const hostUrl = 'http://development-upgrade-experiment-app.eba-gp6psjut.us-east-1.elasticbeanstalk.com'; | |
// Two ways for initializing user | |
const upClient = new UpgradeClient(userId, hostUrl); // No network call will be done over here. | |
const upClient = new UpgradeClient(userId, hostUrl, token); // No network call will be done over here. | |
const groupMembership = convertObjectToRecordGroup({ | |
class: ["class1", "class2"] | |
}) | |
const workingGroup = convertObjectToRecordWorkingGroup({ | |
class: "class1" | |
}) | |
// Initialize user with Upgrade system | |
// There are multiple ways to initialize user | |
// We can set groupMemberShip and workingGroup for user later by invoking the function setGroupMembership and setWorkingGroup | |
await upClient.init(); // Network call. It will create a new user with the userId passed in the constructor if not created already | |
await upClient.init(groupMembership); // Network call. It will create a new user with groupMembership if not created already | |
await upClient.init(groupMembership, workingGroup); // Network call. It will create a new user with groupMembership and workingGroup if not created already | |
// Set alternative user ids for current user | |
const altUserIds = ['aliases1', 'aliases2', 'aliases3']; | |
await upClient.setAltUserIds(altUserIds); // Network call to set altUserIds for current user | |
// set group membership for client | |
await upClient.setGroupMembership(groupMembership); // Network call. It will create group on Upgrade | |
// set working group for client | |
await upClient.setWorkingGroup(workingGroup); // Network call. It will create working group on Upgrade | |
await upClient.getAllExperimentConditions('context'); // Network call. It will retrieve all assignments. | |
const experimentConditions = await upClient.getExperimentCondition('context', 'site', 'target'); // It will do network only if getAllExperimentConditions() is not called before this function | |
await upClient.markExperimentPoint('site', 'conditionCode', 'condition applied', 'target'); // Network call. It will mark experiment point | |
await upClient.failedExperimentPoint('site', 'Not implemented yet', 'target'); // Network call | |
await upClient.getAllFeatureFlags(); // Network call. It will return all feature flags | |
// Feature flag key | |
const featureFlagKey = 'theme'; | |
const flag = await upClient.getFeatureFlag(featureFlagKey); // No Network call. | |
await upClient.log(logData); // Network call. It will log data of user | |
await upClient.addMetrics(metrics); // Network call. It will add metrics in upgrade system | |
function convertObjectToRecordGroup(obj) { | |
const objMap: Record<string, Array<string>> = {} | |
Object.keys(obj).forEach((key) => { | |
objMap[key] = obj[key]; | |
}); | |
return objMap; | |
} | |
function convertObjectToRecordWorkingGroup(obj) { | |
const objMap: Record<string, string > = {} | |
Object.keys(obj).forEach((key) => { | |
objMap[key] = obj[key]; | |
}); | |
return objMap; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment