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.

CommandArgumentsWhat it does
configureoptions objectShows the widget, or updates it.
preloadoptions objectFetches the widget's configuration ahead of time, without showing it.
clearSessionNoneEnds 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.

OptionTypeRequiredDefaultDescription
apiKeystringYesYour public key (pk_...).
localestringen_USThe widget's locale. Unlike configure, preload doesn't fall back to the browser's language.
sandboxbooleanfalsetrue for the Teak sandbox.
sourcestringThe configuration variant, if you set one in configure.
preloadedCbfunctionCalled with no arguments when the configuration is cached.
onErrorCbfunctionCalled with { message } if the fetch fails.
This onErrorCb isn't the same as configure's

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.

MethodReturnsDescription
teak.get('token')stringThe 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')stringThe protection price for the cart, for example "6.50".
teak.isProtected()boolean or undefinedtrue if the customer has opted in.
teak.isFormComplete()boolean or undefinedtrue 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.

Did this page help you?