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

# Add custom hooks

> Custom hooks let you customize the behavior of the Algolia AI Search & Discovery app.

<Info>
  Shopify apps can't modify theme code directly.
  To integrate Autocomplete and InstantSearch,
  use the Algolia AI Search & Discovery app's **App Embed** and **App Blocks**.

  For more information, see:

  * [Manual theme configuration](/doc/integration/shopify/getting-started/quick-start#manual-theme-configuration)
  * [Shopify Algolia configuration](/doc/integration/shopify/sending-and-managing-data/algolia-config)
</Info>

Hooks are functions that run at specific points while the Algolia AI Search & Discovery app loads and runs.
Use hooks to customize the app's behavior.

To see which hooks are available for your store,
run `window.algoliaShopify.hooks.allowedHooks` in your browser's developer tools.

<img src="https://mintcdn.com/algolia/IBpvqiDfc9oOPDK9/doc/integration/shopify/building-search-ui/allowed-hooks.png?fit=max&auto=format&n=IBpvqiDfc9oOPDK9&q=85&s=ee81846c041abcbdf5aed18185db36a7" alt="Screenshot of a code snippet showing 'algoliaShopify.hooks.allowedHooks' with hook names like 'beforeAutocompleteOptions' and 'beforeInstantSearchOptions'." width="3456" height="1352" data-path="doc/integration/shopify/building-search-ui/allowed-hooks.png" />

## Add custom hooks

To add custom hooks, add a new JavaScript file, register the hook, and include that file in your theme.

<Steps>
  <Step title="Add a new JavaScript file">
    1. In your store's Shopify admin, go to **Sales channels > Online Store > Themes**.

    2. Select the theme you want to edit and click <Icon icon="ellipsis" /> **Click for more theme actions > Edit code**.

           <img src="https://mintcdn.com/algolia/IBpvqiDfc9oOPDK9/doc/integration/shopify/building-search-ui/edit-theme-code.png?fit=max&auto=format&n=IBpvqiDfc9oOPDK9&q=85&s=7a05e30460be5d2b40981efb26fbd2a1" alt="Screenshot of the theme code editor" width="365" height="315" data-path="doc/integration/shopify/building-search-ui/edit-theme-code.png" />

    3. In the **assets** section, click **New file**.

    4. Enter a filename, for example, `algolia_custom_hooks.js`.
  </Step>

  <Step title="Register a hook function">
    1. Open the custom hooks JavaScript file you created in the last step (for example, `algolia_custom_hooks.js`).
    2. Add an event listener to the `algolia.hooks.initialize` event and call the `algoliaShopify.hooks.registerHook` function. For example:

       ```js JavaScript icon=code theme={"system"}
       document.addEventListener("algolia.hooks.initialize",
         function () {
           algoliaShopify.hooks.registerHook(
             "beforeAutocompleteOptions",
             function (options) {
               // ...
             },
           );
         }
       );
       ```
  </Step>

  <Step title="Include the JavaScript file in your theme">
    1. Under **layouts**, open the `theme.liquid` file.

    2. Go to the bottom of the `theme.liquid` file and add the following code just before the `</body>` tag. Replace `NEW_FILE` with the JavaScript file that contains your custom hooks, for example `algolia_custom_hooks`:

       ```html HTML icon=code-xml theme={"system"}
       <script src="{{ 'CUSTOM_HOOKS_FILE.js' | asset_url }}" defer="defer"></script>
       ```

    3. Save the file.
  </Step>
</Steps>

## Hook names

Your hook function must return the expected type.
The hook name suffix indicates the expected return type:

| Hook name ends with | Expected return type                                                                                           |
| ------------------- | -------------------------------------------------------------------------------------------------------------- |
| `Options`           | Object                                                                                                         |
| `Template`          | [Tagged template literal](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) |
| `Array`             | Array of strings or objects                                                                                    |
| `String`            | String                                                                                                         |
| `Number`            | Number                                                                                                         |
| `Action`            | Function                                                                                                       |

## See also

* [Autocomplete hooks](/doc/integration/shopify/building-search-ui/autocomplete-hooks)
* [Insights and Recommend hooks](/doc/integration/shopify/building-search-ui/insights-recommend-hooks)
* [InstantSearch hooks](/doc/integration/shopify/building-search-ui/instantsearch-hooks)
* [Global events](/doc/integration/shopify/building-search-ui/global-events)
