@kywi-software/js
v0.11.0
Published
Kywi CMS browser runtime — audience personalization, experiments, forms, and feed rendering for the public site (loaded as kywi.js).
Maintainers
Readme
@kywi-software/js
The Kywi CMS browser runtime — audience personalization (variant swaps,
transparency badge), A/B experiment exposure/conversion beacons, the
self-ID widget, and content-form hooks. It reads a small JSON context that
@kywi-software/core's server-rendered pages embed in the page, then patches
the DOM client-side. See the
root README for how it fits
into the rest of Kywi.
This package ships two builds from the same source
(packages/kywi-js/src/index.ts):
dist/kywi.js— an IIFE bundle that exposes aKywiglobal. This is the one Kywi CMS serves at the public URL/kywi.js(hence the package name).dist/kywi.esm.js— an ES module, for bundler-based setups.
Install
npm install @kywi-software/jsMost projects don't install this directly — a Kywi CMS site (any deployment
mode) serves the built bundle at /kywi.js automatically, and pages that
need it load it with a plain <script> tag (see below). Install it directly
if you're building a decoupled frontend and want to import it as an ES
module instead.
Usage: script tag (how Kywi CMS itself loads it)
<script src="/kywi.js" data-kywi-js defer></script>Kywi boots itself on DOMContentLoaded (or immediately if the document
has already loaded) — reads its context from <meta name="kywi:*"> tags and
data-kywi-* attributes that the server-rendered page embeds
(site-id, theme-path, base-path, csrf-token, plus <html lang> and
.kywi-edit-mode), patches personalization variants and the transparency
badge into the DOM, and wires experiment exposure/conversion beacons and
form hooks. No manual Kywi.boot() call needed for the default flow; use
Kywi.preInit(fn) if you need to run code before boot.
Usage: ES module
import { Kywi } from '@kywi-software/js'
Kywi.preInit((kywi) => {
// runs before boot — e.g. register a custom module via kywi.UI.extend(...)
})What it does
- Personalization: swaps
display:nonevariant containers (data-kywi-variant) based on the visitor's resolved audience, and fills the transparency badge mount (data-kywi-personalization-badge) with an opt-in/opt-out control. - Experiments: confirms exposure of the server-rendered A/B variant and
wires conversion signals (pageview, auto-emitted on boot; form-submit,
auto-wired). The exported
bootExperiments()(also usable standalone) returns anExperimentRuntimewith.track(eventName)/.trackConversion(signal)for custom conversions. - Self-ID widget: renders the audience self-identification UI
(hello bar / modal / inline / drawer) authored in
/admin/audiences/self-id-widget. - Feeds:
Kywi.getFeed(type)/Kywi.renderFeed(options)for client-rendered content feeds. - Entities:
Kywi.getEntity(type)— a thin fetch wrapper over the/api/v1REST API, scoped with the page's CSRF token.
packages/kywi-js/src/index.ts and its tests
(packages/kywi-js/src/__tests__/*.test.ts) are the source of truth for
exact behavior.
