@embeddables/core
v0.7.3
Published
Core Embeddables identity and configuration SDK for client and server.
Keywords
Readme
@embeddables/core
Install
npm install @embeddables/coreUsage
Client Example:
import { initEmbeddables } from '@embeddables/core'
import { config } from './embeddables/_dist/config.ts'
const embeddables = initEmbeddables({ ...config })
embeddables.getAppUserId()
embeddables.getProjectId()
embeddables.getPublishableKey() // string | undefinedPersist and restore the last visited dynamic page key (browser-only):
await embeddables.registerPageView({ pageKey: 'checkout' })
const lastPage = embeddables.getLastPageView() // 'checkout' after reload + re-initOptionally emit a page:viewed analytics event when Analytics is wired post-init:
import { initAnalytics } from '@embeddables/analytics'
const embeddables = initEmbeddables({ ...config })
const analytics = initAnalytics({ core: embeddables, publishableKey: 'pk_live_…' })
embeddables.setAnalyticsInstance(analytics)
await embeddables.registerPageView({ pageKey: 'checkout' })In React, pass analytics() in EmbeddablesProvider's modules prop — the module wires
setAnalyticsInstance during init. See React below.
Server Example:
import { initEmbeddablesServer } from '@embeddables/core/server'
import { config } from './embeddables/_dist/config.ts'
const embeddables = initEmbeddablesServer({
...config,
cookies: {
get(key) {
return readCookie(key)
},
},
})publishableKey is optional. Pass it on config when using an SDK that requires
authentication, such as @embeddables/analytics. Core initializes successfully
without it.
App authentication
Each instance owns one app-level auth manager:
const core = initEmbeddables({
...config,
publishableKey: 'pk_live_…',
})
await core.auth.requestOtp({ phone: '+15551234567' })
await core.auth.verifyOtp({ phone: '+15551234567', token: '123456' })
await core.auth.refresh()
await core.auth.logout()React
Install React alongside Core:
npm install @embeddables/core reactPlace EmbeddablesProvider above every Embeddables product binding. Pass product SDK
modules through the modules prop and each product's configuration through the prop named
after it. The provider initializes every module before publishing Core as ready, so a
component that sees Core as ready always sees every product client already registered.
Example:
import {
EmbeddablesProvider,
useAppUserId,
useEmbeddables,
useEmbeddablesProjectId,
} from '@embeddables/core/react'
import { config } from './embeddables/_dist/config.ts'
import { modules as generatedModules } from './embeddables/_dist/modules.ts'
function App() {
return (
<EmbeddablesProvider
config={config}
publishableKey={import.meta.env.VITE_EMBEDDABLES_PUBLISHABLE_KEY}
modules={generatedModules}
>
<Identity />
</EmbeddablesProvider>
)
}
function Identity() {
const embeddables = useEmbeddables()
const appUserId = useAppUserId()
const projectId = useEmbeddablesProjectId()
if (embeddables?.isError) return <p>Embeddables could not initialize.</p>
if (embeddables == null) return <p>Loading…</p>
return (
<p>
{projectId}: {appUserId}
</p>
)
}Page views
useRegisterPageView() records a page view against the ambient instance. The last page key
is always written to storage; the page:viewed event only goes out when the analytics()
module registered a client on the same provider. It never throws — an invalid page key or an
unwritable store comes back as isError.
import { useEffect } from 'react'
import { useEmbeddables, useRegisterPageView } from '@embeddables/core/react'
function Step({ pageKey }: { pageKey: string }) {
const embeddables = useEmbeddables()
const { registerPageView, getLastPageView, isError } = useRegisterPageView()
// * React runs child effects before parent effects, so gate on Core being
// * ready — otherwise the very first page view no-ops and is dropped.
useEffect(() => {
if (embeddables == null || embeddables.isError) return
void registerPageView({ pageKey, isFunnelStep: true, funnelStepLabel: 'Checkout' })
}, [embeddables, registerPageView, pageKey])
return <p>previous: {getLastPageView() ?? 'none'}</p>
}Example (manual module list):
import { analytics } from '@embeddables/analytics/react'
<EmbeddablesProvider config={config} publishableKey="pk_live_…" modules={[analytics()]}>
{children}
</EmbeddablesProvider>modulesis optional, and its order does not matter — the provider works out which modules depend on which and initializes them accordingly.- Configure a product through the top-level prop named after it (
analytics,forms,protocols, …). Each module only ever sees its own value, and configuring a product that is not inmodulesis an error rather than a silent no-op. publishableKeyoverridesconfig.publishableKeyfor this provider instance.excludeAnalyticsInModulesturns off automatic Analytics tracking for the named modules.- CLI
buildgeneratesembeddables/_dist/modules.tsfromenabledModulesinconfig.yaml. useEmbeddables()returnsnullduring server rendering, the first client render, and until initialization completes. On failure it returns{ isError: true, error, appUserId: null }.useAppUserId()anduseEmbeddablesProjectId()returnnulluntil Core is ready insideEmbeddablesProvider.- Pass
serverAppUserIdto the provider when the server has already resolved the identity.
The provider treats its initial configuration, modules, product parameters, and identity as immutable. Remount it to initialize a different project or identity.
