1
1
using System ;
2
+ using System . Linq ;
3
+ using Newtonsoft . Json ;
2
4
using OptimizelySDK ;
3
5
4
6
namespace QuickStart
@@ -7,78 +9,57 @@ public static class QuickStart
7
9
{
8
10
public static void Main ( )
9
11
{
10
- // This Optimizely initialization is synchronous. For other methods, see the C# SDK reference.
11
- var optimizelyClient = OptimizelyFactory . NewDefaultInstance ( "K4UmaV5Pk7cEh2hbcjgwe" ) ;
12
+ var optimizelyClient = OptimizelyFactory . NewDefaultInstance ( "TbrfRLeKvLyWGusqANoeR" ) ;
12
13
if ( ! optimizelyClient . IsValid )
13
14
{
14
15
Console . WriteLine (
15
16
"Optimizely client invalid. " +
16
17
"Verify in Settings>Environments that you used the primary environment's SDK key" ) ;
17
18
optimizelyClient . Dispose ( ) ;
18
19
}
20
+
21
+ const string USER_ID = "matjaz-user-2" ;
19
22
20
- var hasOnFlags = false ;
21
-
22
- /*
23
- * To get rapid demo results, generate random users.
24
- * Each user always sees the same variation unless you reconfigure the flag rule.
25
- */
26
- var rnd = new Random ( ) ;
27
- for ( var i = 0 ; i < 10 ; i ++ )
28
- {
29
- var userId = rnd . Next ( 1000 , 9999 ) . ToString ( ) ;
30
-
31
- // Create a user context to bucket the user into a variation.
32
- var user = optimizelyClient . CreateUserContext ( userId ) ;
33
-
34
- // "product_sort" corresponds to a flag key in your Optimizely project
35
- var decision = user . Decide ( "product_sort" ) ;
36
-
37
- // Did decision fail with a critical error?
38
- if ( string . IsNullOrEmpty ( decision . VariationKey ) )
39
- {
40
- Console . WriteLine ( Environment . NewLine + Environment . NewLine +
41
- "Decision error: " + string . Join ( " " , decision . Reasons ) ) ;
42
- continue ;
43
- }
44
-
45
- /*
46
- * Get a dynamic configuration variable.
47
- * "sort_method" corresponds to a variable key in your Optimizely project.
48
- */
49
- var sortMethod = decision . Variables . ToDictionary ( ) [ "sort_method" ] ;
50
-
51
- hasOnFlags = hasOnFlags || decision . Enabled ;
52
-
53
- /*
54
- * Mock what the user sees with print statements (in production, use flag variables to implement feature configuration)
55
- */
56
-
57
- // always returns false until you enable a flag rule in your Optimizely project
58
- Console . WriteLine ( Environment . NewLine +
59
- $ "Flag { ( decision . Enabled ? "on" : "off" ) } . " +
60
- $ "User number { user . GetUserId ( ) } saw " +
61
- $ "flag variation { decision . VariationKey } and got " +
62
- $ "products sorted by { sortMethod } config variable as part of " +
63
- $ "flag rule { decision . RuleKey } ") ;
64
- }
65
-
66
- if ( ! hasOnFlags )
67
- {
68
- var projectId = optimizelyClient . ProjectConfigManager . GetConfig ( ) . ProjectId ;
69
- var projectSettingsUrl =
70
- $ "https://app.optimizely.com/v2/projects/{ projectId } /settings/implementation";
71
-
72
- Console . WriteLine ( Environment . NewLine + Environment . NewLine +
73
- "Flag was off for everyone. Some reasons could include:" +
74
- Environment . NewLine +
75
- "1. Your sample size of visitors was too small. Re-run, or increase the iterations in the FOR loop" +
76
- Environment . NewLine +
77
- "2. 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." +
78
- Environment . NewLine + Environment . NewLine +
79
- $ "Check your key at { projectSettingsUrl } ") ;
80
- }
23
+ var user = optimizelyClient . CreateUserContext ( USER_ID ) ;
81
24
25
+ // Fetch
26
+ // Console.WriteLine("Fetch:" + user.FetchQualifiedSegments());
27
+ // var qualifiedSegments = user.GetQualifiedSegments();
28
+ // Console.WriteLine(JsonConvert.SerializeObject(qualifiedSegments));
29
+ // const string SEGMENT_ID = "atsbugbashsegmentdob";
30
+ // Console.WriteLine($"Is Qualified for {SEGMENT_ID}: {user.IsQualifiedFor(SEGMENT_ID)}");
31
+
32
+ // TrackEvent
33
+ // user.TrackEvent("myevent");
34
+
35
+ // Decide
36
+ // var decision = user.Decide("flag1");
37
+ // Console.WriteLine(JsonConvert.SerializeObject(decision));
38
+ // var variables = decision.Variables.ToDictionary();
39
+ // Console.WriteLine(JsonConvert.SerializeObject(variables));
40
+
41
+ // DecideForKeys
42
+ var keys = new [ ] { "flag1" , "flag2" } ;
43
+ var decisions = user . DecideForKeys ( keys ) ;
44
+ Console . WriteLine ( JsonConvert . SerializeObject ( decisions ) ) ;
45
+
46
+ // DecideAll
47
+ // var decisions = user.DecideAll();
48
+ // Console.WriteLine(JsonConvert.SerializeObject(decisions));
49
+
50
+ // Set & Get & Remove ForcedDecision
51
+ // var context = new OptimizelyDecisionContext("flag1", "default-rollout-34902-22583870382");
52
+ // var forcedDecision = new OptimizelyForcedDecision("off");
53
+ // user.SetForcedDecision(context, forcedDecision);
54
+ // var result = user.GetForcedDecision(context);
55
+ // Console.WriteLine(JsonConvert.SerializeObject(result));
56
+ // var wasRemoved = user.RemoveForcedDecision(context);
57
+ // Console.WriteLine($"Was removed: {wasRemoved}");
58
+
59
+ // Activate
60
+ // var variation = optimizelyClient.Activate("flag1", USER_ID);
61
+ // Console.WriteLine("Variation: " + JsonConvert.SerializeObject(variation));
62
+
82
63
optimizelyClient . Dispose ( ) ;
83
64
}
84
65
}
0 commit comments