Client-side context in the Svelte SDK

Developer preview

This feature is in developer preview. Use with caution as it may change unexpectedly. For more information, contact us.

Access the Uniform Context instance directly. Must be called within a UniformContext provider:

<script lang="ts"> import { getUniformContext } from '@uniformdev/context-svelte'; const { context } = getUniformContext(); const forgetMe = async () => { await context.forget(true); }; </script> <button onclick={forgetMe}>Forget me</button>

The context object is the @uniformdev/context Context instance:

Method / PropertyDescription
context.scoresCurrent visitor score values (not reactive, so use getScores())
context.quirksCurrent visitor quirk values (not reactive, so use getQuirks())
context.update(options)Update visitor data (quirks, enrichments, URL)
context.forget(purge)Clear all visitor data. Pass true to also clear cookies
context.personalize(options)Manually evaluate personalization

warning

The context.scores and context.quirks properties accessed directly are not reactive: they do not trigger re-renders. Use getScores() and getQuirks() for reactive access.

Provides reactive access to the current visitor's score values. Uses Svelte 5's $state rune internally:

<script lang="ts"> import { getScores } from '@uniformdev/context-svelte'; const scores = getScores(); </script> <div>Tech interest score: {scores.current?.tech ?? 0}</div>

Returns an object with a reactive .current property containing the ScoreVector.

Provides reactive access to the current visitor's quirk values:

<script lang="ts"> import { getQuirks } from '@uniformdev/context-svelte'; const quirks = getQuirks(); </script> <div>Current country: {quirks.current?.country ?? 'Unknown'}</div>

Returns an object with a reactive .current property containing the Quirks object.

Update visitor quirks to trigger personalization changes:

<script lang="ts"> import { getUniformContext } from '@uniformdev/context-svelte'; const { context } = getUniformContext(); const setCountry = async (country: string) => { await context.update({ quirks: { country }, }); }; </script> <div> <button onclick={() => setCountry('Canada')}>I'm from Canada</button> <button onclick={() => setCountry('USA')}>I'm from USA</button> </div>