Skip to main content
The Topsort JavaScript SDK is the official client library to integrate with Topsort’s auction and event tracking APIs. Built in TypeScript, this SDK simplifies the integration, allowing the creation of an end-to-end flow in minutes.

Installation

Install the package from npm and import it in your code:
Or import it from a CDN inside a module script:
The examples on this page are ES modules: they use import and top-level await, so run them in a <script type="module"> or in your bundled code.

Creating an Auction

Reporting Events

Report a render when you insert 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.
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.

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 resolvedBidId is required and there is no entity field.

Impressions

An impression records that an ad or product became visible to the shopper.

Clicks

A click records that the shopper clicked an ad or product.
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.
See the events API reference for every event field.

Retryable Errors

reportEvent resolves with retry: true for 429 and 5xx responses. Other errors, such as a 400 or a network failure, reject the promise, so catch them too:
For full documentation and end-to-end examples, see the GitHub repository.