Web SDK reference

The Web SDK (@lempert/taleart): the element, the page options, the functions, the texts, the hooks and the DOM events, and what happens when something goes wrong. For the steps, read the Web guide.

The element

Loading the SDK registers <taleart-view>. Each attribute has a property of the same meaning, named in camel case.

Attributes of <taleart-view>
AttributePropertyRequiredType and meaning
zone-keyzoneKeyYes The zone's key, tak_…, issued in the dashboard. It names the zone, so the element needs no slug. Without it the element renders nothing.
api-urlapiUrlNo The TaleArt API origin. Default: the page's apiUrl option, else the origin the script loaded from.
localelocaleNo This element's locale (BCP 47, such as he-IL). Default: the page's locale option, else the page's lang, else en.
themethemeNo Theme fields for this element only: JSON in the attribute (a JSON object), an object in the property. A field left out falls through to the next source (see the theme section of the SDK's README).
fallbackfallbackNo What the element shows when its zone fails to load: none (default, nothing) or message (the SDK's text and a retry button). A child with slot="fallback" is shown instead when there is one.
frequencyfrequencyNo How often a viewer sees a tale group: once, daily, weekly, or a whole number from 1 to 50 (hidden after that many views). Default: none. Counted in this browser only.
analyticsanalyticsNooff stops this element's impression, open, view and click events. Any other value, or none, leaves it to the SDK's analytics option.
dirdirNoltr or rtl sets this element's reading direction; auto (the default) takes the page's. The Web guide's Right-to-left section shows what turns.
Properties without an attribute
PropertyMeaning
zone The zone's slug, such as home-top, from the response. Set it only to name the zone for configureZone when the response does not carry the slug.
strings This element's texts, per locale or flat, added to the page's strings. This element's texts win.
hooksThis element's hooks. They are called before the page's hooks.

The element's method retry() reads the zone again after a failed load. The button of fallback="message" calls it.

The props of a tale's elements, such as a countdown's endsAt, a promo code's code or a reaction's emojis, are set in Tale Studio, not on this element. Their types and limits are on the Interactive components page.

Taleart.init options

init(config) sets the page's options, before or after the elements connect. Every option is optional: an option left out keeps its value, and an element attribute overrides the option. null clears locale, strings, theme, externalUserId, tags, properties, hooks and frequency; fallback: null means none, analytics: null means on, and dir: null means auto. An invalid option logs one console warning that names it and keeps its value.

Options of Taleart.init
OptionTypeDefaultMeaning
apiUrlstringThe origin the script loaded fromThe TaleArt API origin. Needed with the npm package, which has no script origin. An element's api-url wins.
localestring or nulldocument.documentElement.lang, then enThe locale of the SDK's texts (BCP 47). An element's locale wins.
stringsobject or nullNone: the SDK's English textsTexts per locale, such as { he: { … } }, or flat for every locale. See the strings table below.
themetheme object or nullNone: the project's themeTheme fields for every element. A zone's configureZone theme and an element's own theme win over it.
externalUserIdstring or nullNone The viewer's external user ID, for the user lists of the Audience page: an opaque ID, matched exactly, never an email address or a phone number.
tagsarray of strings or nullNone: every group showsThe viewer's targeting tags. Matched ignoring case and surrounding spaces. Same as setTags.
propertiesobject or nullNone: every group showsThe viewer's properties for audience rules: a flat object of name to text, number, boolean or list of text. Same as setProperties. Never sent to TaleArt.
hooksobject or nullNoneHook functions for every element. See the hooks table below.
fallback'none' or 'message', or nullnoneWhat every element shows when its zone fails to load: nothing, or the SDK's text and a retry button. An element's fallback wins.
frequencyNumber from 1 to 50, once, daily, weekly, or nullNone: every group showsHow often a viewer sees a tale group, counted in this browser. An element's frequency wins.
analyticstrue or false, or nullOnWhether elements report impressions, opens, tale views and clicks to the TaleArt API. An element's analytics attribute wins.
dirauto, ltr, rtl, or nullautoThe reading direction of every element: auto takes the page's. An element's dir attribute wins.

Exported functions

The module (import … from the script's URL, or the npm package) exports the functions below. The script also sets a global Taleart object with the same functions, except getConfig.

Exports of the Web SDK
ExportSignatureMeaning
initinit(config), returns nothingSets the page's options (above). Calling it again changes only the options it names.
getConfiggetConfig()Returns every option with the value the SDK uses now. Module export only, not on the global object.
configureZoneconfigureZone(zone, options?) Sets the theme of the zone with this slug: options.theme, the same fields as the theme option. configureZone(zone) with no theme removes it. Import it from exactly the script's URL.
setExternalUserIdsetExternalUserId(id)The same as init with externalUserId. Pass null after sign-out.
setTagssetTags(tags)The same as init with tags. Rendered elements show the matching groups at once.
setPropertiessetProperties(properties)The same as init with properties. Pass null to clear them. Rendered elements show the groups that pass at once.
setPropertysetProperty(name, value)Sets one property and keeps the others. A null value removes it. With no properties set yet, setProperty(name, null) changes nothing.
defineTaleartElementdefineTaleartElement(tag?)Registers the element under tag (default taleart-view). Loading the SDK already does this.
TaleartElementclassThe custom element's class, for instanceof checks and typings.
TALEART_ELEMENT_TAG'taleart-view'The element's tag name.

Locale and strings

A text is looked up under the full locale (he-IL), then its language (he), then the flat strings option, then English. A key that nobody defines shows English, never the key itself. A {name} in a text is filled in by the SDK. The keys below are the SDK's texts; the English column is the default. Three defaults start with a space, so they follow a number or a name in a sentence.

Text keys of the SDK and their English defaults
KeyEnglish default
viewer.closeClose
viewer.playPlay
viewer.pausePause
viewer.previousTalePrevious tale
viewer.nextTaleNext tale
viewer.previousGroupPrevious
viewer.nextGroupNext
viewer.previousGroupNamePrevious group: {name}
viewer.nextGroupNameNext group: {name}
viewer.progress{group}, tale {tale} of {count}
viewer.taleFallbackTale {number}
banner.labelBanner
banner.carouselcarousel
banner.slideslide
banner.slideOf{index} of {count}
banner.previousSlidePrevious slide
banner.nextSlideNext slide
banner.showGroupShow {name}
banner.stopRotationStop automatic slide show
banner.startRotationStart automatic slide show
banner.watchWatch
banner.dismissDismiss banner
bar.seen{name}, seen
bar.story{count} story
bar.stories{count} stories
bar.newStory{count} new story
bar.newStories{count} new stories
bar.noNewStoriesNo new stories
load.failedStories could not be loaded.
load.retryTry again
links.newTab(opens in a new tab)
poll.votedYou voted {label}.
poll.choseYou chose {label}.
poll.previewNotRecordedVotes are not recorded in the preview.
poll.failedYour vote could not be sent. Try again.
poll.resultsAfterVoteResults appear after you vote
poll.resultsResults
poll.vote{count} vote
poll.votes{count} votes
quiz.answeredYou answered {label}.
quiz.rightRight! You answered {label}.
quiz.wrongAnswerWrong: you answered {label}. The correct answer is {correct}.
quiz.correctcorrect answer
quiz.wrongwrong
quiz.previewNotRecordedAnswers are not recorded in the preview.
quiz.failedYour answer could not be sent. Try again.
rating.sliderLabelYour rating
rating.valueText{value} out of {max}
rating.ratedValueTextYou rated {value} out of {max}
rating.sendSend rating
rating.sentYou rated {value}.
rating.previewRatedYou rated {value}. Ratings are not recorded in the preview.
rating.failedYour rating could not be sent. Try again.
rating.summaryAverage {average} from {total} {count}
rating.singularrating
rating.pluralratings
countdown.endedEnded
countdown.daysDays
countdown.hoursHours
countdown.minutesMin
countdown.secondsSec
promo.copyCopy
promo.copiedCopied
promo.copyFailedCould not copy. Select the code and copy it yourself.
reaction.countReactions: {count}
reaction.failedYour reaction could not be sent. Try again.
preview.stopStop preview
preview.size.smallSmall
preview.size.defaultDefault
preview.size.largeLarge

Hooks

Hooks are set with init({ hooks }) for every element, or with an element's hooks property for one. The element's hook runs first, then the page's. Each hook gets one event object with name, zoneKeyPrefix (the first 8 characters of the key: the full key is never in an event) and zoneSlug, plus the fields in the table. A hook that throws is logged with console.error, and the viewer keeps going.

Hooks
HookWhen it firesPayload fieldsCancelable
onLoadedThe zone was read. Once per load.noneNo
onLoadFailedThe zone could not be read: a refused, unknown, rotated or revoked key, a network failure, a bad answer.error: the messageNo
onGroupImpressionA group's bar item or banner slide first is half on screen. Once per group per load.group: id, name, index (from 0)No
onPresentedThe full-screen viewer opened on a group.groupNo
onTaleShownA tale came on screen in the viewer.group; tale: id, index (from 0 in the group), titleNo
onActionClickedA button, an image button or the swipe-up link was activated, before it navigates.group, tale; component: id, type; url, newTab; preventDefault(), which stops the navigation Yes
onUserInteractedThe server accepted a poll, quiz, rating or reaction answer.group, tale; component: id, type, valueNo
onComponentEventA component of a tale did something: a countdown ended, a promo code was copied, a poll, quiz, rating or reaction answer was selected (it fires with onUserInteracted for an answer).groupId, taleId, component (the element type, such as countdown), action, value (the promo code, the option id, the answer id, the rating's number or the reaction's emoji, when there is one)No
onDismissedThe viewer closed.groupNo
onGroupCappedThe frequency cap left a tale group out of the display. Once per group per render.groupId, rule (the cap: once, daily, weekly or a number)No
onEventEvery event above, in one stream. Its argument is the event itself.The same as the eventOnly action clicked, as above

DOM events

Each event is also a DOM event on the <taleart-view> element: it bubbles and is composed, and its detail is the event's payload (the same fields as the hook, with name omitted). The name is taleart: plus the event's name in kebab case. Only taleart:action-clicked can be canceled, with preventDefault() on the event.

DOM events on the element
EventHookCancelable
taleart:loadedonLoadedNo
taleart:load-failedonLoadFailedNo
taleart:group-impressiononGroupImpressionNo
taleart:presentedonPresentedNo
taleart:tale-shownonTaleShownNo
taleart:action-clickedonActionClickedYes
taleart:user-interactedonUserInteractedNo
taleart:component-eventonComponentEventNo
taleart:dismissedonDismissedNo
taleart:group-cappedonGroupCappedNo

Errors and edge behaviour

What the SDK does when something goes wrong
SituationWhat happens
The zone key is missing, refused, rotated or revokedThe element renders nothing and logs one console error for it. Load failed fires.
A rate limit (HTTP 429) or a network failureThe element renders nothing and logs one console error. A reload later shows the zone again.
An invalid init optionOne console warning names it, and the option keeps its value.
An invalid fallback or frequency attributeOne console warning names it, and the page's option (or none) applies.
The zone cannot be loaded, and the element has a slot="fallback" child or fallback="message"The element shows the child, or the SDK's text and a retry button. retry() reads the zone again.
An invalid external user IDOne console warning, and no ID is sent.
An invalid theme field, or a theme attribute that is not a JSON objectThe field, or the attribute, is ignored with one console warning, and the next source's value applies.
configureZone without a zone slugOne console warning, and the call is ignored.
An element with the old site-key attributeOne console warning, and nothing is placed with it. Use the element with zone-key.
A hook throwsThe error is logged with console.error. The viewer keeps going.
A button's or the swipe-up link's destination is not an absolute http or https URL of at most 2,048 charactersThe SDK does not draw that link.
The page sets no tags, or no external user IDEvery group shows, except those with user lists, which need the ID.