Skip to content
Widget Library

Runtime Interface

The WidgetRuntime interface, the only way a widget talks to the outside world.

This page describes WidgetRuntime: what a widget may ask for and what it may report. The source is src/contract/runtime.ts.


interface WidgetRuntime {
readonly capabilities: { addToBasket: boolean; favorites: boolean };
getContext(): WidgetContext;
sendInteraction(interaction: Interaction): void;
addToBasket(item: BasketItem): Promise<BasketResult>;
toggleFavorite(item: FavoriteItem): Promise<FavoriteResult>;
observeVisibility(root: unknown, onVisible: () => void): () => void;
getExtension(extensionId: string): WidgetExtension | undefined;
getProductCard(): ProductCard | undefined;
deliverData(delivery: DataDelivery): void;
reportError(error: WidgetError): void;
}

The library defines it. On the web @segmentify/integration implements it; on mobile the platform SDK does; in tests createFakeRuntime() does.


Member What the widget uses it for
capabilities Whether this site can add to the basket or to favourites. A button is only drawn when the capability is true.
getContext() The device, page type, language, currency and region of the current page view.
sendInteraction() Sends one event. See Events and Display Rules.
addToBasket() Sends the basket event and asks the site’s adapter to add the product. Resolves to 'added' or 'failed'.
toggleFavorite() Asks the site’s adapter to add or remove a favourite and sends the favourite event for what happened: favorite:add after 'added', favorite:remove after 'removed', nothing after 'failed'. Resolves to 'added', 'removed' or 'failed'.
observeVisibility() Calls back once when the widget becomes visible. Returns a function that stops watching.
getExtension() The registered per-account extension with this id, or undefined. A platform without extensions always answers undefined. See Extensions.
getProductCard() The account’s product card, or undefined. Without one the widget draws the library card. A platform without account cards always answers undefined. See The Account’s Product Card.
deliverData() In “data only” delivery, hands the data and its event functions to the site or app. See Data Only Delivery.
reportError() Reports a problem the widget cannot fix itself.

interface WidgetContext {
device: 'PC' | 'mobile' | 'tablet' | 'Unknown';
pageType: string; // 'Home Page', 'Product Page', ...
language: string;
currency: string;
region: string;
}

The widget never works these out itself. It does not read the user agent, the URL or the lang attribute of the page. The language in particular comes from the place the integration’s initialising class was told to take it from; see Languages and Texts.


interface Interaction {
type:
| 'impression'
| 'widget-view'
| 'click'
| 'slider:arrow:left'
| 'slider:arrow:right';
instanceId: string;
interactionId: string;
}

The type values are the ones @segmentify/integration already uses. Forwarding an interaction to Google Analytics is the runtime’s job; a widget does not know about analytics tools.


Code Reported when
UNSUPPORTED_SCHEMA The config has a schemaVersion this version of the library does not know.
DIV_SELECTOR_NOT_FOUND The placement target did not appear within 50 lookups, 100 ms apart. The name is the web SDK’s alert type, so a runtime can send it as event:alert unchanged.
EXTENSION_NOT_FOUND The config names an extension the runtime does not have.
EXTENSION_FAILED The extension threw, or answered with something it may not.
PRODUCT_CARD_FAILED The account’s product card threw, or answered with something it may not. The widget drew the library card instead.
BASKET_FAILED Adding to the basket failed or threw.
FAVORITE_FAILED Adding to favourites failed or threw.

With delivery: 'data' the site or app draws the recommendations itself. The widget draws nothing and calls deliverData() once with this handle:

interface DataDelivery {
instanceId: string;
title: string; // chosen by the context language
products: Product[];
observeView(root: unknown): void;
reportClick(productId: string): void;
addToBasket(productId: string, quantity?: number): Promise<DataBasketResult>;
}
type DataBasketResult = 'added' | 'failed' | 'unknown-product' | 'unavailable';
Member What it does
title, products What to draw. products is empty when a display rule fails or there is nothing to recommend; the site should then hide its area.
observeView(root) Takes the element or view the site drew the data into. Sends widget-view once, when the runtime sees it visible. Only the first call is used.
reportClick() Sends click for a delivered product. Any other id sends nothing.
addToBasket() Sends click, then adds the product to the basket; quantity is 1 when left out.
addToBasket() resolves to Meaning
'added' The product is in the basket.
'failed' The site’s basket refused or threw. Also reported as BASKET_FAILED.
'unknown-product' The id is not one of the delivered products. Nothing was sent.
'unavailable' This runtime cannot add to the basket (capabilities.addToBasket is false). Nothing was sent.

The last two are not failures: nothing was tried, so no error is reported.

This follows the callback delivery of the web SDK: the impression is sent first, then the site receives the title, the products and a way to have its element watched for widget-view.

The site sends its events through the handle, so they stay the same events a drawn widget sends.


  • Config Schema describes what the widget is given.
  • Testing shows how the recording runtime is used.