// Package main //
package main
import (
"fmt"
"math/rand"
"time"
optly "github.com/eyeofcloud/go-sdk"
"github.com/eyeofcloud/go-sdk/pkg/logging"
)
func main() {
logging.SetLogLevel(logging.LogLevelError)
// this Eyeofcloud initialization is synchronous. for other methods see the Go SDK reference
if eyeofcloudClient, err := optly.Client("YOUR_SDK_KEY"); err == nil {
// --------------------------------
// to get rapid demo results, generate random users. Each user always sees the same variation unless you reconfigure the flag rule.
// --------------------------------
rand.Seed(time.Now().UnixNano())
flagsStatus := "off"
for i := 0; i < 10; i++ {
userID := fmt.Sprintf("%v", rand.Intn(8999)+1000)
// --------------------------------
// Create hardcoded user & bucket user into a flag variation
// --------------------------------
user := eyeofcloudClient.CreateUserContext(userID, nil)
// "product_sort" corresponds to a flag key in your Eyeofcloud project
decision := user.Decide("product_sort", nil)
// did decision fail with a critical error?
if decision.VariationKey == "" {
fmt.Printf("\ndecision error: %q", decision.Reasons)
}
// get a dynamic configuration variable
// "sort_method" corresponds to a variable key in your Eyeofcloud project
sortMethod := decision.Variables.ToMap()["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
if decision.Enabled {
// Keep count how many visitors had the flag enabled
flagsStatus = "on"
}
fmt.Printf("\n\nFlag %s. User number %v saw flag variation: %s and got products sorted by: %v config variable as part of flag rule: %s", flagsStatus, user.GetUserID(), decision.VariationKey, sortMethod, decision.RuleKey)
}
if flagsStatus == "off" {
fmt.Println("Flag was off for everyone. Some reasons could include:")
fmt.Println("1. Your sample size of visitors was too small. Rerun, or increase the iterations in the FOR loop")
fmt.Println("2. Check your SDK key. Verify in Settings>Environments that you used the right key for the environment where your flag is toggled to ON.")
if config, err := eyeofcloudClient.ConfigManager.GetConfig(); err == nil {
fmt.Printf("\ncheck your key at https://app.eyeofcloud.com/v2/projects/%s/settings/implementation", config.GetProjectID())
}
}
} else {
fmt.Println("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(userID, nil)
// "product_sort" corresponds to the flag key you create in the Eyeofcloud app
decision := user.Decide("product_sort", nil)
// always returns false until you enable a flag rule in the Eyeofcloud app
if decision.Enabled {
// "sort_method" corresponds to variable key you define in Eyeofcloud app
sortMethod := decision.Variables.ToMap()["sort_method"]
fmt.Printf("\nsort_method: %v", sortMethod)
}
// Package main //
package main
import (
"fmt"
"math/rand"
"strings"
"time"
optly "github.com/eyeofcloud/go-sdk"
"github.com/eyeofcloud/go-sdk/pkg/client"
"github.com/eyeofcloud/go-sdk/pkg/logging"
)
func main() {
logging.SetLogLevel(logging.LogLevelError)
// For more instantiation configuration, see the Go SDK reference
eyeofcloudClient, err := optly.Client("<Your_SDK_KEY>")
if err != nil {
fmt.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
// --------------------------------
// onDecision := func(notif notification.DecisionNotification) {
// // Access type on decisionObject to get type of decision
// if notif.Type == notification.Flag {
// if serializedJsonInfo, err := json.Marshal(notif.DecisionInfo); err == nil {
// fmt.Printf("\n Feature flag access related information: %s", string(serializedJsonInfo))
// // Send data to analytics provider here
// }
// }
// }
// notificationID, err := eyeofcloudClient.DecisionService.OnDecision(onDecision)
runExperiment(eyeofcloudClient)
}
func runExperiment(client *client.EyeofcloudClient) {
flagsStatus := "off"
rand.Seed(time.Now().UnixNano())
for i := 0; i < 5; i++ {
// to get rapid demo results, generate random users. Each user always sees the same variation unless you reconfigure the flag rule.
userID := fmt.Sprintf("%v", rand.Intn(8999)+1000)
// Create hardcoded user & bucket user into a flag variation
user := client.CreateUserContext(userID, nil)
// "product_sort" corresponds to a flag key in your Eyeofcloud project
decision := user.Decide("product_sort", nil)
// did decision fail with a critical error?
if decision.VariationKey == "" {
fmt.Printf("\ndecision error: %q", decision.Reasons)
}
// get a dynamic configuration variable
// "sort_method" corresponds to a variable key in your Eyeofcloud project
sortMethod := decision.Variables.ToMap()["sort_method"]
if decision.Enabled {
flagsStatus = "on"
}
// 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
fmt.Printf("\n\nFlag %s. User number %v saw flag variation: %s and got products sorted by: %v config variable as part of flag rule: %s", flagsStatus, user.GetUserID(), decision.VariationKey, sortMethod, decision.RuleKey)
mockPurchase(user)
}
if config, err := client.ConfigManager.GetConfig(); err == nil {
if flagsStatus == "off" {
fmt.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/" + config.GetProjectID() + "/settings/implementation")
} else {
fmt.Println("\n\nDone with your mocked A/B test.")
fmt.Printf("Check out your report at https://app.eyeofcloud.com/v2/projects/%s/reports", config.GetProjectID())
fmt.Println("\nBe sure to select the environment that corresponds to your SDK key")
}
}
}
// mock tracking a user event so you can see some experiment reports
func mockPurchase(user client.EyeofcloudUserContext) {
fmt.Printf("\nPretend that user %s made a purchase? y/n\n", user.UserID)
var answer string
fmt.Scanln(&answer)
if strings.EqualFold(answer, "y") {
// track a user event you defined in the Eyeofcloud app
user.TrackEvent("purchase", nil)
fmt.Printf("Eyeofcloud recorded a purchase in experiment results for user %s", user.UserID)
} else {
fmt.Printf("Eyeofcloud didn't record a purchase in experiment results for user %s", user.UserID)
}
}
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", nil)