PHP SDK
Getting Started
The Taplytics PHP SDK can be found through the Taplytics PHP GitHub repository. This SDK enables the delivery of server-side experiments, feature flags and functionality at edge.
The generated code has dependencies over external libraries like UniRest. These dependencies are defined in the composer.json file that comes with the SDK. To resolve these dependencies, we use the Composer package manager which requires PHP greater than 5.3.2 installed in your system. Visit https://getcomposer.org/download/ to download the installer file for Composer and run it in your system. Open command prompt and type composer --version. This should display the current version of the Composer installed if the installation was successful.
Using command line, navigate to the directory containing the generated files (including composer.json
) for the SDK.
Run the command composer install
. This should install all the required dependencies and create the vendor directory in your project directory.
Initialization
API client can be initialized as following.
$client = new TaplyticsLib\TaplyticsClient();
Class Reference
APIController
Get singleton instance
The singleton instance of the APIController
class can be accessed from the API Client.
$client = $client->getClient();
createGetVariables
All variables and their values for the given user
function createGetVariables(
$token,
$userId,
$body = null)
Parameters
Parameter | Tags | Description |
---|---|---|
token | Required | SDK token for the project |
userId | Required | ID for given user |
body | Optional | All relevant attributes associated with the user |
Example Usage
$token = 'token';
$userId = 'user_id';
$body = new Body();
$result = $client->createGetVariables($token, $userId, $body);
createGetVariationForExperiment
For a given experiment, determine whether or not a user is in the experiment, and in which variation
function createGetVariationForExperiment(
$token,
$userId,
$experimentName,
$body = null)
Parameters
Parameter | Tags | Description |
---|---|---|
token | Required | SDK token for the project |
userId | Required | ID for given user |
experimentName | Required | Name of an Experiment |
body | Optional | All relevant attributes associated with the user |
Example Usage
$token = 'token';
$userId = 'user_id';
$experimentName = 'experimentName';
$body = new Body();
$result = $client->createGetVariationForExperiment($token, $userId, $experimentName, $body);
createGetVariableValue
Value for given Taplytics Dynamic Variable. If a user is not in an experiment containing the variable, the response be the provided default value in the query params.
function createGetVariableValue(
$token,
$userId,
$varName,
$defaultValue,
$body = null)
Parameters
Parameter | Tags | Description |
---|---|---|
token | Required | SDK token for the project |
userId | Required | ID for given user |
varName | Required | name of variable |
defaultValue | Required | default value to be used if user does not have variable available. |
body | Optional | All relevant attributes associated with the user |
Example Usage
$token = 'token';
$userId = 'user_id';
$varName = 'varName';
$defaultValue = 'defaultValue';
$body = new Body();
$result = $client->createGetVariableValue($token, $userId, $varName, $defaultValue, $body);
createGetBucketing
Returns a key/value pairing of all experiments that the user has been segmented into, as well as the variation the users are assigned.
function createGetBucketing(
$token,
$userId,
$body = null)
Parameters
Parameter | Tags | Description |
---|---|---|
token | Required | SDK token for the project |
userId | Required | ID for current user |
body | Optional | Provide all relevant attributes associated with the user |
Example Usage
$token = 'token';
$userId = 'user_id';
$body = new Body();
$result = $client->createGetBucketing($token, $userId, $body);
postEvent
Send an event to Taplytics. These events are then used to compare against an experiment's goals to determine the success of an A/B test.
function postEvent(
$token,
$userId,
$body = null)
Parameters
Parameter | Tags | Description |
---|---|---|
token | Required | SDK token for the project |
userId | Required | ID for given user |
body | Optional | Provide an array of events, as well as all additional relevant user attributes. |
Example Usage
$token = 'token';
$userId = 'user_id';
$body = array(
'events' => array(
array('eventName' => 'event name!', 'eventValue' => 5)
)
);
$result = $client->postEvent($token, $userId, $body);
The format for passing in events is as follows:
{
attributes: {
name: '',
...
},
events: [
{eventName: '', eventValue},
...
]
}
createGetConfig
Returns the entire configuration for the project. This is the document that informs the experiment information such as segmentation. Extremely verbose and should be used for debugging.
function createGetConfig(
$token,
$userId,
$body = null)
Parameters
Parameter | Tags | Description |
---|---|---|
token | Required | SDK token for the project |
userId | Required | ID for given user |
body | Optional | All relevant attributes associated with the user |
Example Usage
$token = 'token';
$userId = 'user_id';
$body = new Body();
$result = $client->createGetConfig($token, $userId, $body);
createGetFeatureFlags
Returns the list of feature flags with names and key names.
function createGetFeatureFlags(
$token,
$userId,
$body = null)
Parameters
Parameter | Tags | Description |
---|---|---|
token | Required | SDK token for the project |
userId | Required | ID for given user |
body | Optional | All relevant attributes associated with the user |
Example Usage
$token = 'token';
$userId = 'user_id';
$body = new Body();
$result = $client->createGetFeatureFlags($token, $userId, $body);
foreach($result as $flagObj) {
// $flagObj->name to get the name of the feature flag
// $flagObj->keyName to get the key of the feature flag
}
isFeatureFlagEnabled
Returns true or false based on if the keyName passed in is an enabled feature flag.
function isFeatureFlagEnabled(
$token,
$userId,
$keyName,
$body = null)
Parameters
Parameter | Tags | Description |
---|---|---|
token | Required | SDK token for the project |
userId | Required | ID for given user |
keyName | Required | key name for the feature flag |
body | Optional | All relevant attributes associated with the user |
Example Usage
$token = 'token';
$userId = 'user_id';
$keyName = 'featureFlagKey';
$body = new Body();
$result = $client->isFeatureFlagEnabled($token, $userId, $keyName, $body);
if ($result) {
showFeature();
}
Updated almost 3 years ago