# From your project directory, run the terminal command below to install Eyeofcloud Feature Experimentation: npm install --save @eyeofcloud/eyeofcloud-sdk
使用Yarn
# From your project directory, run the terminal command below to install Eyeofcloud Full Stack: yarn add @eyeofcloud/eyeofcloud-sdk
从项目目录中,创建一个名为 的空文本文件。 eyeofcloud-js-quickstart.js
将以下代码示例复制到在上一步中创建的文件中。
替换为在上一步中找到的 SDK 密钥。<Your_SDK_Key>
使用 NPM
const eyeofcloudSdk = require('@eyeofcloud/eyeofcloud-sdk');const eyeofcloudClient = eyeofcloudSdk.createInstance({ sdkKey: '<YOUR_SDK_KEY>'});eyeofcloudClient.onReady().then(() => { console.log('***eyeofcloudClient is valid instance 2****', eyeofcloudClient.isValidInstance()); if (!eyeofcloudClient.isValidInstance()) { console.log('Eyeofcloud client invalid. Verify in Settings>Environments that you used the primary environment\'s SDK key'); return; } let hasOnFlags = false; for (let i = 0; i < 10; i++) { // to get rapid demo results, generate random users. Each user always sees the same variation unless you reconfigure the flag rule. let userId = (Math.floor(Math.random() * (10000 - 1000) + 1000)).toString(); // Create hardcoded user & bucket user into a flag variation let user = eyeofcloudClient.createUserContext(userId); // "product_sort" corresponds to a flag key in your Eyeofcloud project let decision = user.decide('product_sort'); let variationKey = decision.variationKey; // did decision fail with a critical error? if (variationKey === null) { console.log(' decision error: ', decision['reasons']); } let sortMethod = decision.variables['sort_method']; // get a dynamic configuration variable // "sort_method" corresponds to a variable key in your Eyeofcloud project if (decision.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 console.log(`\nFlag ${decision.enabled ? 'on' : 'off'}. User number ${user.getUserId()} saw flag variation: ${variationKey} and got products sorted by: ${sortMethod} config variable as part of flag rule: ${decision.ruleKey}`); } if (!hasOnFlags) { console.log("\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." + "\nCheck your key at https://app.eyeofcloud.com/v2/projects/" + eyeofcloudClient.projectConfigManager.getConfig().projectId + "/settings/implementation"); };});
Flag on. User number 6998 saw flag variation: on and got products sorted by: popular_first config variable as part of flag rule: targeted_deliveryFlag on. User number 1177 saw flag variation: on and got products sorted by: popular_first config variable as part of flag rule: targeted_deliveryFlag on. User number 9714 saw flag variation: on and got products sorted by: popular_first config variable as part of flag rule: targeted_deliveryFlag on. User number 4140 saw flag variation: on and got products sorted by: popular_first config variable as part of flag rule: targeted_deliveryFlag on. User number 4994 saw flag variation: on and got products sorted by: popular_first config variable as part of flag rule: targeted_deliveryFlag off. User number 8700 saw flag variation: off and got products sorted by: alphabetical config variable as part of flag rule: default-rollout-208-19963693913Flag off. User number 9912 saw flag variation: off and got products sorted by: alphabetical config variable as part of flag rule: default-rollout-208-19963693913Flag on. User number 6560 saw flag variation: on and got products sorted by: popular_first config variable as part of flag rule: targeted_deliveryFlag on. User number 9252 saw flag variation: on and got products sorted by: popular_first config variable as part of flag rule: targeted_deliveryFlag on. User number 6582 saw flag variation: on and got products sorted by: popular_first config variable as part of flag rule: targeted_delivery
var user = eyeofcloudClient.createUserContext(userID);// "product_sort" corresponds to the flag key you create in the Eyeofcloud appvar decision = user.decide('product_sort');
// always returns false until you enable a flag rule in the Eyeofcloud appif (decision.enabled) { // "sort_method" corresponds to variable key you define in Eyeofcloud app var sortMethod = decision.variables['sort_method']; console.log('sort_method: ', sortMethod);}
const eyeofcloudSdk = require('@eyeofcloud/eyeofcloud-sdk');const readline = require("readline");// For more instantiation configuration, see the Javascript SDK reference const eyeofcloudClient = eyeofcloudSdk.createInstance({ sdkKey: '<YOUR_SDK_KEY>'});const rl = readline.createInterface({ input: process.stdin, output: process.stdout});eyeofcloudClient.onReady().then(() => { if (!eyeofcloudClient.isValidInstance()) { console.log('Eyeofcloud client invalid. Verify in Settings>Environments that you used the primary environment\'s SDK key'); return; } runExperiment(); // mock tracking a user event so you can see some experiment reports function mockPurchase(user) { return new Promise(function (resolve) { rl.question('Pretend that user ' + user.getUserId() + ' made a purchase? y/n\n', function(answer) { // track a user event you defined in the Eyeofcloud app if (answer === 'y') { user.trackEvent('purchase'); console.log("Eyeofcloud recorded a purchase in experiment results for user " + user.getUserId()); } else { console.log("Eyeofcloud didn't record a purchase in experiment results for user " + user.getUserId()); } resolve(answer); }); }); }; async function runExperiment() { let hasOnFlags = false; for (let i = 0; i < 4; i++) { // to get rapid demo results, generate random users. Each user always sees the same variation unless you reconfigure the flag rule. let userId = (Math.floor(Math.random() * (10000 - 1000) + 1000)).toString(); // Create hardcoded user & bucket user into a flag variation let user = eyeofcloudClient.createUserContext(userId); // "product_sort" corresponds to a flag key in your Eyeofcloud project let decision = user.decide('product_sort'); let variationKey = decision.variationKey; // did decision fail with a critical error? if (variationKey === null) { console.log(' decision error: ', decision['reasons']); } // get a dynamic configuration variable // "sort_method" corresponds to a variable key in your Eyeofcloud project let sortMethod = decision.variables['sort_method']; if (decision.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 console.log(`\n\nFlag ${decision.enabled ? 'on' : 'off'}. User number ${user.getUserId()} saw flag variation: ${variationKey} and got products sorted by: ${sortMethod} config variable as part of flag rule: ${decision.ruleKey}`); await mockPurchase(user); } if (!hasOnFlags) { console.log("\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." + "\nCheck your key at https://app.eyeofcloud.com/v2/projects/" + eyeofcloudClient.projectConfigManager.getConfig().projectId + "/settings/implementation"); } else { console.log("\n\nDone with your mocked A/B test. " + "\nCheck out your report at https://app.eyeofcloud.com/v2/projects/" + eyeofcloudClient.projectConfigManager.getConfig().projectId + "/reports" + "\nBe sure to select the environment that corresponds to your SDK key"); } }});
使用 HTML 脚本标记
<!DOCTYPE html><html><head> <title>Quickstart Guide</title> <script src="https://unpkg.com/@eyeofcloud/eyeofcloud-sdk/dist/eyeofcloud.browser.umd.min.js"></script></head><body> <pre>Welcome to our Quickstart Guide!</pre> <pre id="errors"></pre> <pre id="experiences"></pre> <pre id="result"></pre> <script> // For more instantiation configuration, see the Javascript SDK reference var eyeofcloudClient = window.eyeofcloudSdk.createInstance({ sdkKey: '<YOUR_SDK_KEY>' }); eyeofcloudClient.onReady().then(() => { var errors = document.getElementById('errors'); if (!eyeofcloudClient.isValidInstance()) { errors.innerText = 'Eyeofcloud client invalid. Verify in Settings>Environments that you used the primary environment\'s SDK key'; return; } var experiences = document.getElementById('experiences'); let hasOnFlags = false; for (let i = 0; i < 4; i++) { // to get rapid demo results, generate random users. Each user always sees the same variation unless you reconfigure the flag rule. let userId = (Math.floor(Math.random() * (10000 - 1000) + 1000)).toString(); // Create hardcoded user & bucket user into a flag variation let user = eyeofcloudClient.createUserContext(userId); // "product_sort" corresponds to a flag key in your Eyeofcloud project let decision = user.decide('product_sort'); let variationKey = decision.variationKey; // did decision fail with a critical error? if (variationKey === null) { errors.innerText += `\n\ndecision error: ${decision['reasons']}`; } // get a dynamic configuration variable // "sort_method" corresponds to a variable key in your Eyeofcloud project let sortMethod = decision.variables['sort_method']; if (decision.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 experiences.innerText += `\n\n\nFlag ${decision.enabled ? 'on' : 'off'}. User number ${user.getUserId()} saw flag variation: ${variationKey} and got products sorted by: ${sortMethod} config variable as part of flag rule: ${decision.ruleKey}`; mockPurchase(user, experiences); } // mock tracking a user event so you can see some experiment reports function mockPurchase(user, experiences) { var question = '\nPretend that user ' + user.getUserId() + ' made a purchase? y/n'; experiences.innerText += question; var answer = prompt(question); experiences.innerText += ("\n" + answer); // track a user event you defined in the Eyeofcloud app if (answer == 'y') { user.trackEvent('purchase'); experiences.innerText += ("\nEyeofcloud recorded a purchase in experiment results for user " + user.getUserId()); } else { experiences.innerText += ("\nEyeofcloud didn't record a purchase in experiment results for user " + user.getUserId()); } } var result = document.getElementById('result'); if (!hasOnFlags) { result.innerText = "\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.projectConfigManager.getConfig().projectId + "/settings/implementation"; } else { result.innerText = "\nDone with your mocked A/B test. " + "\nCheck out your report at https://app.eyeofcloud.com/v2/projects/" + eyeofcloudClient.projectConfigManager.getConfig().projectId + "/reports" + "\nBe sure to select the environment that corresponds to your SDK key" } }); </script></body></html>
Flag on. User number 1496 saw flag variation: on and got products sorted by: popular_first config variable as part of flag rule: experiment_1Pretend that user 1496 made a purchase? y/nnEyeofcloud didn't record a purchase in experiment results for user 1496Flag off. User number 1194 saw flag variation: off and got products sorted by: alphabetical config variable as part of flag rule: experiment_1Pretend that user 1194 made a purchase? y/nyEyeofcloud recorded a purchase in experiment results for user 1194Flag off. User number 5815 saw flag variation: off and got products sorted by: alphabetical config variable as part of flag rule: experiment_1Pretend that user 5815 made a purchase? y/nyEyeofcloud recorded a purchase in experiment results for user 5815Flag on. User number 1248 saw flag variation: on and got products sorted by: popular_first config variable as part of flag rule: experiment_1Pretend that user 1248 made a purchase? y/nyEyeofcloud recorded a purchase in experiment results for user 1248Flag off. User number 9580 saw flag variation: off and got products sorted by: alphabetical config variable as part of flag rule: experiment_1Pretend that user 9580 made a purchase? y/nnEyeofcloud didn't record a purchase in experiment results for user 9580Done with your mocked A/B test.Check out your report at https://app.eyeofcloud.com/v2/projects/19957465438/reportsBe 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');