> ## Documentation Index
> Fetch the complete documentation index at: https://algolia.com/llms.txt
> Use this file to discover all available pages before exploring further.

# insights

> Middleware for setting the user token and sending click and conversion events to the Insights API.

export const FlavorSwitcher = ({current, baseHref = "", options = [], label = "InstantSearch framework"}) => {
  if (options.length === 0) {
    return <div className="not-prose" role="alert" style={{
      margin: "0.25rem 0 1.5rem",
      padding: "0.75rem",
      border: "1px solid #f59e0b",
      borderRadius: "0.625rem",
      color: "inherit",
      fontSize: "0.875rem"
    }}>
        FlavorSwitcher requires at least one option.
      </div>;
  }
  const selected = options.find(option => option.value === current) ?? options[0];
  return <div className="not-prose mint-flavor-switcher">
      <style>{`
        .mint-flavor-switcher {
          --mfs-bg: #ffffff;
          --mfs-bg-hover: #f4f4f5;
          --mfs-bg-current: #eef2ff;
          --mfs-border: #d4d4d8;
          --mfs-fg: #18181b;
          --mfs-muted: #71717a;
          --mfs-accent: #4f46e5;
          position: relative;
          width: min(100%, 19rem);
          margin: 0.25rem 0 1.5rem;
          color: var(--mfs-fg);
          font-size: 0.875rem;
          line-height: 1.25rem;
        }

        .dark .mint-flavor-switcher {
          --mfs-bg: #18181b;
          --mfs-bg-hover: #27272a;
          --mfs-bg-current: #272747;
          --mfs-border: #3f3f46;
          --mfs-fg: #fafafa;
          --mfs-muted: #a1a1aa;
          --mfs-accent: #a5b4fc;
        }

        .mint-flavor-switcher details {
          position: relative;
        }

        .mint-flavor-switcher summary {
          display: flex;
          min-height: 2.75rem;
          box-sizing: border-box;
          align-items: center;
          justify-content: space-between;
          gap: 0.75rem;
          padding: 0.625rem 0.75rem;
          border: 1px solid var(--mfs-border);
          border-radius: 0.625rem;
          background: var(--mfs-bg);
          color: var(--mfs-fg);
          cursor: pointer;
          font-weight: 600;
          list-style: none;
          transition: border-color 150ms ease, box-shadow 150ms ease;
        }

        .mint-flavor-switcher summary::-webkit-details-marker {
          display: none;
        }

        .mint-flavor-switcher summary:hover {
          border-color: var(--mfs-accent);
        }

        .mint-flavor-switcher summary:focus-visible {
          outline: 2px solid var(--mfs-accent);
          outline-offset: 2px;
        }

        .mint-flavor-switcher__label {
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }

        .mint-flavor-switcher__chevron {
          flex: none;
          transition: transform 150ms ease;
        }

        .mint-flavor-switcher details[open] .mint-flavor-switcher__chevron {
          transform: rotate(180deg);
        }

        .mint-flavor-switcher__menu {
          position: absolute;
          z-index: 50;
          top: calc(100% + 0.375rem);
          left: 0;
          width: 100%;
          box-sizing: border-box;
          margin: 0;
          padding: 0.375rem;
          border: 1px solid var(--mfs-border);
          border-radius: 0.625rem;
          background: var(--mfs-bg);
          box-shadow: 0 12px 30px rgb(0 0 0 / 16%);
          list-style: none;
        }

        .mint-flavor-switcher__menu li {
          margin: 0;
          padding: 0;
        }

        .mint-flavor-switcher__option {
          display: grid;
          gap: 0.125rem;
          padding: 0.625rem 0.75rem;
          border-radius: 0.4rem;
          color: var(--mfs-fg);
          text-decoration: none;
        }

        .mint-flavor-switcher__option:hover {
          background: var(--mfs-bg-hover);
        }

        .mint-flavor-switcher__option:focus-visible {
          outline: 2px solid var(--mfs-accent);
          outline-offset: -2px;
        }

        .mint-flavor-switcher__option[aria-current="page"] {
          background: var(--mfs-bg-current);
          color: var(--mfs-accent);
        }

        .mint-flavor-switcher__name {
          font-weight: 600;
        }

        .mint-flavor-switcher__description {
          color: var(--mfs-muted);
          font-size: 0.8125rem;
        }

        @media (prefers-reduced-motion: reduce) {
          .mint-flavor-switcher summary,
          .mint-flavor-switcher__chevron {
            transition: none;
          }
        }
      `}</style>

      <details>
        <summary aria-label={`${label}: ${selected.label}`}>
          <span className="mint-flavor-switcher__label">{selected.label}</span>
          <svg className="mint-flavor-switcher__chevron" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
            <path d="m6 9 6 6 6-6" />
          </svg>
        </summary>

        <ul className="mint-flavor-switcher__menu" aria-label={label}>
          {options.map(option => {
    const isCurrent = option.value === selected.value;
    const href = option.href ?? `${baseHref.replace(/\/$/, "")}/${encodeURIComponent(option.value)}`;
    return <li key={option.value}>
                <a className="mint-flavor-switcher__option" href={href} aria-current={isCurrent ? "page" : undefined}>
                  <span className="mint-flavor-switcher__name">
                    {option.label}
                  </span>
                  {option.description ? <span className="mint-flavor-switcher__description">
                      {option.description}
                    </span> : null}
                </a>
              </li>;
  })}
        </ul>
      </details>
    </div>;
};

<div className="mint-flavor-switcher-slot not-prose">
  <FlavorSwitcher
    current="js"
    baseHref="/doc/api-reference/widgets/insights"
    options={[
{ value: "js", label: "JavaScript", description: "InstantSearch.js" },
{ value: "react", label: "React", description: "React InstantSearch" },
{ value: "vue", label: "Vue", description: "Vue InstantSearch" },
]}
  />
</div>

```ts Signature theme={"system"}
createInsightsMiddleware({
  insightsClient: null | InsightsClient,
  insightsInitParams?: object,
  onEvent?: (event: InsightsEvent, aa: null | InsightsClient) => void,
});
```

## Import

<CodeGroup>
  ```js Package manager theme={"system"}
  import { createInsightsMiddleware } from "instantsearch.js/es/middlewares";
  ```

  ```js CDN theme={"system"}
  const { createInsightsMiddleware } = instantsearch.middlewares;
  // or directly use instantsearch.middlewares.createInsightsMiddleware()
  ```
</CodeGroup>

## About this widget

<Note>
  Starting from v4.55.0, you can use the [`insights`](/doc/api-reference/widgets/instantsearch/js#param-insights)
  option instead of setting up the Insights middleware yourself.
</Note>

The `createInsightsMiddleware` creates an insights middleware to help you achieve the following:

1. Set the `userToken` for insights purposes (Click Analytics, Personalization, etc.)
2. Automatically send events from built-in widgets (you can turn this off)
3. Lets you send events from your own custom widgets

### Requirements

* [`search-insights`](https://github.com/algolia/search-insights.js) v1.6.2 or later.
* [InstantSearch.js](https://github.com/algolia/instantsearch) v4.8.3 or later.

See also: [Send click and conversion events with InstantSearch.js](/doc/guides/building-search-ui/events/js)

## Examples

```js JavaScript icon=code theme={"system"}
const insightsMiddleware = createInsightsMiddleware({
  insightsClient,
  insightsInitParams,
  onEvent,
});

search.use(insightsMiddleware);
```

## Options

<ParamField body="insightsClient" type="InsightsClient | null">
  The [Insights client](/doc/libraries/search-insights?client=javascript) is used to send events.
  It synchronizes the [user token](/doc/guides/sending-events/concepts/usertoken#exclude-users-who-want-to-opt-out-of-analytics-recommend-and-personalization)
  between search and analytics calls.

  To disable `userToken` synchronization and automatic event sending, set this to `null`.

  <CodeGroup>
    ```html UMD theme={"system"}
    <script>
      /*
        A snippet to include `search-insights`
        You can find it here
        https://github.com/algolia/search-insights.js
      */
    </script>

    <script>
      const insightsMiddleware = instantsearch.middlewares.createInsightsMiddleware({
        insightsClient: window.aa,
      });

      search.use(insightsMiddleware);

      const userToken = // Get the user token (synchronously or asynchronously).

      // The `insights` middleware receives a notification
      // and attaches the `userToken` to search calls onwards.
      window.aa('setUserToken', userToken);
    </script>
    ```

    ```js ESM theme={"system"}
    import aa from "search-insights";
    import { createInsightsMiddleware } from "instantsearch.js/es/middlewares";

    const insightsMiddleware = createInsightsMiddleware({
      insightsClient: aa,
    });

    search.use(insightsMiddleware);

    // Get the user token (synchronously or asynchronously).
    const userToken = /*...*/;

    // The `insights` middleware receives a notification
    // and attaches the `userToken` to search calls onwards.
    aa("setUserToken", userToken);
    ```
  </CodeGroup>
</ParamField>

<ParamField body="insightsInitParams" type="object" post={['since: v4.11.0']}>
  Insights parameters to forward to the Insights client's [`init`](/doc/libraries/search-insights/init) method.

  With `search-insights >= v1.7.0 and < 2.0.0`,
  the Insights client accepts `useCookie` and `userToken` parameters in the [`init`](/doc/libraries/search-insights/init) method.
  You can pass `useCookie: false` to prevent the usage of cookies to store an anonymous `userToken`.
  You can also pass a custom `userToken` while creating `insights` middleware, if you have one.

  With `search-insights >= 2.0.0`, the default value of `useCookie` is `false`.

  <Note>
    You can set an authenticated user token and a user token in `insightsInitParams`.
    Both tokens are passed to the Insights API for sending events.
    If both are set, InstantSearch uses the authenticated user token for search queries.
  </Note>

  <CodeGroup>
    ```js Cookie theme={"system"}
    createInsightsMiddleware({
      insightsInitParams: {
        useCookie: true,
      },
    });
    ```

    ```js Tokens theme={"system"}
    createInsightsMiddleware({
      insightsInitParams: {
        userToken: "user-token",
        authenticatedUserToken: "auth-user-token",
      },
    });
    ```
  </CodeGroup>
</ParamField>

<ParamField body="onEvent" type="(event: InsightsEvent, aa: null | InsightsClient) => void">
  By default, the middleware sends events to Algolia using the provided `insightsClient`.
  You can also control events and send them yourself by implementing an `onEvent` method for the middleware to call instead.
  The method lets you access data, and filter or modify the payload. You can also use it to send events to third-party trackers.

  If you want to use `onEvent` to send events to third-party trackers,
  but don't want to send them to Algolia,
  you can set `insightsClient` to `null`,
  and you don't need the `search-insights` library in your application.

  <Expandable title="event properties">
    <ParamField body="event.insightsMethod" type="string">
      The Insights method, such as, `'viewedObjectIDs'`, `'clickedObjectIDsAfterSearch'`.
      For more information, see the [Insights API reference](/doc/libraries/search-insights).
    </ParamField>

    <ParamField body="event.payload" type="[key: string]: any">
      Event payload.
    </ParamField>

    <ParamField body="event.widgetType" type="string">
      Widget type given by connectors, such as `'ais.refinementList` or `'ais.hits`.
    </ParamField>

    <ParamField body="event.eventType" type="string">
      Event type, such as `'view'`, `'click'`, `'conversion'`, or anything else if you customized it.
    </ParamField>
  </Expandable>

  ```js JavaScript icon=code theme={"system"}
  createInsightsMiddleware({
    insightsClient: window.aa,
    onEvent: (event, aa) => {
      const { insightsMethod, payload, widgetType, eventType } = event;

      // Send the event to Algolia
      if (insightsMethod) {
        aa(insightsMethod, payload);
      }

      // Send the event to a third-party tracker
      if (widgetType === "ais.hits" && eventType === "click") {
        thirdPartyTracker.send("Product Clicked", payload);
      }
    },
  });
  ```
</ParamField>

## Custom events

<ParamField body="Connectors">
  Many of the InstantSearch connectors expose the `sendEvent` method.
  If you use these connectors to create custom widgets, you can leverage the method to send custom events.

  Here's a list of connectors that expose `sendEvent`.

  * `connectAutocomplete`: [`autocomplete`](/doc/api-reference/widgets/autocomplete/js#param-indices)
  * `connectGeoSearch`: [`geoSearch`](/doc/api-reference/widgets/geo-search/js#param-send-event)
  * `connectHierarchicalMenu`: [`hierarchicalMenu`](/doc/api-reference/widgets/hierarchical-menu/js#param-send-event)
  * `connectHits`: [`hits`](/doc/api-reference/widgets/hits/js#param-send-event)
  * `connectInfiniteHits`: [`infiniteHits`](/doc/api-reference/widgets/infinite-hits/js#param-send-event)
  * `connectMenu`: [`menu`](/doc/api-reference/widgets/menu/js#param-send-event) and [`menuSelect`](/doc/api-reference/widgets/menu-select/js#param-send-event) \*
  * `connectNumericMenu`: [`numericMenu`](/doc/api-reference/widgets/numeric-menu/js#param-send-event)
  * `connectRange`: [`rangeInput`](/doc/api-reference/widgets/range-input/js#param-send-event) and [`rangeSlider`](/doc/api-reference/widgets/range-slider/js#param-send-event) \*
  * `connectRatingMenu`: [`ratingMenu`](/doc/api-reference/widgets/rating-menu/js#param-send-event) \*
  * `connectRefinementList`: [`refinementList`](/doc/api-reference/widgets/refinement-list/js#param-send-event)
  * `connectToggleRefinement`: [`toggleRefinement`](/doc/api-reference/widgets/toggle-refinement/js#param-send-event)

  <Note>
    \* The `sendEvent` method provided to `connectNumericMenu`, `connectRange`, and `connectRatingMenu` can't be used to send [clicked filter](/doc/libraries/sdk/v1/methods/clicked-filters) events.
  </Note>

  ```js JavaScript icon=code theme={"system"}
  const renderRefinementList = (renderOptions, isFirstRendering) => {
    const { sendEvent } = renderOptions;

    // `sendEvent` from `connectRefinementList` can send `click` events.
    const clickedFacetValue = "Apple";

    // This sends an event like the following:
    /*
      {
        eventType: 'click',
        insightsMethod: 'clickedFilters',
        payload: {
          eventName: 'Filter Applied',
          filters: ['brand:"Apple"'],
          index: '<your-index-name>',
        },
        widgetType: 'ais.refinementList',
      }
    */
    sendEvent("click", clickedFacetValue);

    // Or, you can send a custom payload
    sendEvent({
      eventType: "click",
      insightsMethod: "clickedFilters",
      payload: {
        eventName: "Filter Applied",
        filters: [`brand:${JSON.stringify(clickedFacetValue)}`],
        index: "<your-index-name>",
      },
      widgetType: "ais.refinementList",
    });
  };

  const customRefinementList = connectRefinementList(renderRefinementList);
  ```
</ParamField>

<ParamField body="Hits">
  The [`hits`](/doc/api-reference/widgets/hits/js) and [`infiniteHits`](/doc/api-reference/widgets/infinite-hits/js)
  widgets provide templates with an exposed `sendEvent` function.

  The `sendEvent(eventType, payload, eventName)` function triggers an event.
  When clicking the button, InstantSearch sends it to Algolia, or forwards it to `onEvent` if provided.

  * `eventType: 'click' | 'conversion'`
  * `payload: Hit`
  * `eventName: string`

  ```js JavaScript icon=code theme={"system"}
  hits({
    // or `infiniteHits`
    // ...
    templates: {
      item: (hit, { html, components, sendEvent }) => {
        return html`
          <div>
            <p>${components.Highlight({ attribute: "title", hit })}</p>
            <button
              type="button"
              onClick=${() => sendEvent("conversion", hit, "Product Added")}
            >
              Add to cart
            <button>
          </div>
        `;
      },
    },
  });
  ```
</ParamField>
