You're Invited:Meet the Socket Team at RSAC and BSidesSF 2026, March 23–26.RSVP
Socket
Book a DemoSign in
Socket

@topsort/banners

Package Overview
Dependencies
Maintainers
2
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@topsort/banners

A web component for displaying Topsort banner ads.

Source
npmnpm
Version
0.6.0
Version published
Weekly downloads
30
-47.37%
Maintainers
2
Weekly downloads
 
Created
Source

version downloads license GitHub Repo stars

Topsort Banner Ad Web component

Usage

Directly from unpkg.com

<script
  async
  type="module"
  src="https://unpkg.com/@topsort/banners@0.6.0/dist/banners.mjs"
></script>
<script async type="module" src="https://unpkg.com/@topsort/analytics.js"></script>
<script>
  // Set API key for auctions and events
  window.TS = {
    token: "<your topsort api key>",
  };
  // Custom behavior can be configured for each site.
  window.TS_BANNERS = {
    // handle the destination link
    getLink(banner) {
      return `https://example.com/${banner.id}`;
    },
    // handle loading/fetching state
    getLoadingElement() {
      const el = document.createElement("div");
      el.innerText = "Loading...";
      return el;
    },
    // handle errors
    getErrorElement() {
      const el = document.createElement("div");
      el.innerText = "Error loading banner";
      return el;
    },
  };
</script>
<body>
  <topsort-banner width="600" height="400" id="<your slot id>"></topsort-banner>
</body>

Rendering multiple banners with one slot ID

You can render multiple banners using the same slot ID and dimensions by setting up a banner context. This is useful when you want to run an auction with multiple results. To do that you have to pass the attribute context="true" to the topsort-banner and use topsort-banner-slot as children elements.

<topsort-banner context="true" width="600" height="400" id="<your slot id>">
  <topsort-banner-slot rank="1"></topsort-banner-slot>
  <topsort-banner-slot rank="2"></topsort-banner-slot>
  <topsort-banner-slot rank="3"></topsort-banner-slot>
</topsort-banner>

Banner Attributes

NameTypeDescription
widthNumberBanner width
heightNumberBanner height
idStringThe slot ID for this banner
category-id*Optional StringThe category ID of the current page
category-ids*Optional StringComma (,) separated list of category IDs, the item must match all
category-disjunctions*Optional StringComma (,) separated list of category IDs, the item must match any
search-queryOptional StringThe search query of the current page
locationOptional StringThe location for geotargeting
new-tabOptional BooleanOpens the banner's link in a new tab (defaults to false)
contextOptional BooleanUses the element as a context provider to render multiple banners

* Only one of [category-id, category-ids, category-disjunctions] must be set. If multiple are set, only the first will be considered, in that order.

Banner Slot Attributes

NameTypeDescription
rankNumberThe ranking of the slot. Ranks should be sorted the same as the winning bids. The lower the rank, the higher the bid

Banner Behaviors

Function NameArg typeReturn TypeDescription
getLinkBannerstringGenerates a URL from a banner response
getLoadingElementHTMLElementA custom element to be shown when the banner is loading.
getErrorElementErrorHTMLElementA custom element to be shown when the banner errors.
getNoWinnersElementHTMLElementA custom element to be shown when the auction returns no banner.
getBannerElementBannerHTMLElementA custom clement to be shown when a banner is loaded.

Banner Interface

NameTypeDescription
type"product" | "vendor" | "brand" | "url"The type of the winning entity, represented by the banner.
slotIdstringThe ID of the winning entity. If the entity is of type URL, this is the URL.
resolvedBidIdstringThe corresponding auction ID of the winning entity.
asset[{ url: string }]An array of url linking to the assets of the banner.

Custom User ID (Optional)

If you want to use your own user identification system instead of the automatic opaque user ID, you can override the getUserId function in the window.TS configuration.

Your custom getUserId function should return the user's ID as a string. You are responsible for generating and persisting the ID (e.g., in a cookie or local storage).

window.TS = {
  token: "<your topsort api key>",
  getUserId() {
    // Return your custom user ID
    // This will be used for both auction requests and event reporting
    return globalUserId ?? generateAndStoreUserId();
  },
};

This configuration needs to be set before analytics.js is loaded or imported.

Listening to events

The banner component emits an event when the state changes. You can listen to this event to write custom logic. The various states are loading, ready, error, and nowinners.

document.querySelector('#my-slot-id').addEventListener('statechange', (event) => {
  console.log(event.detail); // { status: 'ready', banner: { ... } }
});

Running the example

You can play around with the provided index.html file. To run it, you'll need to install the dependencies and start a local server.

git clone https://github.com/Topsort/banners.js.git
cd banners.js
pnpm install
pnpm run dev

Remember to add window.TS.token and modify the slot-id attribute with your own values that you can find at Topsort.

Keywords

banners

FAQs

Package last updated on 07 Jan 2026

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts