Skip to main content

C# SDK

Welcome to the developer documentation for the Kameleoon C# SDK! Use our SDK to run experiments on your back-end .NET application server. It is easy to integrate our SDK into your web-application, and its memory and network usage are low.

Latest version of the C# SDK: 3.3.2 (changelog).

Getting started

This guide is designed to help you integrate our SDK into your C# applications.

Install the C# client

You can use the NuGet, .NET CLI, or Paket package manager to install the C# client.

Install-Package KameleoonClient -Version 3.3.0

Additional configuration

Create a .properties configuration file to provide credentials and customize the SDK behavior. You can also download a sample configuration file. Save this file in the default path /etc/kameleoon/client-csharp.conf. If you place it in another location, you'll need to pass the path as an argument to the KameleoonClientFactory.Create() method. With the current version of the C# SDK, these are the available keys.

The following table shows the available properties that you can set:

KeyDescription
client_idRequired for authentication to the Kameleoon service. To find your client_id, see the API credentials documentation.
client_secretRequired for authentication to the Kameleoon service. To find your client_secret, see the API credentials documentation.
actions_configuration_refresh_intervalSpecifies the refresh interval, in minutes, that the SDK fetches the configuration for the active experiments and feature flags. The value determines the maximum time it takes to propagate changes, such as activating or deactivating feature flags or launching experiments, to your production servers. If left unspecified, the default interval is set to 60 minutes. Additionally, we offer a streaming mode that uses server-sent events (SSE) to push new configurations to the SDK automatically and apply new configurations in real-time, without any delays.
visitor_data_maximum_sizeSpecifies the maximum amount of memory that the map holding all the visitor data (in particular custom data) can consume (in MB). If not specified, the default size is 500MB.
default_timeoutSpecifies the timeout, in milliseconds, for network requests from the SDK. Set the value to 30 seconds or more if you do not have a stable connection. The default value is 15 seconds. Some methods have their additional parameters for method-specific timeouts, but if you do not specify them explicitly, the default value is used.
environmentEnvironment from which a feature flag’s configuration is to be used. The value can be production, staging, development. The default environment value is production. See the managing environments article for details.

Initialize the Kameleoon client

After you've installed the SDK into your application and configured your credentials and SDK behavior, create the Kameleoon client in your application code. For example:

using Kameleoon;

string siteCode = "a8st4f59bj";

IKameleoonClient kameleoonClient = KameleoonClientFactory.Create(siteCode);

IKameleoonClient kameleoonClient = KameleoonClientFactory.Create(siteCode, "/etc/kameleoon/client-csharp.conf");

A `KameleoonClient`` is a singleton object that acts as a bridge between your application and the Kameleoon platform. It includes all the methods and properties you need to run an experiment.

It's your responsibility as the app developer to ensure your application code uses the correct logic within the context of A/B testing via Kameleoon. It's a best practice to assume that you can exclude the current visitor from the experiment if the experiment has not yet been launched. This is easy to do, because this practice corresponds to the default and reference variation logic.

Reference

This is the full reference documentation of the C# SDK.

Kameleoon.KameleoonClientFactory

Create()

The starting point for using the SDK is the initialization step. All of your app's interaction with the SDK is done through an object of the KameleoonClient class. Create this object using the KameleoonClientFactory Create() static method.

Arguments
NameTypeDescription
siteCodestringCode of the website you want to run experiments on. This unique code id can be found in our platform's back-office. This field is mandatory.
configurationFilePathstringPath to the SDK configuration file. This field is optional and set to /etc/kameleoon/client-csharp.conf by default.
clientIDstringThis parameter is used for OAUth 2.0 authentication to our service. This field is optional, as it can be provided via the configuration file. However, it must either be supplied by the configuration file or by this method. Otherwise, a KameleoonException.CredentialsNotFound exception will be thrown.
clientSecretstringThis parameter is used for OAUth 2.0 authentication to our service. This field is optional, as it can be provided via the configuration file. However, it must either be supplied by the configuration file or by this method. Otherwise, a KameleoonException.CredentialsNotFound exception will be thrown.
Return value
TypeDescription
IKameleoonClientAn instance of the KameleoonClient class, that will be used to manage your experiments and feature flags.
Exceptions thrown
TypeDescription
KameleoonException.CredentialsNotFoundException indicating that the requested credentials were not provided in the configuration file or as arguments on the method.
KameleoonException.EmptySiteCodeException indicating that the specified site code is empty string which is invalid value.
Example code
IKameleoonClient kameleoonClient = KameleoonClientFactory.Create(siteCode);

// With a custom configurationFilePath
IKameleoonClient kameleoonClient = KameleoonClientFactory.Create(siteCode, false, "/var/lib/kameleoon/client-csharp.conf");

Kameleoon.IKameleoonClient

GetVisitorCode()

note

Previously named: ObtainVisitorCode, which is deprecated since SDK version 3.0.0 and will be removed in a future release.

The GetVisitorCode() helper method should be called to obtain the Kameleoon visitorCode for the current visitor. This is especially important when using Kameleoon in a mixed front-end and back-end environment, where user identification consistency must be guaranteed. The implementation logic is described here:

  1. First, check if a kameleoonVisitorCode cookie or query parameter associated with the current HTTP request exists. If so, use this as the visitor identifier and skip the next step.

  2. If no cookie or parameter is found in the current request, either randomly generate a new identifier, or use the defaultVisitorCode argument as identifier if it is passed. This allows you to use your own identifiers as visitor codes, if desired. This can help you match Kameleoon visitors with your own users without any additional look-ups in a matching table.

  3. Set the server-side kameleoonVisitorCode cookie (via HTTP header) with the identifier value. This identifier value is then returned by the method.

note

If you provide your own visitorCode, make sure it is unique! Also note that the length of visitorCode is limited to 255 characters. Using an identifier with too many characters will result in an exception.

Arguments
NameTypeDescription
RequestMicrosoft.AspNetCore.Http.RequestThe current Request object should be passed as the first parameter. This field is mandatory.
ResponseMicrosoft.AspNetCore.Http.ResponseThe current Response object should be passed as the second parameter. This field is mandatory.
topLevelDomainstringYour current top level domain for the concerned site (this information is needed to set the corresponding cookie accordingly, on the top level domain). This field is mandatory.
defaultVisitorCodestringThis parameter will be used as the visitorCode if no existing kameleoonVisitorCode cookie is found on the request. This field is optional, and by default a random visitorCode will be generated.
Return value
TypeDescription
stringA visitorCode that will be associated with this particular user and should be used with most of the methods of the SDK.
Example code
using static Kameleoon;

string visitorCode = kameleoonClient.GetVisitorCode(Request, Response, "example.com");

string visitorCode = kameleoonClient.GetVisitorCode(Request, Response, "example.com", defaultVisitorCode);

string visitorCode = kameleoonClient.GetVisitorCode(Request, Response, "example.com", Guid.NewGuid().ToString());

TriggerExperiment()

note

The TriggerExperiment() method will be deprecated as SDK and Hybrid type experiments are being merged with feature experimentation in September 2023. Use feature experimentation methods instead.

To trigger an experiment, call the TriggerExperiment() method of our SDK.

This method takes visitorCode and experimentID as mandatory arguments to register a variation for a given user.

If such a user has never been associated with any variation, the SDK returns a randomly selected variation. If a user with a given visitorCode is already registered with a variation, it will detect the previously registered variation and return the variationID.

You have to make sure that proper error handling is set up in your code as shown in the example to the right to catch potential exceptions.

Arguments
NameTypeDescription
visitorCodestringUnique identifier of the user. This field is mandatory.
experimentIDintID of the experiment you want to expose to a user. This field is mandatory.
Return value
TypeDescription
intID of the variation that is registered for a given visitorCode. By convention, the reference (original variation) always has an ID equal to 0.
Exceptions thrown
TypeDescription
KameleoonException.NotTargetedException indicating that the current visitor / user did not trigger the required targeting conditions for this experiment. The targeting conditions are defined via Kameleoon's segment builder.
KameleoonException.NotActivatedException indicating that the current visitor / user triggered the experiment (met the targeting conditions), but did not activate it. The most common reason for that is that part of the traffic has been excluded from the experiment and should not be tracked.
KameleoonException.ExperimentConfigurationNotFoundException indicating that the requested experiment ID has not been found in the internal configuration of the SDK. This is usually normal and means that the experiment has not yet been started on Kameleoon's side (but code triggering / implementing variations is already deployed on the web-application's side).
Example code
string visitorCode = kameleoonClient.GetVisitorCode(Request, Response, "example.com");

int experimentID = 75253;
int variationID;

try {
variationID = kameleoonClient.TriggerExperiment(visitorCode, experimentID);
}
catch (KameleoonException.NotTargeted e) {
/*
The user did not trigger the experiment, as the associated targeting segment
conditions were not fulfilled. He should see the reference variation.
*/
variationID = 0;
}
catch (KameleoonException.NotActivated e) {
/*
The user triggered the experiment, but did not activate it. Usually, this happens
because the user has been associated with excluded traffic.
*/
variationID = 0;
}
catch (KameleoonException.ExperimentConfigurationNotFound e) {
/*
This experiment was not found in the SDK configuration. The user will not be counted
into the experiment, but should see the reference variation.
*/
variationID = 0;
}
catch (Exception e) {
// This is generic Exception handler which will handle all exceptions
Console.WriteLine("Exception occured");
}

IsFeatureActive()

note

Previously named: ActivateFeature - deprecated since SDK version 3.1.0 and will be removed in a future release.

To activate a feature toggle, call the IsFeatureActive method of our SDK.

This method takes a visitorCode and featureKey (or featureID) as mandatory arguments to check if the specified feature will be active for a given user.

If such a user has never been associated with this feature flag, the SDK returns a boolean value randomly (true if the user should have this feature or false if not). If a user with a given visitorCode is already registered with this feature flag, it will detect the previous featureFlag value.

You have to make sure that proper error handling is set up in your code as shown in the example to the right to catch potential exceptions.

Arguments
NameTypeDescription
visitorCodestringUnique identifier of the user. This field is mandatory.
featureKeystringKey of the feature you want to expose to a user. This field is mandatory.
Return value
TypeDescription
boolValue of the feature that is registered for a given visitorCode.
Exceptions thrown
TypeDescription
KameleoonException.NotTargetedException indicating that the current visitor / user did not trigger the required targeting conditions for this feature. The targeting conditions are defined via Kameleoon's segment builder.
KameleoonException.FeatureConfigurationNotFoundException indicating that the requested feature ID has not been found in the internal configuration of the SDK. This is usually normal and means that the feature flag has not yet been activated on Kameleoon's side (but code implementing the feature is already deployed on the web-application's side).
string visitorCode = kameleoonClient.GetVisitorCode(Request, Response, "example.com");
string featureKey = "new_checkout";
bool hasNewCheckout = false;

try {
hasNewCheckout = kameleoonClient.IsFeatureActive(visitorCode, featureKey);
}
catch (KameleoonException.NotTargeted e) {
/*
The user did not trigger the feature, as the associated targeting segment
conditions were not fulfilled. The feature should be considered inactive.
*/
hasNewCheckout = false;
}
catch (KameleoonException.FeatureConfigurationNotFound e) {
// Feature toggle not yet activated on Kameleoon's side - we consider the feature inactive
hasNewCheckout = false;
}
catch (Exception e) {
// This is generic Exception handler which will handle all exceptions.
Console.WriteLine("Exception occured");
}
if (hasNewCheckout)
{
// Implement new checkout code here
}

GetFeatureVariationKey()

To get feature variation key, call the GetFeatureVariationKey() method.

This method takes a visitorCode and featureKey as mandatory arguments to get variation key for a given user.

If such a user has never been associated with this feature flag, the SDK returns a variation key randomly (according to the feature flag rules). If a user with a given visitorCode is already registered with this feature flag, it will detect the previous variation key value. If the user does not match any of the rules, the default value will be returned, which we can define in your customer's account.

You have to make sure that proper error handling is set up in your code as shown in the example to the right to catch potential exceptions.

Arguments
NameTypeDescription
visitorCodestringUnique identifier of the user. This field is mandatory.
featureKeystringKey of the feature you want to expose to a user. This field is mandatory.
Return value
TypeDescription
stringVariation key of the feature flag that is registered for a given visitorCode.
Exceptions thrown
TypeDescription
KameleoonException.FeatureConfigurationNotFoundException indicating that the requested feature key has not been found in the internal configuration of the SDK. This is usually normal and means that the feature flag has not yet been activated on Kameleoon's side (but code implementing the feature is already deployed on the web-application's side).
string visitorCode = kameleoonClient.GetVisitorCode(Request, Response, "example.com");
string featureKey = "new_checkout";
string variationKey = "";

try {
variationKey = kameleoonClient.GetFeatureVariationKey(visitorCode, featureKey);
} catch (KameleoonException.FeatureConfigurationNotFound e) {
// The error is happened, feature flag isn't found in current configuraiton
}

switch (variationKey) {
case "on":
// Main variation key is selected for visitorCode
break;
case "alternative_variation":
// Alternative variation key
break;
default:
// Default variation key
break;
}

GetFeatureVariable()

To get variable of variation key associated with a user, call the GetFeatureVariable() method of our SDK.

This method takes a visitorCode, featureKey and variableKey as mandatory arguments to get a variable of variation key for a given user.

If such a user has never been associated with this feature flag, the SDK returns a variable value of variation key randomly (according to the feature flag rules). If a user with a given visitorCode is already registered with this feature flag, it will detect the variable value for previous associated variation. If the user does not match any of the rules, the variable of default value will be returned.

You have to make sure that proper error handling is set up in your code as shown in the example to the right to catch potential exceptions.

Arguments
NameTypeDescription
visitorCodestringUnique identifier of the user. This field is mandatory.
featureKeystringKey of the feature you want to expose to a user. This field is mandatory.
variableKeystringKey of the variable you want to get a value. This field is mandatory.
Return value
TypeDescription
objectValue of variable of variation that is registered for a given visitorCode for this feature flag. Possible types: bool, int, double, string, JObject, JArray
Exceptions thrown
TypeDescription
KameleoonException.FeatureConfigurationNotFoundException indicating that the requested feature key has not been found in the internal configuration of the SDK. This is usually normal and means that the feature flag has not yet been activated on Kameleoon's side (but code implementing the feature is already deployed on the web-application's side).
Example code
var visitorCode = kameleoonClient.GetVisitorCode(req, res, "example.com");
const string featureKey = "feature_key";
const string variableKey = "var"

try {
var variableValue = kameleoonClient.GetFeatureVariable(visitorCode, featureKey, variableKey);
// Your custom code depending of variableValue
} catch (KameleoonException.FeatureConfigurationNotFound e) {
// The error is happened, feature flag isn't found in current configuraiton
}

GetVariationAssociatedData()

note

The GetVariationAssociatedData() method will be deprecated as SDK and Hybrid type experiments are being merged with feature experimentation in September 2023. Use feature experimentation methods instead.

note

Previously named: ObtainVariationAssociatedData, which was deprecated in SDK version 3.0.0 and will be removed in a future release.

To retrieve JSON data associated with a variation, call the GetVariationAssociatedData() method of our SDK. The JSON data usually represents some metadata of the variation, and can be configured on our web application interface or via our Automation API.

This method takes the variationID as a parameter and will return the data as a JObject instance. It will throw an exception (KameleoonException.VariationConfigurationNotFound) if the variation ID is wrong or corresponds to an experiment that is not yet online.

note

Kameleoon uses the Newtonsoft.Json package as a JSON provider / library. This adds a dependency to our SDK.

Arguments
NameTypeDescription
variationIDintID of the variation you want to obtain associated data for. This field is mandatory.
Return value
TypeDescription
Newtonsoft.Json.Linq.JObjectData associated with this variationID.
Exceptions thrown
TypeDescription
KameleoonException.VariationConfigurationNotFoundException indicating that the requested variation ID has not been found in the internal configuration of the SDK. This is usually normal and means that the variation's corresponding experiment has not yet been activated on Kameleoon's side.
Example code
string visitorCode = kameleoonClient.GetVisitorCode(Request, Response, "example.com");
int experimentID = 75253;

try {
int variationID = kameleoonClient.TriggerExperiment(visitorCode, experimentID);
JObject jsonObject = kameleoonClient.GetVariationAssociatedData(variationID);
string firstName = jsonObject.getString("firstName");
}
catch (KameleoonException.VariationConfigurationNotFound e) {
// The variation is not yet activated on Kameleoon's side, ie the associated experiment is not online
}
catch (Exception e) {
// This is generic Exception handler which will handle all exceptions
Console.WriteLine("Exception occured");
}

ObtainFeatureVariable()

To retrieve a feature variable, call the ObtainFeatureVariable() method of our SDK. A feature variable can be changed easily via our web application.

This method takes two input parameters: featureKey and variableKey. It will return the data as a System.Object instance. Usually it should be casted to the expected type (the one defined on the web interface). It will throw an exception (KameleoonException.FeatureConfigurationNotFound) if the requested feature has not been found in the internal configuration of the SDK.

note

We decided to use the Newtonsoft.Json package as a JSON provider / library. This adds a dependency to our SDK.

Arguments
NameTypeDescription
featureID or featureKeyint or stringID or Key of the feature you want to obtain to a user. This field is mandatory.
variableKeystringKey of the variable. This field is mandatory.
Return value
TypeDescription
objectData associated with this variable for this feature flag. This can be a int, double, string, bool or Newtonsoft.Json.Linq.JObject (depending on the type defined on the web interface).
Exceptions thrown
TypeDescription
KameleoonException.FeatureConfigurationNotFoundException indicating that the requested feature ID has not been found in the internal configuration of the SDK. This is usually normal and means that the feature flag has not yet been activated on Kameleoon's side.
Example code
string featureKey = "myFeature";
string variableKey = "myVariable";
string data;

try {
data = (string) kameleoonClient.ObtainFeatureVariable(featureKey, variableKey);
}
catch (KameleoonException.FeatureConfigurationNotFound e) {
// The feature is not yet activated on Kameleoon's side
}
catch (Exception e) {
// This is generic Exception handler which will handle all exceptions.
Console.WriteLine("Exception occurred");
}

GetFeatureAllVariables()

note

Previously named: ObtainFeatureAllVariables, which is deprecated since SDK version 3.0.0 and will be removed in a future release.

To retrieve the all feature variables, call the GetFeatureAllVariables() method of our SDK. A feature variable can be changed easily via our web application.

This method takes one input parameter: featureKey. It will return the data with the Dictionary<string, object> type, as defined on the web interface. It will throw an exception (KameleoonException.FeatureConfigurationNotFound) if the requested feature has not been found in the internal configuration of the SDK.

Arguments
NameTypeDescription
featureKeystringIdentificator key of the feature you need to obtain. This field is mandatory.
Return value
TypeDescription
Dictionary<string, object>Data associated with this feature flag. The values of can be a number, string, boolean or object (depending on the type defined on the web interface).
Exceptions thrown
TypeDescription
KameleoonException.FeatureConfigurationNotFoundException indicating that the requested feature has not been found in the internal configuration of the SDK. This is usually normal and means that the feature flag has not yet been activated on Kameleoon's side.
Example code
string featureKey = "myFeature";

try {
var allVariables = kameleoonClient.GetFeatureAllVariables(featureKey);
}
catch (KameleoonException.FeatureConfigurationNotFound e) {
// The feature is not yet activated on Kameleoon's side
}
catch (Exception e) {
// This is generic Exception handler which will handle all exceptions.
Console.WriteLine("Exception occurred");
}

TrackConversion()

To track conversion, use the TrackConversion() method. This method requires visitorCode and goalID to track conversion on this particular goal. In addition, this method also accepts revenue as a third optional argument to track revenue. The visitorCode usually is identical to the one that was used when triggering the experiment.

The TrackConversion() method doesn't return any value. This method is non-blocking as the server call is made asynchronously.

Arguments
NameTypeDescription
visitorCodestringUnique identifier of the user. This field is mandatory.
goalIDintID of the goal. This field is mandatory.
revenuefloatRevenue of the conversion. This field is optional.
Example code
using Kameleoon;
using Kameleoon.Data;

string visitorCode = kameleoonClient.GetVisitorCode(Request, Response, "example.com");
int goalID = 83023;

kameleoonClient.AddData(visitorCode, new Browser(Browser.Browsers.CHROME));
kameleoonClient.AddData(
visitorCode,
new PageView("http://url.com", "title", new int[] {3}),
new Interest(2)
);
kameleoonClient.AddData(visitorCode, new Conversion(32, 10f, false));

kameleoonClient.TrackConversion(visitorCode, goalID);

AddData()

To associate various data with the current user, we can use the AddData() method. This method requires the visitorCode as a first parameter, and then accepts several additional parameters. Those additional parameters represent the various Data Types allowed in Kameleoon.

Note that the AddData() method doesn't return any value and doesn't interact with the Kameleoon back-end servers by itself. Instead, the declared data is saved for future sending via the Flush() method described in the next paragraph. This reduces the number of server calls made, as data is usually grouped into a single server call triggered by the execution of Flush().

note

The TriggerExperiment() and TrackConversion() methods also send out previously associated data, just like the Flush() method.

Arguments
NameTypeDescription
visitorCodestringUnique identifier of the user. This field is mandatory.
dataTypesIDataCustom data types which may be passed separated by a comma.
Example code
kameleoonClient.AddData(new Browser(Browser.Browsers.CHROME));
kameleoonClient.AddData(
visitorCode,
new PageView("https://url.com", "title", new int[] {3}),
new Interest(0)
);
kameleoonClient.AddData(visitorCode, new Conversion(32, 10f, false));

Flush()

Data associated with the current user via AddData() method is not sent immediately to the server. It is stored and accumulated until it is sent automatically by the TriggerExperiment() or TrackConversion() methods, or manually by the Flush() method. This allows the developer to control exactly when the data is flushed to our servers. For instance, if you call the AddData() method a dozen times, it would be a waste of ressources to send data to the server after each AddData() invocation. Just call Flush() once at the end.

The Flush() method doesn't return any value. This method is non-blocking as the server call is made asynchronously.

Arguments
NameTypeDescription
visitorCodestringUnique identifier of the user. This field is mandatory.
Example code
string visitorCode = kameleoonClient.GetVisitorCode(Request, Response, "example.com");

kameleoonClient.AddData(new Browser(Browser.Browsers.CHROME));
kameleoonClient.AddData(
visitorCode,
new PageView("https://url.com", "title", new int[] {3}),
new Interest(0)
);
kameleoonClient.AddData(visitorCode, new Conversion(32, 10f, false));

kameleoonClient.Flush(visitorCode);

GetExperimentList()

note

The GetExperimentList() method will be deprecated as SDK and Hybrid type experiments are being merged with feature experimentation in September 2023. Use feature experimentation methods instead.

note

Previously named: ObtainExperimentList - deprecated since SDK version 3.0.0 and will be removed in a future release.

Returns a list of experiment IDs currently available for the SDK

Return value
TypeDescription
List<int>List of experiment's IDs

GetExperimentListForVisitor()

note

The GetExperimentListForVisitor() method will be deprecated as SDK and Hybrid type experiments are being merged with feature experimentation in September 2023. Use feature experimentation methods instead.

var experimenListIds = kameleoonClient.GetExperimentListForVisitor(visitorCode) // onlyAllocated == true by default

var experimenListIds = kameleoonClient.GetExperimentListForVisitor(visitorCode, false)
note

Previously named: ObtainExperimentListForVisitorCode - deprecated since SDK version 3.0.0 and will be removed in a future release.

This method takes two input parameters: visitorCode and onlyActive. If onlyActive parameter is true result contains only active experiments, otherwise it contains all targeted experiments to specific visitorCode. Returns a list of experiment IDs currently available for specific visitorCode according to the onlyActive option

Arguments
NameTypeDescription
visitorCodestringUnique identifier of the user. This field is mandatory.
onlyAllocatedboolThe value is true by default, result contains only active for the user experiments. Set false for fetching all targeted experiment IDs. This field is optional.
Return value
TypeDescription
List<int>List of experiment IDs available for specific visitorCode according to the onlyActive option
Example code
var experimentIds = kameleoonClient.GetExperimentList()

GetFeatureList()

note

Previously named: ObtainFeatureList - deprecated since SDK version 3.0.0 and will be removed in a future release.

Returns a list of feature flag IDs currently available for the SDK

Return value
TypeDescription
List<int>List of feature flag IDs
Example code
const featureFlagIds = kameleoonClient.GetFeatureList()

GetActiveFeatureListForVisitor()

note

Previously named: ObtainFeatureListForVisitorCode - deprecated since SDK version 3.0.0 and will be removed in a future release.

This method takes two input parameters: visitorCode and onlyActive. If onlyActive parameter is true result contains only active feature flags, otherwise it contains all targeted feature flags to specific visitorCode. Returns a list of feature flag IDs currently available for specific visitorCode according to the onlyActive option

Arguments
NameTypeDescription
visitorCodestringUnique identifier of the user. This field is mandatory.
Return value
TypeDescription
List<string>List of active feature flag IDs available for specific visitorCode
Example code
var onlyActive = true
var featureListIds = kameleoonClient.GetActiveFeatureListForVisitor(visitorCode)

GetRemoteData()

\

note

Previously named: RetrieveDataFromRemoteSource, which is deprecated since SDK version 3.1.0 and will be removed in a future release.

The GetRemoteData() method allows you to retrieve data (according to a key passed as argument) for specified siteCode (specified in KameleoonClientFactory.create()) stored on a remote Kameleoon server. Usually data will be stored on our remote servers via the use of our Data API. This method, along with the availability of our highly scalable servers for this purpose, provides a convenient way to quickly store massive amounts of data that can be later retrieved for each of your visitors / users.

Note that since a server call is required, this mechanism is asynchronous.

Arguments
NameTypeDescription
keystringThe key that the data you try to get is associated with. This field is mandatory.
timeoutintTimeout (in milliseconds). This parameter specifies the maximum amount of time to wait for a result. This field is optional, if not provided, it will use the default value of 7500 milliseconds.
Return value
TypeDescription
JObjectData associated with retrieving data for specific key.
Exceptions thrown
TypeDescription
ExceptionException indicating that the request timed out or retrieved data can't be parsed with JObject.Parse method.
Example code
var testValue = await kameleoonClient.GetRemoteData("test"); // default timeout
testValue = await kameleoonClient.GetRemoteData("test", 1000);
try {
testValue = await kameleoonClient.GetRemoteData("test");
} catch (Exception e) {
// Timeout or Json Parsing Exception
}

GetRemoteVisitorData()

The GetRemoteVisitorData() method allows you to retrieve data assigned to a visitor (according to a visitorCode passed as argument) for specified siteCode (specified in KameleoonClientFactory.Create()) stored on a remote Kameleoon server. Method automatically adds retrieved data for a visitor (accroding to a addData parameter). Usually data will be stored on our remote servers via the use of our Data API. This method, along with the availability of our highly scalable servers for this purpose, provides a convenient way to quickly store massive amounts of data that can be later retrieved for each of your visitors / users.

Arguments
NameTypeDescription
visitorCodestringThe visitor code for which you want to retrieve the assigned data. This field is mandatory.
addDataboolA boolean indicating whether the method should automatically add retrieved data for a visitor. This field is optional.
timeoutintTimeout (in milliseconds). This parameter specifies the maximum amount of time to wait for a result. This field is optional, if not provided, it will use the default value of 7500 milliseconds.
Return value
TypeDescription
Task<IReadOnlyCollection<IData>>Collection associated with a given visitor.
Exceptions thrown
TypeDescription
HttpRequestExceptionException indicating that the request was failed for any reason.
ExceptionException indicating that the request timed out or any other reason of failure.
Example code
string visitorCode = "visitorCode";
// Visitor data will be fetched and automatically added for `visitorCode`
Task<IReadOnlyCollection<IData>> visitorData = kameleoonClient.GetRemoteVisitorData(visitorCode);

// If you only want to fetch data and add it yourself manually, set addData == `false`
Task<IReadOnlyCollection<IData>> visitorData = kameleoonClient.GetRemoteVisitorData(visitorCode, false);

try {
IReadOnlyCollection<IData> visitorData = await kameleoonClient.GetRemoteVisitorData(visitorCode);
// Your custom code
} catch (Exception e) {
// Catch exception
}

UpdateConfigurationHandler()

The UpdateConfigurationHandler() method allows you to handle the event when configuration has updated data. It takes one input parameter handler. The handler that will be called when the configuration is updated using a real-time configuration event.

Arguments
NameTypeDescription
handlerActionThe handler that will be called when the configuration is updated using a real-time configuration event.
Example code
kameleoonClient.UpdateConfigurationHandler(async delegate () {
// Configuration was updated
});

GetEngineTrackingCode()

Kameleoon offers built-in integrations with various analytics solutions, such as Mixpanel, GA4, and Segment. To ensure that you can track and analyze your server-side experiments, Kameleoon provides the GetEngineTrackingCode() method that allows you to automatically send exposure events to your analytics solution. For more information on how to implement this method, see the Hybrid experimentation documentation.

note

To benefit from this feature, you will need to implement both the C# SDK and our Kameleoon JavaScript tag. We recommend you implement the Kameleoon Asynchronous tag, which you can install before your closing <body> tag in your HTML page, as it will be only used for tracking purposes.

Arguments
NameTypeDescription
visitorCodestringThe user's unique identifier. This field is mandatory.
Return value
TypeDescription
stringJavasScript code to be inserted in your page
Example code
string engineTrackingCode = kameleoonClient.GetEngineTrackingCode(visitorCode);
// The following string will be returned:
//
// window.kameleoonQueue = window.kameleoonQueue || [];
// window.kameleoonQueue.push(['Experiments.assignVariation', experiment1ID, variation1ID]);
// window.kameleoonQueue.push(['Experiments.trigger', experiment1ID, true]);
// window.kameleoonQueue.push(['Experiments.assignVariation', experiment2ID, variation2ID]);
// window.kameleoonQueue.push(['Experiments.trigger', experiment2ID, true]);
//
// Here, experiment1ID, experiment2ID and variation1ID, variation2ID represent
// the specific experiments and variations that users have been assigned to.

Kameleoon.Data.IData

Browser

kameleoonClient.AddData(visitorCode, new Browser(Browser.Browsers.CHROME));

kameleoonClient.AddData(visitorCode, new Browser(Browser.Browsers.SAFARI, 16));
NameTypeDescription
browserBrowser.BrowsersList of browsers: CHROME, INTERNET_EXPLORER, FIREFOX, SAFARI, OPERA, OTHER. This field is mandatory.
versionfloatVersion of browser. This field is optional.

PageView

NameTypeDescription
urlstringURL of the page viewed. This field is mandatory.
titlestringTitle of the page viewed. This field is mandatory.
referrersint[]Referrers of viewed pages. This field is optional.
note

The index (ID) of the referrer is available on our Back-Office, in the Acquisition channel configuration page. Be careful: this index starts at 0, so the first acquisition channel you create for a given site would have the ID 0, not 1.
https://help.kameleoon.com/create-acquisition-channel

kameleoonClient.AddData(
visitorCode,
new PageView("https://url.com", "title", new int[] {3})
);

Conversion

NameTypeDescription
goalIDintID of the goal. This field is mandatory.
revenuefloatConversion revenue. This field is optional.
negativeboolDefines if the revenue is positive or negative. This field is optional.
kameleoonClient.AddData(visitorCode, new Conversion(32, 10f, false));

CustomData

NameTypeDescription
indexintIndex / ID of the custom data to be stored. This field is mandatory.
valuestringValue of the custom data to be stored. This field is mandatory.
note

The index (ID) of the custom data is available on our Back-Office, in the Custom data configuration page. Be careful: this index starts at 0, so the first custom data you create for a given site would have the ID 0, not 1.

kameleoonClient.AddData(visitorCode, new CustomData(1, "some custom value"));

Device

NameTypeDescription
deviceDevice.TypeList of devices: PHONE, TABLET, DESKTOP. This field is mandatory.
kameleoonClient.AddData(visitorCode, new Device(Device.Type.DESKTOP));

UserAgent

NameTypeDescription
valuestringThe User-Agent value that will be sent with tracking requests. This field is mandatory.
kameleoonClient.AddData(visitorCode, new UserAgent("Your User Agent"));