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

# ais-query-rule-context

> Applies rule contexts based on applied filters for triggering context-dependent rules.

```vue Signature theme={"system"}
<ais-query-rule-context
  :tracked-filters="object"
  // Optional parameters
  :transform-rule-contexts="function"
/>
```

## Import

<Tabs>
  <Tab title="Component">
    To ensure optimal bundle sizes,
    see [Optimize build size](/doc/guides/building-search-ui/going-further/improve-performance/vue#optimize-build-size).

    ```js Vue icon=code theme={"system"}
    import { AisQueryRuleContext } from "vue-instantsearch";
    // Use "vue-instantsearch/vue3/es" for Vue 3

    export default {
      components: {
        AisQueryRuleContext
      },
      // ...
    };
    ```
  </Tab>

  <Tab title="Plugin">
    This imports all widgets, even the ones you don't use.
    For more information, see [Get started with Vue InstantSearch](/doc/guides/building-search-ui/getting-started/vue).

    ```js JavaScript icon="code" theme={"system"}
    import Vue from "vue";
    import InstantSearch from "vue-instantsearch";
    // Use "vue-instantsearch/vue3/es" for Vue 3

    Vue.use(InstantSearch);
    ```
  </Tab>
</Tabs>

<Card title="See this widget in action" icon="monitor-play" href="https://instantsearchjs.netlify.app/stories/vue/?selectedKind=ais-query-rule-context" horizontal>
  Preview this widget and its behavior.
</Card>

## About this widget

The `ais-query-rule-context` widget lets you apply [`ruleContexts`](/doc/api-reference/api-parameters/ruleContexts)
based on filters to trigger context dependent [rules](/doc/guides/managing-results/rules/rules-overview).

Rules offer a custom experience based on contexts.
You might want to customize the users' experience based on the filters of the search
(for example, they're visiting the "Mobile" category, they selected the "Thriller" genre, etc.)
This widget lets you map these filters to their associated contexts, so you can trigger context based rules on refinement.

## Examples

```vue Vue icon=code theme={"system"}
<template>
  <ais-query-rule-context :tracked-filters="trackedFilters" />
</template>

<script>
export default {
  data() {
    return {
      trackedFilters: {
        genre: () => ["Comedy", "Thriller"],
        rating: (values) => values,
      },
    };
  },
};
</script>
```

## Props

<ParamField body="tracked-filters" type="object" required>
  The filters to track to trigger rule contexts.

  Each filter is a function which name is the attribute you want to track.
  They receive their current refinements as arguments.
  You can either compute the filters you want to track based on those, or return static values.
  When the tracked values are refined, it toggles the associated rule contexts.

  The added rule contexts follow the format `ais-{attribute}-{value}` (for example `ais-genre-Thriller`).
  If the context of your rule follows another format,
  you can specify it using the [`transformRuleContexts`](#param-transform-rule-contexts) option.

  Values are escaped so that they only consist of alphanumeric characters, hyphens, and underscores.
  Non-matching characters are replaced with underscores.

  ```vue Vue icon=code theme={"system"}
  <template>
    <ais-query-rule-context :tracked-filters="trackedFilters" />
  </template>

  <script>
  export default {
    data() {
      return {
        trackedFilters: {
          genre: () => ["Comedy", "Thriller"], // this tracks two static genre values
          rating: (values) => values, // this tracks all the rating values
        },
      };
    },
  };
  </script>
  ```
</ParamField>

<ParamField body="transform-rule-contexts" type="function">
  A function to apply to the rule contexts before sending them to Algolia.
  This is useful to rename rule contexts that follow a different naming convention.

  ```vue Vue icon=code theme={"system"}
  <template>
    <ais-query-rule-context
      [...]
      :transform-rule-contexts="transformRuleContexts"
    />
  </template>

  <script>
  export default {
    data() {
      return {
        transformRuleContexts: (ruleContexts) =>
          ruleContexts.map((ruleContext) =>
            ruleContext.replace("ais-", "custom-")
          ),
      };
    },
  };
  </script>
  ```
</ParamField>
