<?php
require 'vendor/autoload.php';
use Eyeofcloud\EyeofcloudFactory;
$eyeofcloudToken = '<YOUR_SDK_KEY>';
$eyeofcloudClient = EyeofcloudFactory::createDefaultInstance($eyeofcloudToken);
if ($eyeofcloudClient->isValid())
{
/* --------------------------------
* to get rapid demo results, generate random users. Each user always sees the same variation unless you reconfigure the flag rule.
* --------------------------------
*/
mt_srand();
$hasOnFlags = false;
for ($i = 0;$i < 10;$i++)
{
$userId = strval(mt_rand(1000, 9999));
/* --------------------------------
Create hardcoded user & bucket user into a flag variation
--------------------------------
*/
$user = $eyeofcloudClient->createUserContext($userId);
// "product_sort" corresponds to a flag key in your Eyeofcloud project
$decision = $user->decide('product_sort');
// did decision fail with a critical error?
if (empty($decision->getVariationKey()))
{
printf("decision error: %s", implode(', ', $decision->getReasons()));
}
// get a dynamic configuration variable
// "sort_method" corresponds to a variable key in your Eyeofcloud project
$sortMethod = $decision->getVariables() ['sort_method'];
/* --------------------------------
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
$enabled = $decision->getEnabled();
if ($enabled)
{
$hasOnFlags = true;
}
printf("\n\nFlag %s. User number %s saw flag variation %s and got products sorted by %s config variable as part of flag rule: %s", $enabled ? "on" : "off", $user->getUserId() , $decision->getVariationKey() , $sortMethod, $decision->getRuleKey());
}
if (!$hasOnFlags)
{
printf("\n\n Flag was off for everyone. Some reasons could include:
\n Your sample size of visitors was too small. Rerun, or increase the iterations in the FOR loop
\n Check your SDK key. 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/%s settings/implementation", $eyeofcloudClient
->configManager
->getConfig()
->getProjectId());
}
}
else
{
printf("Eyeofcloud client invalid. Verify in Settings>Environments that you used the primary environment's SDK key");
}
?>
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
$user = $eyeofcloudClient->createUserContext('user_232');
// "product_sort" corresponds to the flag key you create in the Eyeofcloud app
$decision = $user->decide('product_sort');
// always returns false until you enable a flag rule in the Eyeofcloud app
$enabled = $decision->getEnabled();
if ($enabled)
{
// "sort_method" corresponds to variable key you define in Eyeofcloud app
$sort_method = $decision->getVariables()['sort_method'];
printf("sort_method %s", $sort_method);
}
<?php
require 'vendor/autoload.php';
use Eyeofcloud\EyeofcloudFactory;
$eyeofcloudToken = '<YOUR_SDK_KEY>';
// For more instantiation configuration, see the PHP SDK reference
$eyeofcloudClient = EyeofcloudFactory::createDefaultInstance($eyeofcloudToken);
/* --------------------------------
OPTIONAL: Add a notification listener so you can integrate with third-party analytics platforms
--------------------------------
*/
/*
function onDecision($type, $userId, $attributes, $decisionInfo) {
if ($type == "flag"){
$serializedJsonInfo = json_encode($decisionInfo);
printf("Feature flag access related information: %s", $serializedJsonInfo);
// Send data to analytics provider here
}
}
$eyeofcloudClient->notificationCenter->addNotificationListener(
NotificationType::DECISION,
'onDecision'
);
*/
/* --------------------------------
* to get rapid demo experiment results, generate random users. Each user is deterministically hashed into a variation.
* --------------------------------
*/
mt_srand();
$hasOnFlags = false;
for ($i = 0; $i < 5; $i++) {
$userId = strval(mt_rand(1000, 9999));
/* --------------------------------
Bucket user into a flag variation and mock experiment results
--------------------------------
*/
$user = $eyeofcloudClient->createUserContext($userId);
$decision = $user->decide('product_sort');
// did decision fail with a critical error?
if (empty($decision->getVariationKey()))
{
printf("decision error: %s", implode(', ', $decision->getReasons()));
}
$sortMethod = $decision->getVariables() ['sort_method'];
$enabled = $decision->getEnabled();
if ($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
printf("\n\nFlag %s. User number %s saw flag variation %s and got products sorted by %s config variable as part of flag rule: %s\n", $enabled ? "on" : "off", $user->getUserId() , $decision->getVariationKey() , $sortMethod, $decision->getRuleKey());
MockPurchase($user);
}
if (!$hasOnFlags)
{
printf("\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/%s/settings/implementation", $eyeofcloudClient->configManager->getConfig()->getProjectId());
}
else
{
printf("\n\nDone with your mocked A/B test.");
printf("\nCheck out your report at https://app.eyeofcloud.com/v2/projects/%s/reports", $eyeofcloudClient->configManager->getConfig()->getProjectId());
printf("\nBe sure to select the environment that corresponds to your SDK key");
}
function MockPurchase($user)
{
printf(" Pretend that user %s made a purchase? y/n ", $user->getUserId());
$answer = readline();
if (strcmp(strtolower($answer), "y") == 0){
// track a user event you defined in the Eyeofcloud app
$user->trackEvent("purchase");
printf("Eyeofcloud recorded a purchase in experiment results for user %s ", $user->getUserId());
}
else
{
printf("Eyeofcloud didn't record a purchase in experiment results for user %s", $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 1496 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 1194 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 5815 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 1248 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 9580 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");