PHP code example of dregs / dregs-sdk

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

    

dregs / dregs-sdk example snippets


use Dregs\Client;

$client = new Client($_ENV['DREGS_SECRET_KEY']);

$client->track(
    'user.signup',
    identity: 'user_12345',
    data: ['plan' => 'pro', 'referrer' => 'partner-x'],
    identityData: ['email' => '[email protected]', 'name' => 'Ada Lovelace'],
);

$client->track('purchase', identity: 'user_12345', eventId: "order-{$order->id}");

$result = $client->track('user.signup', identity: 'user_12345');

$result->isAccepted();  // true when Dregs recorded the event
$result->id;            // the event's id

$scores = $client->identities->scores('user_12345');

$scores->humanity();      // 85
$scores->authenticity();  // 72
$scores->uniqueness();    // 91
$scores->behavior();      // 68

if ($scores->authenticity() !== null && $scores->authenticity() < 40) {
    holdForReview('user_12345');
}

$analysis = $client->identities->analysis('user_12345');

foreach ($analysis->observations() as $observation) {
    echo "{$observation->label}: {$observation->explanation} (value {$observation->value})", PHP_EOL;
}

$identity = $client->identities->get('user_12345');

$identity->displayEmail;   // "[email protected]"
$identity->humanityScore;  // 85
$identity->badges;         // [Badge(name: "Account Takeover Suspected", ...)]
$identity->data;           // every attribute you have sent

$client->identities->analyze('user_12345');

use Dregs\Exception\DregsException;
use Dregs\Exception\QuotaExceededException;
use Dregs\Exception\RateLimitException;

try {
    $client->track('user.signup', identity: 'user_12345');
} catch (QuotaExceededException) {
    // Over the monthly event limit; the event was not queued.
} catch (RateLimitException $exception) {
    // Ingesting too fast. $exception->retryAfter when the server said how long.
} catch (DregsException $exception) {
    // Anything else this library throws.
}

$client = new Client(maxRetries: 5);  // or 0 to handle it yourself

final class TrackSignup implements ShouldQueue
{
    public function __construct(private readonly string $identity, private readonly array $attributes)
    {
    }

    public function handle(Client $dregs): void
    {
        $dregs->track('user.signup', identity: $this->identity, identityData: $this->attributes);
    }
}

use Dregs\Exception\WebhookVerificationException;
use Dregs\Webhooks;

try {
    $event = Webhooks::verify(
        $request->getContent(),
        $request->headers->get(Webhooks::SIGNATURE_HEADER) ?? '',
        $_ENV['DREGS_WEBHOOK_SECRET'],
    );
} catch (WebhookVerificationException) {
    return new Response(status: 400);
}

handle($event);

$client = new Dregs\Client(
    secretKey: null,   // defaults to $DREGS_SECRET_KEY
    baseUrl: null,     // defaults to $DREGS_BASE_URL, then https://dregs.com/api
    timeout: 10.0,     // seconds
    maxRetries: 2,
    httpClient: null,  // a Dregs\Http\Transport, or your own PSR-18 client
);

// Symfony's PSR-18 client is its own PSR-17 factory, so it needs nothing alongside it.
$client = new Dregs\Client(httpClient: new Symfony\Component\HttpClient\Psr18Client());

// Guzzle needs the factories naming.
$factory = new GuzzleHttp\Psr7\HttpFactory();

$client = new Dregs\Client(
    httpClient: new GuzzleHttp\Client(),
    requestFactory: $factory,
    streamFactory: $factory,
);