Contentful Personalization & Analytics
    Preparing search index...

    Module @contentful/optimization-core - v0.0.0

    Optimization Core SDK — platform-agnostic optimization and analytics.

    Contentful Logo

    Contentful Personalization & Analytics

    Optimization Core SDK

    Guides · Reference · Contributing

    Warning

    The Optimization SDK Suite is pre-release (alpha). Breaking changes can be published at any time.

    The Optimization Core SDK owns the platform-agnostic optimization state machine, event builders, queues, resolvers, and interceptors used by the application-facing SDKs. Web, React Web, Node, React Native, and native bridge layers build on this package.

    We recommend starting application code with a platform SDK rather than Core directly. Use this README when building or maintaining SDK layers, and use Choosing the right SDK when deciding which application-facing package belongs in an integration. Generated reference documentation remains the source of truth for exported API signatures.

    Table of Contents

    Install using an NPM-compatible package manager, pnpm for example:

    pnpm install @contentful/optimization-core
    

    Choose the stateful or stateless Core class based on the runtime layer you are building:

    import { CoreStateful, CoreStateless } from '@contentful/optimization-core'

    const statefulOptimization = new CoreStateful({ clientId: 'your-client-id' })
    const statelessOptimization = new CoreStateless({ clientId: 'your-client-id' })

    Stateful runtimes own durable in-process SDK state. Stateless runtimes pass request-scoped Experience API options as the final event-method argument:

    await statelessOptimization.page(
    { profile: { id: 'profile-id' } },
    { locale: 'de-DE', preflight: false },
    )

    Use @contentful/optimization-core when you are building or maintaining an SDK layer that needs the shared optimization state machine, event builders, queueing, resolvers, or interceptors. Most application code uses a platform SDK such as Web, React Web, Node, or React Native instead of depending on Core directly.

    CoreStateful is the basis for SDKs that run in stateful JavaScript runtimes such as browsers, mobile JavaScript containers, and native bridge runtimes. It maintains consent, profile, selected optimization, flag, event-stream, blocked-event, and preview-panel state as read-only observables.

    Important

    CoreStateful uses module-global state by design. Initialize exactly one stateful instance per JavaScript runtime and reuse it.

    CoreStateless is the basis for SDKs that run in stateless environments such as Node servers and server-side functions. It does not store consent or profile state. Consumers pass profile and request-scoped Experience options with each event call.

    Shared Core configuration:

    Option Required? Default Description
    clientId Yes N/A Shared API key for Experience API and Insights API requests
    environment No 'main' Contentful environment identifier
    api No See API options below Experience API and Insights API endpoint options
    eventBuilder No SDK-layer defaults Event metadata overrides for platform SDK authors
    fetchOptions No SDK defaults Fetch timeout and retry behavior
    logLevel No 'error' Minimum log level for the default console sink

    Stateful-only configuration:

    Option Required? Default Description
    allowedEventTypes No ['identify', 'page', 'screen'] Event types allowed before consent is explicitly set
    defaults No undefined Initial state, commonly including consent or profile values
    getAnonymousId No undefined Function used to provide an anonymous ID from application-owned identity state
    onEventBlocked No undefined Callback invoked when consent or guard logic blocks an event
    queuePolicy No SDK defaults Flush retry behavior and offline queue bounds

    Common api options:

    Option Applies to Default Description
    experienceBaseUrl All 'https://experience.ninetailed.co/' Base URL for the Experience API
    insightsBaseUrl All 'https://ingest.insights.ninetailed.co/' Base URL for the Insights API
    enabledFeatures All ['ip-enrichment', 'location'] Experience API features for each request
    beaconHandler Stateful undefined Custom handler for enqueueing Insights batches
    ip Stateful undefined IP address override for Experience API analysis
    locale Stateful 'en-US' (in API) Locale used for Experience API location labels
    plainText Stateful false Sends performance-critical endpoints as text
    preflight Stateful false Aggregates a profile state without storing it

    In stateless environments, pass ip, locale, plainText, and preflight as the final argument to stateless event methods instead of constructor config.

    Common fetchOptions are fetchMethod, requestTimeout, retries, intervalTimeout, onFailedAttempt, and onRequestTimeout. Default retries intentionally apply only to HTTP 503 responses.

    For every option, callback payload, and exported type, use the generated Core SDK reference.

    Core exposes reusable primitives for SDK layers:

    Surface Purpose
    CoreStateful Stateful optimization runtime for browser, mobile, and bridge SDKs
    CoreStateless Stateless optimization runtime for server SDKs
    Event methods identify, page, screen, track, trackView, trackClick, etc.
    Resolution helpers resolveOptimizedEntry, getMergeTagValue, and getFlag
    states Stateful observable state streams
    Interceptors First-party hooks for event and state lifecycle customization
    Queue policy and fetch helpers Shared retry, flush, timeout, and offline buffering behavior
    Signal and observable utilities Lightweight reactive primitives used internally by stateful SDK layers

    The generated reference owns method arguments, return types, callback payload shapes, and inherited members. Keep this README focused on package role and maintainer orientation.

    Preview-panel helpers live under the internal preview-support entry. They are used by first-party preview surfaces to register preview state bridges, apply optimization overrides, and map Contentful entries for local authoring workflows.

    Application code must not use preview support directly unless it is building a first-party preview surface.

    Classes

    CoreStateful
    CoreStateless
    EventBuilder
    InterceptorManager

    Interfaces

    BlockedEvent
    ConsentController
    ConsentGuard
    CoreConfig
    CoreConfigDefaults
    CoreSharedApiConfig
    CoreStatefulApiConfig
    CoreStatefulConfig
    CoreStatelessApiConfig
    CoreStatelessConfig
    CoreStatelessRequestOptions
    CoreStates
    EventBuilderConfig
    ExperienceQueue
    ExperienceQueueDropContext
    GuardedByOptions
    InsightsQueue
    LifecycleInterceptors
    Observable
    PreviewPanelSignalObject
    QueueFlushFailureContext
    QueueFlushPolicy
    QueueFlushRecoveredContext
    QueuePolicy
    ResolvedData
    Signal
    SignalFns
    Signals
    Subscription

    Type Aliases

    BlockedEventReason
    BlockHandler
    ClickBuilderArgs
    CoreApiConfig
    EntryInteractionBuilderArgsBase
    EventType
    FlagViewBuilderArgs
    GuardedByFunction
    HoverBuilderArgs
    IdentifyBuilderArgs
    Interceptor
    MaybePromise
    PageViewBuilderArgs
    ScreenViewBuilderArgs
    StatelessExperiencePayload
    StatelessInsightsPayload
    StatelessNonStickyTrackViewPayload
    StatelessStickyTrackViewPayload
    TrackBuilderArgs
    UniversalEventBuilderArgs
    ViewBuilderArgs

    Variables

    ANONYMOUS_ID_KEY
    ANONYMOUS_ID_KEY_LEGACY
    CHANGES_CACHE_KEY
    ClickBuilderArgs
    DEBUG_FLAG_KEY
    DEFAULT_PAGE_PROPERTIES
    EntryInteractionBuilderArgsBase
    FlagsResolver
    FlagViewBuilderArgs
    HoverBuilderArgs
    IdentifyBuilderArgs
    MergeTagValueResolver
    OPTIMIZATION_CORE_SDK_NAME
    OPTIMIZATION_CORE_SDK_VERSION
    OptimizedEntryResolver
    PageViewBuilderArgs
    PREVIEW_PANEL_SIGNAL_FNS_SYMBOL
    PREVIEW_PANEL_SIGNALS_SYMBOL
    PROFILE_CACHE_KEY
    ScreenViewBuilderArgs
    SELECTED_OPTIMIZATIONS_CACHE_KEY
    signalFns
    signals
    TrackBuilderArgs
    UniversalEventBuilderArgs
    ViewBuilderArgs

    Functions

    batch
    effect
    guardedBy
    toDistinctObservable
    toObservable