Commands and methods
The commands you send to the widget, and the methods that read its state.
Commands
Send commands with teak(command, options). Commands are queued, so you can send them before teak.js has loaded, and they run in the order you send them. Commands don't return a value; use callbacks to react to the result.
| Command | Arguments | What it does |
|---|---|---|
configure | options object | Shows the widget, or updates it. |
preload | options object | Fetches the widget's configuration ahead of time, without showing it. |
clearSession | None | Ends the current session and forgets the customer's choice. |
Command names aren't case-sensitive. An unknown command is ignored.
configure
teak('configure', options);The first call prices protection for the items, shows the offer in the widget element, and calls loadedCb. Later calls update the widget: it re-prices and calls updatedCb. Calls made in quick succession are grouped into one update.
See Configuration options for every option.
preload
teak('preload', {
apiKey: 'pk_...',
locale: 'en_US',
preloadedCb: () => {},
onErrorCb: (error) => console.warn(error.message),
});Fetches and caches the widget's configuration, so the widget appears faster when you later call configure. Call it as soon as you know your API key, for example on a product or cart page, before the customer reaches the offer. It doesn't need a widget element or items, and it doesn't show the widget or start a session.
| Option | Type | Required | Default | Description |
|---|---|---|---|---|
apiKey | string | Yes | Your public key (pk_...). | |
locale | string | en_US | The widget's locale. Unlike configure, preload doesn't fall back to the browser's language. | |
sandbox | boolean | false | true for the Teak sandbox. | |
source | string | The configuration variant, if you set one in configure. | ||
preloadedCb | function | Called with no arguments when the configuration is cached. | ||
onErrorCb | function | Called with { message } if the fetch fails. |
The onErrorCb you pass to preload receives { message }. The one you pass to configure receives the session ID.
The cache lasts about 120 minutes. configure uses it only when apiKey, locale, sandbox, and source match the preload call. Pass locale explicitly in both calls: configure otherwise uses the browser's language, which may not match.
If preload fails, nothing else is affected: configure fetches the configuration itself.
clearSession
teak('clearSession');Ends the current session and clears any choice remembered with persist. It also resets the widget's options, so pass all your options again in the next configure, which starts a new session and a new quote. Call it after checkout completes, so the customer's next purchase doesn't reuse a submitted session.
Methods
Methods on the global teak object return the widget's current state. Call them after loadedCb has fired.
| Method | Returns | Description |
|---|---|---|
teak.get('token') | string | The session ID: the sessionId you passed to configure, or the one the widget created. Send it to your server to submit the order. |
teak.get('quote') | string | The protection price for the cart, for example "6.50". |
teak.isProtected() | boolean or undefined | true if the customer has opted in. |
teak.isFormComplete() | boolean or undefined | true once the customer has opted in or out. |
isProtected() and isFormComplete() can return undefined before the widget has loaded. With several widgets on the page, for example one per cart line, they return true only if it's true for every widget.
Next steps
To respond to what the widget does:
- Use callbacks and events to react when the widget loads, the customer chooses, or the price updates.
Updated 29 days ago
