Context

The Context controller injects custom contextual information into the search requests and usage analytics search events sent from a search interface.

Methods

add

Add, or replace if already present, a new context key and value pair.

Parameters

  • contextKey: string

    The context key to add.

  • contextValue: string | string[]

    The context value to add.

remove

Remove a context key from the query.

Parameters

  • key: string

    The context key to remove.

set

Set the context for the query. Replace any existing context by the new one.

Parameters

  • ctx: Record<string, ContextValue>

    The context to set in the query.

subscribe

Adds a callback that will be called on state change.

Parameters

  • listener: () ⇒ void

    A callback to be invoked on state change.

Returns Unsubscribe: An unsubscribe function to remove the listener.

Attributes

state

The state of the Context controller.

Properties

  • values: Record<string, ContextValue>

    An object holding the context keys and their values.

Initialize

buildContext

Creates a Context controller instance.

Parameters

  • engine: Engine<ContextSection>

    The headless engine.

Returns Context

Unsubscribe

Call signatures

  • (): void

Example Implementation

context.ts

import {buildContext} from '@coveo/headless';
import {engine} from '../../engine';
 
export function setContext(ageGroup: string, interests: string[]) {
  buildContext(engine).set({
    ageGroup,
    interests,
  });
}
Recommended Articles