Skip to main content
Topsort’s analytics.js public library allows marketplaces to easily send Topsort interaction events, such as clicks, impressions and purchases, with a low-code integration. By using a simple HTML tag, the marketplace can connect to Topsort’s ad server and send all tracking events related to an ad.

Installation

Load the library with a <script> tag, setting window.TS before the library loads:
Pin the version in the URL and upgrade deliberately. Do not use @latest or an unversioned URL.

With npm

Imports run before the rest of a module, so set window.TS in a separate file and import that file first:
Do not replace window.TS after the library has loaded. To set the token later, assign window.TS.token instead.

Configuration

Set these on window.TS before the library loads:
  • token (required): your Topsort Marketplace API key, which starts with TSE_. 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 to https://api.topsort.com.
  • cookieName (optional): the cookie that stores the user ID. Defaults to tsuid.
  • getUserId (optional): a function that returns your own user ID as a string. You are responsible for generating and persisting it.
The library also exposes 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 element data-ts-* to mark promoted elements in the page.

Organic (non-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 same data-ts-resolved-bid attribute, so no extra markup is needed:
Renders require an explicit 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:0 or content-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

Use data-ts-clickable to indicate what areas should trigger a click event:
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

Use data-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 the topsort-banner tag.