Installation
Load the library with a<script> tag, setting window.TS before the library loads:
@latest or an unversioned URL.
With npm
window.TS in a separate file and import that file first:
window.TS after the library has loaded. To set the token later, assign window.TS.token instead.
Configuration
Set these onwindow.TS before the library loads:
token(required): your Topsort Marketplace API key, which starts withTSE_. Create it in the Topsort app under Tools → Integrations → Topsort API. Events are queued until it is set.url(optional): the Topsort API URL. Defaults tohttps://api.topsort.com.cookieName(optional): the cookie that stores the user ID. Defaults totsuid.getUserId(optional): a function that returns your own user ID as a string. You are responsible for generating and persisting it.
window.TS.setUserId(id), which stores an ID in the cookie for events created after the call, and window.TS.resetUserId(). When you provide getUserId, the cookie and setUserId are not used for event IDs.
Marking Promoted Elements
To allow Topsort to automatically track renders, clicks, impressions and purchases, use the HTML elementdata-ts-* to mark promoted elements in the page.
Organic (non-promoted) elements
Promoted elements
Render tracking
The library reports a render event as soon as a promoted element is inserted into the page, and an impression later, when that element becomes visible. Renders use the samedata-ts-resolved-bid attribute, so no extra markup is needed:
resolvedBidId, so they do not fire for organic elements or for data-ts-resolved-bid="inherit". Do not also report renders, impressions or clicks for these elements with a Topsort SDK, or they are counted twice.
When events fire
- Impression: reported when at least 50% of the element is in the viewport and painted (not hidden by
display:none,visibility:hidden,opacity:0orcontent-visibility:hidden, on the element or an ancestor) continuously for 1 second. The timer restarts if the element leaves the viewport or the tab is hidden. Each element reports at most one impression. - Click: primary clicks only, so middle clicks are not reported. A click is reported once per product and bid on a given page.
- Deduplication: within one page load, each route, event type, product, bid and purchase items combination is reported once. The route is the path plus the query string (or the hash, for hash routers). A page reload resets this.
- Delivery: a request that fails with a network error is dropped. The library retries only when the API asks it to.
Clickable areas
Usedata-ts-clickable to indicate what areas should trigger a click event:
Banner attribution
On the banner’s destination page, mark each product featured in the banner so that its events are attributed to the banner. The library attaches the most recently clicked sponsored bid in the current browser tab to the product.data-ts-product is required alongside inherit (without it, the literal value inherit is sent as the bid). If no sponsored element was clicked earlier in the tab, the event is sent without a bid. inherit applies to clicks and impressions, never to renders.
Purchase tracking
Usedata-ts-items JSON array to track purchases. Place the element on your order confirmation page. The purchase is reported when the element is inserted into the page, not when it is clicked, and reloading the page reports it again, so render it once per order.
Each item has product (the product ID), quantity, price and an optional vendorId (needed for halo attribution, and recommended when a product is sold by more than one vendor). price is the unit price as a decimal in your marketplace currency, for example 23.99, not 2399.
End to End Example
This example also uses banners.js for thetopsort-banner tag.