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.
<?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"];
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, and nothing is sent until you call ->wait() on it. wait() blocks until Topsort responds, 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
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.
Every event also accepts an id, generated by the SDK when you omit it, and an occurredAtDateTime, which defaults to the current time. See the events API reference for every event field.
All SDK errors extend Topsort\TopsortException, so one catch handles them:
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":[...]}.