A PHP Software Development Kit for interacting with the auctions and events endpoints
Topsort.php is a PHP Software Development Kit for Topsort’s auction and event tracking APIs.
Minimum requirements: PHP 7.3 and Composer.
Installation
Install with Composer:
composer require topsort/sdk
Or add to your composer.json:
{ "require": { "topsort/sdk": "^4.0" }}
Running an Auction
create_auction takes the number of slots, the product IDs, their quality scores, a category ID, a search query and the shopper’s opaqueUserId, in that order. Pass null for the arguments you do not use. products takes precedence over category, which takes precedence over searchQuery: a searchQuery is ignored when products or category is passed, and qualityScores only apply with products.
<?phpuse Topsort\SDK;$topsort_client = new SDK("my_api_key");$opaque_user_id = "<YOUR_OPAQUE_USER_ID>"; // The same persistent id for this shopper$products = ["i8bfHPJaxcAb3", "gDG0HV97ed2s"];$slots = 1;$auction_result = $topsort_client->create_auction($slots, $products, null, null, null, $opaque_user_id)->wait();$winners = $auction_result["results"][0]["winners"];
For a banner auction, use create_banner_auction. It takes one array, sent as is: slots and slotId are required, and category (an array such as ["id" => "c_shoes"], not a string), searchQuery, device ("desktop" or "mobile") and opaqueUserId are optional.
Report a render when your server inserts a winner into the page, and an impression when that winner becomes visible to the shopper. Send the same opaqueUserId for a shopper on every auction and event, for example an id you keep in a cookie or your login session. A new id per event breaks attribution. The SDK does not generate one: the report_* methods throw a Topsort\InvalidEventException when it is missing or blank, before sending anything.Each report_* method returns a promise. Always call ->wait() on it: an un-waited promise is not guaranteed to be sent (with the cURL handler Guzzle does not send it, and with the stream handler it is sent immediately and blocks). wait() blocks until Topsort responds, and the SDK sets no request timeout, so where possible report events after the response has been sent to the shopper, for example after fastcgi_finish_request().
If the page also loads analytics.js, it already reports renders, impressions and clicks for every element with data-ts-resolved-bid. Do not report those events again with the SDK.
<?phpuse Topsort\SDK;use Topsort\TopsortException;$topsort_client = new SDK("my_api_key");$opaque_user_id = "<YOUR_OPAQUE_USER_ID>"; // The same id you sent on the auction$placement = ["path" => "/categories/shoes"];$winner = $winners[0]; // A winner from the auction result
Renders
A render records that a sponsored ad was inserted into the page, whether or not it ever becomes visible. Renders are sponsored-only, so report_render takes the resolvedBidId as its first argument and there is no entity field.
An impression records that an ad or product became visible to the shopper. Your server cannot see that, so report it from the request your frontend sends when the element enters the viewport. report_impressions takes one impression per call.
For organic results, send "entity" => ["type" => "product", "id" => "<product id>"] instead of resolvedBidId, in impressions and clicks.
Purchases
A purchase records the products a shopper bought. Topsort attributes it to the ads the shopper interacted with. unitPrice is the price of one unit in your currency’s major units with decimals (12.95, not cents). Each item also accepts an optional vendorId.
Every event also accepts an id, which the SDK generates anew on every call when you omit it, so pass your own stable id (for example the order ID for a purchase) to keep retries idempotent. Events also accept an occurredAtDateTime, which defaults to the current time. See the events API reference for every event field.
Handling Errors
SDK errors extend Topsort\TopsortException, with one exception described below:
Topsort\InvalidEventException is thrown by the report_* call itself when the event is invalid, before anything is sent. Keep the call inside the try, not only the ->wait().
Topsort\TopsortException is thrown by ->wait() when the API rejects the request or cannot be reached. For an API error, the message includes the response body, for example Event creation failed: Content: {"errors":[...]}.
\InvalidArgumentException (GuzzleHttp\Exception\InvalidArgumentException) is thrown by the create_auction, create_banner_auction and report_* calls themselves when the payload cannot be JSON encoded, for example a string that is not valid UTF-8. It is not a TopsortException, so catch it too: catch (TopsortException | \InvalidArgumentException $e).