Contentful Personalization & Analytics
    Preparing search index...

    Analytics implementation that maintains local state (consent, profile) and queues events until flushed or the queue reaches a maximum size.

    Repeated flush failures are managed by the configured QueueFlushPolicy using bounded backoff and a temporary circuit-open window.

    Hierarchy

    • AnalyticsBase
      • AnalyticsStateful

    Implements

    • ConsentGuard
    Index

    Constructors

    Properties

    allowedEventTypes?: string[]

    Allow‑list of event type keys permitted when consent is not present.

    Optimization API client used to send events to the Experience and Insights APIs.

    builder: EventBuilder

    Event builder used to construct strongly‑typed events.

    interceptors: LifecycleInterceptors

    Interceptors that can mutate/augment outgoing events or optimization state.

    onEventBlocked?: (event: BlockedEvent) => void

    Optional callback invoked when an event call is blocked.

    states: AnalyticsStates = ...

    Exposed observable state references.

    Methods

    • Send all queued events grouped by profile and clear the queue.

      Parameters

      • options: { force?: boolean } = {}

        Optional flush controls.

        • Optionalforce?: boolean

          When true, bypass offline/backoff/circuit gates and attempt immediately.

      Returns Promise<void>

      Only under rare circumstances should there be more than one profile in a stateful application.

      await analytics.flush()
      
    • Determine whether the named operation is permitted based on consent and allowed event type configuration.

      Parameters

      • name: string

        The method name; component view/flag methods are normalized to 'component', component click methods are normalized to 'component_click', and component hover methods are normalized to 'component_hover' for allow‑list checks.

      Returns boolean

      true if the operation is permitted; otherwise false.

      if (analytics.hasConsent('track')) { ... }
      
    • Hook invoked when an operation is blocked due to missing consent.

      Parameters

      • name: string

        The blocked operation name.

      • payload: readonly unknown[]

        The original arguments supplied to the operation.

      Returns void

      analytics.onBlockedByConsent('track', [payload])
      
    • Publish blocked event metadata to both callback and blocked event signal.

      Parameters

      • reason: "consent"

        Reason the method call was blocked.

      • product: BlockedEventProduct

        Product that blocked the method call.

      • method: string

        Name of the blocked method.

      • args: readonly unknown[]

        Original blocked call arguments.

      Returns void

    • Queue a component click event for the active profile.

      Parameters

      • payload: {
            campaign?: {
                content?: string;
                medium?: string;
                name?: string;
                source?: string;
                term?: string;
            };
            componentId: string;
            experienceId?: string;
            locale?: string;
            location?: {
                city?: string;
                continent?: string;
                coordinates?: { latitude: number; longitude: number };
                country?: string;
                countryCode?: string;
                postalCode?: string;
                region?: string;
                regionCode?: string;
                timezone?: string;
            };
            page?: {
                path: string;
                query: Record<string, string>;
                referrer: string;
                search: string;
                title?: string;
                url: string;
                [key: string]: JSONType;
            };
            screen?: { name: string; [key: string]: JSONType };
            userAgent?: string;
            variantIndex?: number;
        }

        Component click builder arguments.

      Returns Promise<void>

      A promise that resolves when the event has been queued.

      await analytics.trackComponentClick({ componentId: 'hero-banner' })
      
    • Queue a component hover event for the active profile.

      Parameters

      • payload: {
            campaign?: {
                content?: string;
                medium?: string;
                name?: string;
                source?: string;
                term?: string;
            };
            componentHoverId: string;
            componentId: string;
            experienceId?: string;
            hoverDurationMs: number;
            locale?: string;
            location?: {
                city?: string;
                continent?: string;
                coordinates?: { latitude: number; longitude: number };
                country?: string;
                countryCode?: string;
                postalCode?: string;
                region?: string;
                regionCode?: string;
                timezone?: string;
            };
            page?: {
                path: string;
                query: Record<string, string>;
                referrer: string;
                search: string;
                title?: string;
                url: string;
                [key: string]: JSONType;
            };
            screen?: { name: string; [key: string]: JSONType };
            userAgent?: string;
            variantIndex?: number;
        }

        Component hover builder arguments.

      Returns Promise<void>

      A promise that resolves when the event has been queued.

      await analytics.trackComponentHover({ componentId: 'hero-banner' })
      
    • Queue a component view event for the active profile.

      Parameters

      • payload: {
            campaign?: {
                content?: string;
                medium?: string;
                name?: string;
                source?: string;
                term?: string;
            };
            componentId: string;
            componentViewId: string;
            experienceId?: string;
            locale?: string;
            location?: {
                city?: string;
                continent?: string;
                coordinates?: { latitude: number; longitude: number };
                country?: string;
                countryCode?: string;
                postalCode?: string;
                region?: string;
                regionCode?: string;
                timezone?: string;
            };
            page?: {
                path: string;
                query: Record<string, string>;
                referrer: string;
                search: string;
                title?: string;
                url: string;
                [key: string]: JSONType;
            };
            screen?: { name: string; [key: string]: JSONType };
            sticky?: boolean;
            userAgent?: string;
            variantIndex?: number;
            viewDurationMs: number;
        }

        Component view builder arguments.

      Returns Promise<void>

      A promise that resolves when the event has been queued.

      await analytics.trackComponentView({ componentId: 'hero-banner' })
      
    • Queue a flag view event for the active profile.

      Parameters

      • payload: {
            campaign?: {
                content?: string;
                medium?: string;
                name?: string;
                source?: string;
                term?: string;
            };
            componentId: string;
            componentViewId: string;
            experienceId?: string;
            locale?: string;
            location?: {
                city?: string;
                continent?: string;
                coordinates?: { latitude: number; longitude: number };
                country?: string;
                countryCode?: string;
                postalCode?: string;
                region?: string;
                regionCode?: string;
                timezone?: string;
            };
            page?: {
                path: string;
                query: Record<string, string>;
                referrer: string;
                search: string;
                title?: string;
                url: string;
                [key: string]: JSONType;
            };
            screen?: { name: string; [key: string]: JSONType };
            sticky?: boolean;
            userAgent?: string;
            variantIndex?: number;
            viewDurationMs: number;
        }

        Flag view builder arguments.

      Returns Promise<void>

      A promise that resolves when the event has been queued.

      await analytics.trackFlagView({ componentId: 'feature-flag-123' })