Contentful Personalization & Analytics
    Preparing search index...

    Module @contentful/optimization-core - v1.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 bind request-scoped consent, profile state, event context, and Experience API options before calling event methods:

    const requestOptimization = statelessOptimization.forRequest({
    consent: true,
    locale: 'en-US',
    eventContext: { locale: 'en-US' },
    experienceOptions: { preflight: false },
    profile: { id: 'profile-id' },
    })

    const { accepted, data } = await requestOptimization.page()

    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 between requests. Consumers call forRequest() to create a request-bound event client with the consent, profile, event context, and Experience options for that one incoming request.

    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
    contentful No undefined App-owned contentful.js client, default query, and cache
    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

    Consent and event configuration:

    Option Required? Default Description
    allowedEventTypes No [] Event types allowed before consent is explicitly set
    onEventBlocked No undefined Callback invoked when consent or guard logic blocks an event

    Core itself fails closed before consent. Platform SDKs set runtime-specific defaults for the event types that make sense in that runtime, such as browser identify/page, server identify/page, and mobile identify/screen.

    Stateful-only configuration:

    Option Required? Default Description
    defaults No undefined Initial state, commonly including consent, persistence consent, or profile values
    getAnonymousId No undefined Function used to provide an anonymous ID from application-owned identity state
    queuePolicy No SDK defaults Flush retry behavior and offline queue bounds

    Persistence consent controls durable profile-continuity storage for SDKs with runtime storage: profile, anonymous ID, changes, and selected optimization caches. Event consent, stored consent decisions, and debug state are tracked separately.

    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
    ip Stateful undefined IP address override for Experience API analysis
    plainText Stateful true for single-profile mutations Sends single-profile Experience mutations as text
    preflight Stateful false Aggregates a profile state without storing it

    When plainText is omitted, single-profile Experience mutation/event requests use text/plain. Pass plainText: false to send JSON. Experience batch profile updates still default to JSON.

    In stateless environments, pass ip, locale, plainText, and preflight as experienceOptions when creating the request-bound client instead of constructor config. Pass request-specific Insights API options, such as a last-chance beacon sender, as insightsOptions.

    Core-backed stateful SDKs can accept an initial top-level locale and runtime setLocale(locale) calls. They expose that SDK Experience API and default event locale through the live locale getter and states.locale observable. Stateless server SDKs should pass the request-scoped SDK locale as forRequest({ locale }); advanced callers can still pass experienceOptions.locale when no request locale is supplied.

    Applications own Contentful CDA locale selection and pass that locale directly to their Contentful client. For the full locale model, see Locale handling in the Optimization SDK Suite.

    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 and fetch helpers resolveOptimizedEntry, fetchContentfulEntry, fetchContentfulEntries, fetchOptimizedEntry, prefetchManagedEntries, getMergeTagValue, and getFlag
    Current-state tracking AcceptedCurrentStateTracker for SDK-owned page or screen adapters
    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

    When a contentful.js client is available, prefer SDK-managed fetching. Configure contentful: { client, defaultQuery?, cache? }, then call fetchContentfulEntry(entryId, query?), fetchContentfulEntries(entries), prefetchManagedEntries(entries), or fetchOptimizedEntry(entryId, options?). Managed calls merge defaultQuery, per-entry query overrides, SDK locale fallback, and include: 10, then cache entries per SDK instance by default. For multiple uncached entries with the same normalized query, Core calls client.getEntries({ 'sys.id[in]': ids, limit: ids.length }), splitting large batches into 100-ID chunks. Same-tick uncached single-entry calls can join the same batch. Results preserve descriptor order and duplicates. Set contentful.cache: false to disable the cache or call clearContentfulEntryCache() to clear it. resolveOptimizedEntry() remains the manual path for entries the app already fetched. Stateful Core uses the current selectedOptimizations when omitted, request-bound stateless clients use the latest accepted Experience selections and request locale fallback for managed Contentful fetches, and root stateless callers pass explicit selectedOptimizations.

    Do not pass all-locale CDA responses from withAllLocales or locale=*; optimization fields such as fields.nt_experiences and fields.nt_variants must be direct single-locale field values. See Entry personalization and variant resolution for the entry contract and Locale handling in the Optimization SDK Suite for the broader locale model.

    Custom entry-source adapters

    @contentful/optimization-core/entry-source is an advanced subpath for custom JavaScript runtime or framework adapters that cannot use an official Web, React Web, React Native, Node, or native SDK surface. It is not part of the root Core API posture and is not the preferred path for application integrations.

    Import OptimizedEntrySourceController when an adapter accepts either a direct baselineEntry or an entryId that must be fetched through an SDK-managed contentful.js client. The controller owns the baselineEntry versus entryId source lifecycle, entryId + entryQuery fetch keying, SDK readiness/loading/error snapshots, stale request protection, and disconnect() cleanup. createOptimizedEntryLoadingEntry(entryId) creates a stable placeholder Contentful entry for adapter loading states.

    The entry-source controller does not own rendering, variant resolution, tracking, consent, Experience API calls, or Contentful client creation. After a snapshot contains baselineEntry, the adapter still calls resolveOptimizedEntry(), renders the result, and wires any runtime-specific tracking.

    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.

    Client

    ContentfulClientApi
    ChainModifiers

    ContentType

    Entity

    LocaleCode

    Entry

    EntryFieldTypes
    EntrySys
    Entry
    EntryCollection
    EntrySkeletonType

    Link

    LinkType

    Other

    AcceptedCurrentStateTracker
    CoreStateful
    CoreStateless
    CoreStatelessRequest
    EventBuilder
    InterceptorManager
    AcceptedCurrentStateEmissionOptions
    AcceptedCurrentStateTrackerOptions
    BlockedEvent
    ConsentController
    ConsentGuard
    ContentfulConfig
    ContentfulEntryCacheOptions
    ContentfulEntryClient
    CoreConfig
    CoreConfigDefaults
    CoreSharedApiConfig
    CoreStatefulApiConfig
    CoreStatefulConfig
    CoreStatelessApiConfig
    CoreStatelessConfig
    CoreStatelessForRequestOptions
    CoreStatelessInsightsOptions
    CoreStatelessRequestOptions
    CoreStates
    CreatePageContextFromUrlOptions
    EntryInteractionOptimizationContextArgs
    EntryViewFireSchedule
    EntryViewTiming
    EventBuilderConfig
    EventOptimizationContext
    ExperienceQueue
    ExperienceQueueDropContext
    FetchOptimizedEntryOptions
    FetchOptimizedEntryResult
    GuardedByOptions
    InsightsQueue
    InsightsQueueFlushOptions
    LifecycleInterceptors
    ManagedEntryHandoff
    NormalizedOptimizationConfig
    Observable
    OptimizationEntryFields
    OptimizedEntryFields
    OptimizedEntryMetadata
    PersistedStatefulDefaults
    QueueFlushFailureContext
    QueueFlushPolicy
    QueueFlushRecoveredContext
    QueuePolicy
    ResolvedData
    ResolvedDataWithOptimizationContext
    ResolvedStatefulDefaults
    Signal
    SignalFns
    Signals
    StatefulDefaults
    Subscription
    AcceptedCurrentStateEmissionResult
    AliasEvent
    AllowedEventType
    App
    AudienceEntry
    AudienceEntryFields
    AudienceEntrySkeleton
    BatchExperienceEvent
    BatchExperienceEventArray
    BatchExperienceRequestData
    BatchExperienceResponse
    BatchExperienceResponseData
    BatchInsightsEvent
    BatchInsightsEventArray
    BlockedEventReason
    BlockHandler
    Campaign
    Change
    ChangeArray
    Channel
    ClickBuilderArgs
    ClickEvent
    ConsentInput
    ConsentStorageValue
    ContentfulEntry
    ContentfulEntryQuery
    CoreApiConfig
    CoreStatelessRequestConsent
    Dictionary
    EntryInteractionBuilderArgsBase
    EntryReplacementComponent
    EntryReplacementVariant
    EntryViewTimingOptions
    EventEmissionResult
    EventType
    ExperienceData
    ExperienceEvent
    ExperienceEventArray
    ExperienceEventType
    ExperienceRequestData
    ExperienceRequestFailureReason
    ExperienceRequestOptions
    ExperienceRequestState
    ExperienceResponse
    Flags
    FlagViewBuilderArgs
    GeoLocation
    GroupEvent
    GuardedByFunction
    HoverBuilderArgs
    HoverEvent
    IdentifyBuilderArgs
    IdentifyEvent
    InlineVariableComponent
    InsightsEvent
    InsightsEventArray
    InsightsEventType
    InteractionEventProperties
    Interceptor
    Json
    Library
    ManagedContentfulEntry
    ManagedEntryDescriptor
    MaybePromise
    MergeTagEntry
    MergeTagEntryFields
    MergeTagEntrySkeleton
    OptimizationComponent
    OptimizationComponentArray
    OptimizationConfig
    OptimizationContextId
    OptimizationData
    OptimizationEntry
    OptimizationEntryArray
    OptimizationEntrySkeleton
    OptimizationEventStreamEvent
    OptimizationType
    OptimizedEntry
    OptimizedEntrySkeleton
    Page
    PageEventContext
    PageViewBuilderArgs
    PageViewEvent
    PartialProfile
    PendingEventOptimizationContext
    PersistedDefaultLoader
    PersistedDefaultValue
    Profile
    Properties
    ResponseEnvelope
    Screen
    ScreenEventContext
    ScreenViewBuilderArgs
    ScreenViewEvent
    SelectedOptimization
    SelectedOptimizationArray
    SessionStatistics
    SnapshotFn
    StatelessExperiencePayload
    StatelessInsightsPayload
    StatelessNonStickyTrackViewPayload
    StatelessStickyTrackViewPayload
    TrackBuilderArgs
    TrackEvent
    Traits
    UniversalEventBuilderArgs
    UniversalEventContext
    UniversalEventProperties
    UnknownChange
    VariableChange
    ViewBuilderArgs
    ViewEvent
    AliasEvent
    ANONYMOUS_ID_KEY
    ANONYMOUS_ID_KEY_LEGACY
    App
    AudienceEntryFields
    BatchExperienceEvent
    BatchExperienceEventArray
    BatchExperienceRequestData
    BatchExperienceResponse
    BatchExperienceResponseData
    BatchInsightsEvent
    BatchInsightsEventArray
    Campaign
    Change
    ChangeArray
    ChangeBase
    CHANGES_CACHE_KEY
    ChangeType
    Channel
    ClickBuilderArgs
    ClickEvent
    DEBUG_FLAG_KEY
    DEFAULT_ALLOWED_EVENT_TYPES
    DEFAULT_PAGE_PROPERTIES
    Dictionary
    EntryInteractionBuilderArgsBase
    EntryReplacementComponent
    EntryReplacementVariant
    ExperienceData
    ExperienceEvent
    ExperienceEventArray
    ExperienceRequestData
    ExperienceRequestOptions
    ExperienceResponse
    FlagsResolver
    FlagViewBuilderArgs
    GeoLocation
    GroupEvent
    HoverBuilderArgs
    HoverEvent
    IdentifyBuilderArgs
    IdentifyEvent
    InlineVariableComponent
    InlineVariableComponentValueType
    InlineVariableVariant
    InsightsEvent
    InsightsEventArray
    InteractionEventProperties
    Library
    MergeTagEntryFields
    MergeTagValueResolver
    OPTIMIZATION_CORE_SDK_NAME
    OPTIMIZATION_CORE_SDK_VERSION
    OptimizationComponent
    OptimizationComponentArray
    OptimizationConfig
    OptimizationEntryFields
    OptimizationType
    OptimizedEntryFields
    OptimizedEntryResolver
    Page
    PageEventContext
    PageViewBuilderArgs
    PageViewEvent
    PartialProfile
    Profile
    PROFILE_CACHE_KEY
    Properties
    ResponseEnvelope
    Screen
    ScreenEventContext
    ScreenViewBuilderArgs
    ScreenViewEvent
    SELECTED_OPTIMIZATIONS_CACHE_KEY
    SelectedOptimization
    SelectedOptimizationArray
    SessionStatistics
    signalFns
    signals
    TrackBuilderArgs
    TrackEvent
    Traits
    UniversalEventBuilderArgs
    UniversalEventContext
    UniversalEventProperties
    UnknownChange
    VariableChange
    VariableChangeValue
    ViewBuilderArgs
    ViewEvent
    batch
    createPageContextFromUrl
    decodeConsentStorageValue
    effect
    encodeConsentStorageValue
    getRemainingMsUntilNextEntryViewFire
    guardedBy
    isEntryReplacementComponent
    isEntryReplacementVariant
    isInlineVariableComponent
    isMergeTagEntry
    isRecord
    isResolvedAudienceEntry
    isResolvedContentfulEntry
    isResolvedOptimizationEntry
    isResolvedOptimizedEntry
    isRichTextDocument
    isRichTextNode
    normalizeExplicitLocale
    normalizeLocale
    normalizeOptimizationConfig
    parseWithFriendlyError
    resolveEntryViewTimingOptions
    resolvePersistedPersistenceConsent
    resolveStatefulDefaults
    shouldRememberStickyEntryViewResult
    shouldSendStickyEntryView
    toDistinctObservable
    toObservable

    Tag