Skip to content

Commit 91eebfb

Browse files
Add quick start
1 parent c6213a9 commit 91eebfb

File tree

4 files changed

+187
-0
lines changed

4 files changed

+187
-0
lines changed

OptimizelySDK.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OptimizelySDK.Net40", "Opti
2828
EndProject
2929
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OptimizelySDK.NetStandard20", "OptimizelySDK.NetStandard20\OptimizelySDK.NetStandard20.csproj", "{CFEC91C6-B6E5-45D5-97D6-7081B0DC7453}"
3030
EndProject
31+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QuickStart", "QuickStart\QuickStart.csproj", "{74BAE350-8779-413A-A88E-AFE6A0A84D28}"
32+
EndProject
3133
Global
3234
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3335
Debug|Any CPU = Debug|Any CPU
@@ -62,6 +64,10 @@ Global
6264
{CFEC91C6-B6E5-45D5-97D6-7081B0DC7453}.Debug|Any CPU.Build.0 = Debug|Any CPU
6365
{CFEC91C6-B6E5-45D5-97D6-7081B0DC7453}.Release|Any CPU.ActiveCfg = Release|Any CPU
6466
{CFEC91C6-B6E5-45D5-97D6-7081B0DC7453}.Release|Any CPU.Build.0 = Release|Any CPU
67+
{74BAE350-8779-413A-A88E-AFE6A0A84D28}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
68+
{74BAE350-8779-413A-A88E-AFE6A0A84D28}.Debug|Any CPU.Build.0 = Debug|Any CPU
69+
{74BAE350-8779-413A-A88E-AFE6A0A84D28}.Release|Any CPU.ActiveCfg = Release|Any CPU
70+
{74BAE350-8779-413A-A88E-AFE6A0A84D28}.Release|Any CPU.Build.0 = Release|Any CPU
6571
EndGlobalSection
6672
GlobalSection(SolutionProperties) = preSolution
6773
HideSolutionNode = FALSE

QuickStart/Program.cs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using System;
2+
using OptimizelySDK;
3+
4+
namespace QuickStart
5+
{
6+
public static class QuickStart
7+
{
8+
public static void Main()
9+
{
10+
// This Optimizely initialization is synchronous. For other methods, see the C# SDK reference.
11+
var optimizelyClient = OptimizelyFactory.NewDefaultInstance("K4UmaV5Pk7cEh2hbcjgwe");
12+
if (!optimizelyClient.IsValid)
13+
{
14+
Console.WriteLine(
15+
"Optimizely client invalid. " +
16+
"Verify in Settings>Environments that you used the primary environment's SDK key");
17+
optimizelyClient.Dispose();
18+
}
19+
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+
}
81+
82+
optimizelyClient.Dispose();
83+
}
84+
}
85+
}

QuickStart/Properties/AssemblyInfo.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.Reflection;
2+
using System.Runtime.InteropServices;
3+
4+
// General Information about an assembly is controlled through the following
5+
// set of attributes. Change these attribute values to modify the information
6+
// associated with an assembly.
7+
[assembly: AssemblyTitle("QuickStart")]
8+
[assembly: AssemblyDescription("")]
9+
[assembly: AssemblyConfiguration("")]
10+
[assembly: AssemblyCompany("")]
11+
[assembly: AssemblyProduct("QuickStart")]
12+
[assembly: AssemblyCopyright("Copyright © 2024")]
13+
[assembly: AssemblyTrademark("")]
14+
[assembly: AssemblyCulture("")]
15+
16+
// Setting ComVisible to false makes the types in this assembly not visible
17+
// to COM components. If you need to access a type in this assembly from
18+
// COM, set the ComVisible attribute to true on that type.
19+
[assembly: ComVisible(false)]
20+
21+
// The following GUID is for the ID of the typelib if this project is exposed to COM
22+
[assembly: Guid("74BAE350-8779-413A-A88E-AFE6A0A84D28")]
23+
24+
// Version information for an assembly consists of the following four values:
25+
//
26+
// Major Version
27+
// Minor Version
28+
// Build Number
29+
// Revision
30+
//
31+
// You can specify all the values or you can default the Build and Revision Numbers
32+
// by using the '*' as shown below:
33+
// [assembly: AssemblyVersion("1.0.*")]
34+
[assembly: AssemblyVersion("1.0.0.0")]
35+
[assembly: AssemblyFileVersion("1.0.0.0")]

QuickStart/QuickStart.csproj

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props"
4+
Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')"/>
5+
<PropertyGroup>
6+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
7+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
8+
<ProjectGuid>{74BAE350-8779-413A-A88E-AFE6A0A84D28}</ProjectGuid>
9+
<OutputType>Exe</OutputType>
10+
<AppDesignerFolder>Properties</AppDesignerFolder>
11+
<RootNamespace>QuickStart</RootNamespace>
12+
<AssemblyName>QuickStart</AssemblyName>
13+
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
14+
<FileAlignment>512</FileAlignment>
15+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
16+
</PropertyGroup>
17+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
18+
<PlatformTarget>AnyCPU</PlatformTarget>
19+
<DebugSymbols>true</DebugSymbols>
20+
<DebugType>full</DebugType>
21+
<Optimize>false</Optimize>
22+
<OutputPath>bin\Debug\</OutputPath>
23+
<DefineConstants>DEBUG;TRACE</DefineConstants>
24+
<ErrorReport>prompt</ErrorReport>
25+
<WarningLevel>4</WarningLevel>
26+
</PropertyGroup>
27+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
28+
<PlatformTarget>AnyCPU</PlatformTarget>
29+
<DebugType>pdbonly</DebugType>
30+
<Optimize>true</Optimize>
31+
<OutputPath>bin\Release\</OutputPath>
32+
<DefineConstants>TRACE</DefineConstants>
33+
<ErrorReport>prompt</ErrorReport>
34+
<WarningLevel>4</WarningLevel>
35+
</PropertyGroup>
36+
<ItemGroup>
37+
<Reference Include="System"/>
38+
<Reference Include="System.Core"/>
39+
<Reference Include="System.Data"/>
40+
<Reference Include="System.Xml"/>
41+
</ItemGroup>
42+
<ItemGroup>
43+
<Compile Include="Program.cs"/>
44+
<Compile Include="Properties\AssemblyInfo.cs"/>
45+
</ItemGroup>
46+
<ItemGroup>
47+
<ProjectReference Include="..\OptimizelySDK\OptimizelySDK.csproj">
48+
<Project>{4dde7faa-110d-441c-ab3b-3f31b593e8bf}</Project>
49+
<Name>OptimizelySDK</Name>
50+
</ProjectReference>
51+
</ItemGroup>
52+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets"/>
53+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
54+
Other similar extension points exist, see Microsoft.Common.targets.
55+
<Target Name="BeforeBuild">
56+
</Target>
57+
<Target Name="AfterBuild">
58+
</Target>
59+
-->
60+
61+
</Project>

0 commit comments

Comments
 (0)