> ## Documentation Index
> Fetch the complete documentation index at: https://www.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.

```js Signature theme={"system"}
import {
  createInsightsMiddleware
} from "instantsearch.js/es/middlewares";

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

## About this widget

> [!NOTE]
>
> Starting from v4.9.0, you can use the [`insights`](https://www.algolia.com/doc/api-reference/widgets/instantsearch/vue#param-insights)
> option instead of setting up the Insights middleware yourself.

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 if needed
3. Send events from your own custom widgets

### Requirements

* [`search-insights`](https://github.com/algolia/search-insights.js) v1.6.2 or later.
* [Vue InstantSearch](https://github.com/algolia/instantsearch/tree/master/packages/vue-instantsearch) v3.7.0 or later.

See also: [Send click and conversion events with Vue InstantSearch](https://www.algolia.com/doc/guides/building-search-ui/events/vue.md)

## Examples

```js JavaScript icon=code theme={"system"}
import { createInsightsMiddleware } from "instantsearch.js/es/middlewares";

createInsightsMiddleware({
  insightsClient,
  insightsInitParams,
  onEvent,
});
```

## Options

**insightsClient** `InsightsClient | null`

The [Insights client](https://www.algolia.com/doc/libraries/search-insights?client=javascript) is used to send events.
It synchronizes the [user token](https://www.algolia.com/doc/guides/sending-events/concepts/usertoken.md#exclude-users-who-want-to-opt-out-of-analytics-recommend-and-personalization) between search, Recommend, and analytics calls.

When the middleware is enabled, it adds `userToken` and `clickAnalytics` to Recommend requests and fills missing values in the widget's existing `fallbackParameters`.
Values in the widget's `queryParameters` and `fallbackParameters` take precedence.

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

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

<!-- In your .vue file -->
<template>
  <ais-instant-search
    :index-name="<index-name>"
    :search-client="searchClient"
    :middlewares="middlewares"
  >
    <!-- widgets -->
  </ais-instant-search>
</template>

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

  export default {
    data() {
      return {
        // ...
        middlewares: [insightsMiddleware]
      }
    },
    created() {
      const userToken = // Get the user token (synchronously or asynchronously).

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

```vue ESM theme={"system"}
<template>
  <ais-instant-search
    :index-name="<index-name>"
    :search-client="searchClient"
    :middlewares="middlewares"
  >
    <!-- widgets -->
  </ais-instant-search>
</template>

<script>
  import aa from 'search-insights';
  import { createInsightsMiddleware } from 'instantsearch.js/es/middlewares';

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

  export default {
    data() {
      return {
        // ...
        middlewares: [insightsMiddleware]
      }
    },
    created() {
      const userToken = // Get the user token (synchronously or asynchronously).

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

**insightsInitParams** `object`

Insights parameters to forward to the Insights client's [`init`](https://www.algolia.com/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`](https://www.algolia.com/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.

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

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

**onEvent** `(event: InsightsEvent, aa: null | InsightsClient) =\> void` (default: undefined)

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.
This 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.

**event properties**

**event.insightsMethod** `string`

The Insights method, such as, `'viewedObjectIDs'`, `'clickedObjectIDsAfterSearch'`.
For more information, see the [Insights API reference](https://www.algolia.com/doc/libraries/search-insights).

**event.payload** `[key: string]: any`

Event payload.

**event.widgetType** `string`

Widget type given by connectors, such as `'ais.refinementList` or `'ais.hits`.

**event.eventType** `string`

Event type, such as `'view'`, `'click'`, `'conversion'`, or anything else if you customized it.

```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);
    }
  },
});
```

## Custom events

**Connectors**

Many of the InstantSearch connectors expose the `sendEvent` method.
If you use these connectors to create custom widgets,
you can use the method to send custom events.

Here's a list of connectors that expose `sendEvent` and they're exposed at the default slot of corresponding Vue InstantSearch components.

* `connectAutocomplete`: [`ais-autocomplete`](https://www.algolia.com/doc/api-reference/widgets/autocomplete/vue#param-default)
* `connectHierarchicalMenu`: [`ais-hierarchical-menu`](https://www.algolia.com/doc/api-reference/widgets/hierarchical-menu/vue#param-default)
* `connectHits`: [`ais-hits`](https://www.algolia.com/doc/api-reference/widgets/hits/vue#param-default)
* `connectInfiniteHits`: [`ais-infinite-hits`](https://www.algolia.com/doc/api-reference/widgets/infinite-hits/vue#param-default)
* `connectMenu`: [`ais-menu`](https://www.algolia.com/doc/api-reference/widgets/menu/vue#param-default) and [`ais-menu-select`](https://www.algolia.com/doc/api-reference/widgets/menu-select/vue#param-default)
* `connectNumericMenu`: [`ais-numeric-menu`](https://www.algolia.com/doc/api-reference/widgets/numeric-menu/vue#param-default) \*
* `connectRange`: [`ais-range-input`](https://www.algolia.com/doc/api-reference/widgets/range-input/vue#param-default) \*
* `connectRatingMenu`: [`ais-rating-menu`](https://www.algolia.com/doc/api-reference/widgets/rating-menu/vue#param-default) \*
* `connectRefinementList`: [`ais-refinement-list`](https://www.algolia.com/doc/api-reference/widgets/refinement-list/vue#param-default)
* `connectToggleRefinement`: [`ais-toggle-refinement`](https://www.algolia.com/doc/api-reference/widgets/toggle-refinement/vue#param-default)

> [!NOTE]
>
> The `sendEvent` method provided to `connectNumericMenu`, `connectRange`, and `connectRatingMenu` can't be used to send [clicked filter](https://www.algolia.com/doc/libraries/sdk/v1/methods/clicked-filters.md) events.

```vue-html Vue theme={"system"}
<ais-hits>
  <template v-slot="{ items, sendEvent }">
    <ul>
      <li v-for="item in items" :key="item.objectID">
        <p>{{ item.name }}</p>
        <button
          type="button"
          @click="sendEvent('click', item, 'Item Starred')"
        >
          Star
        </button>

        <button
          type="button"
          @click="sendEvent('conversion', item, 'Item Ordered')"
        >
          Order
        </button>
      </li>
    </ul>
  </template>
</ais-hits>
```
