PHP code example of simpleab / sdk-php

1. Go to this page and download the library: Download simpleab/sdk-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

simpleab / sdk-php example snippets




impleAB\SDK\SimpleABSDK;
use SimpleAB\SDK\BaseAPIUrls;
use SimpleAB\SDK\Stages;
use SimpleAB\SDK\AggregationTypes;
use SimpleAB\SDK\Segment;

// Initialize the SDK
$apiURL = BaseAPIUrls::CAPTCHIFY_NA;
$apiKey = 'your-api-key';
$sdk = new SimpleABSDK($apiURL, $apiKey);

// Get a treatment for an experiment
$experimentID = 'your-experiment-id';
$stage = Stages::PROD;
$dimension = 'default';
$allocationKey = 'user-123';

$treatment = $sdk->getTreatment($experimentID, $stage, $dimension, $allocationKey);

// Use the treatment in your application
if ($treatment === 'T1') {
    // Show variant 1
} elseif ($treatment === 'T2') {
    // Show variant 2
} else {
    // Show control or default version
}

// Track a metric
$sdk->trackMetric([
    'experimentID' => $experimentID,
    'stage' => $stage,
    'dimension' => $dimension,
    'treatment' => $treatment,
    'metricName' => 'conversion',
    'metricValue' => 1,
    'aggregationType' => AggregationTypes::SUM
]);

// Get a segment for a user
$segment = $sdk->getSegment([
    'ip' => '123.45.67.89',
    'userAgent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
]);

// Get a treatment using the segment
$treatmentWithSegment = $sdk->getTreatmentWithSegment($experimentID, $stage, $segment, $allocationKey);

// Track a metric using the segment
$sdk->trackMetricWithSegment([
    'experimentID' => $experimentID,
    'stage' => $stage,
    'segment' => $segment,
    'treatment' => $treatmentWithSegment,
    'metricName' => 'conversion',
    'metricValue' => 1,
    'aggregationType' => AggregationTypes::SUM
]);

// Make sure to flush metrics before your script ends
$sdk->flush();

public function __construct($apiURL, $apiKey, $experiments = [])

public function getTreatment($experimentID, $stage, $dimension, $allocationKey)

public function trackMetric($params)

public function getSegment($options = [])

public function getTreatmentWithSegment($experimentID, $stage, $segment, $allocationKey)

public function trackMetricWithSegment($params)

public function flush()

class Segment
{
    public $countryCode;
    public $region;
    public $deviceType;

    public function __construct($countryCode = '', $region = '', $deviceType = '')
    {
        $this->countryCode = $countryCode;
        $this->region = $region;
        $this->deviceType = $deviceType;
    }
}
bash
composer 

   ./vendor/bin/phpunit tests/SimpleABSDKTest.php