PHP code example of traffical / sdk

1. Go to this page and download the library: Download traffical/sdk 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/ */

    

traffical / sdk example snippets


use Traffical\Client;
use Traffical\ClientOptions;

$client = new Client(new ClientOptions(
    orgId: 'org_...',
    projectId: 'prj_...',
    env: 'production',
    apiKey: 'traffical_sk_…',
));

// Resolve parameters with defaults as the fallback.
$params = $client->getParams(
    context: ['userId' => 'user-abc', 'country' => 'US'],
    defaults: ['checkout_button_color' => 'blue', 'discount_pct' => 0],
);

$color = $params['checkout_button_color'];

$decision = $client->decide(
    context: ['userId' => 'user-abc'],
    defaults: ['hero_variant' => 'control'],
);

$variant = $decision->assignments['hero_variant'];

// ...render it, then record that the user saw it:
$client->trackExposure($decision);

// Custom analytics events. Optional args (decisionId, value, values, unitKey,
// eventTimestamp) live in a TrackOptions bag:
$client->track('checkout_completed', ['orderId' => 'o-1001'], new Traffical\TrackOptions(
    decisionId: $decision->decisionId,
    value: 49.0,
));

// Flush is automatic on shutdown; call explicitly in worker/CLI contexts:
$client->flushEvents();

// In a long-lived process, close() runs teardown and awaits a final flush:
$client->close();

$client = new Client(new ClientOptions(
    orgId: 'org_...',
    projectId: 'prj_...',
    env: 'production',
    apiKey: 'traffical_sk_…',
    evaluationMode: 'server',
));

use Traffical\Client;
use Traffical\ClientOptions;
use Traffical\Warehouse\WarehouseNativeLogger;

$logger = new WarehouseNativeLogger(function (array $row): void {
    // INSERT $row into your warehouse / CDP / queue.
});

$client = new Client(new ClientOptions(
    orgId: 'org_...',
    projectId: 'prj_...',
    env: 'production',
    apiKey: 'traffical_sk_…',
    assignmentLogger: $logger,
    disableCloudEvents: true, // keep assignment data on your own infra
));

use Traffical\ClientOptions;
use Traffical\Plugins\DebugPlugin;

$options = new ClientOptions(/* ... */, plugins: [new DebugPlugin()]);