Customize
While the extension provides JavaScript libraries such as Autocomplete, InstantSearch, and Insights, your customizations modify the implementation, not the libraries. These libraries are here to help add to your customization. As the extension has already initialized these components, you should hook into the area between the extension and the libraries.Frontend JavaScript libraries and the legacy bundle
Depending on your extension version, you can have different versions of the Autocomplete or InstantSearch libraries installed. For more information, refer to the extension’s repository. Knowing the version of the extension helps you understand what’s available in these libraries. It also helps you find which documentation to reference when you start working on your customization. Before version 3.15.0, the extension included a single minified bundle file calledalgoliaBundle.min.js to hold the Autocomplete and InstantSearch libraries and their dependencies.
These were previously accessible from the global algoliaBundle and were added to most event hooks for access.
As of version 3.15.0, support for the bundle has been discontinued and these libraries are now included individually in the Magento extension and loaded with RequireJS.
This has a number of advantages:
- Easier library upgrades as newer versions become available in between official releases of the Algolia Magento extension
- Reduce asset size by excluding libraries that are not useful to your implementation
- You can use mixins (in addition to Algolia’s frontend events) for easier customizations without having to override entire files
- You can use your own JavaScript bundling approach such as standard and advanced bundling
algoliaBundle as a dependency, but be aware that it will be removed in a future release.
Hooks still pass the bundle as an argument as in previous versions of the extension,
but the object won’t contain all legacy libraries that were previously included in the bundle.
If you need any of these libraries for your customizations,
see Bundle stuffing.
What’s included?
Libraries that are packaged with the extension include:algolia-search.min.jsandalgolia-search-helper.min.jsfor communicating with Algolia from the frontendalgolia-instantsearch.min.jsfor building the search interface with InstantSearchalgolia-autocomplete.min.jsfor building the Autocomplete search boxsearch-insights.min.jsfor the insights features like Click and Conversion Analytics and Personalizationrecommend.min.jsandrecommend-js.min.jsto support Algolia Recommend featureshogan.min.jsandmustache.min.jsfor legacy templating support
Accessing frontend configurations
The extension handles the implementation of the frontend features based on your configuration in Stores > Configuration > Algolia Search. The extension stores these settings in the global JavaScript objectalgoliaConfig.
The data for this object returns from the block method Algolia\AlgoliaSearch\Block\Configuration::getConfiguration() and is set in the configuration.phtml template file.
The Autocomplete and InstantSearch implementations refer to this global variable to build their UI.
To inspect the algoliaConfig object, type algoliaConfig in your browser’s console when on your Magento store.

Dependency loading with RequireJS
Before version 3.10.3, the extension loaded its JavaScript resources withscript tags.
This had several disadvantages, such as:
- Pollution of the global namespace
- Inability to defer loading of JavaScript asynchronously to improve perceived page load times
- Inability to ensure that all dependencies are always available to the calling resource
window namespace in your custom implementation,
first use require or define to ensure they’re loaded and ready for your customizations.
For instance, several global objects are loaded by algoliaCommon, which contains the object algolia.
If you want to reference this object to register a new frontend hook,
you must pass algoliaCommon as a dependency.
For example, the following naked call to the algolia object is no longer supported:
JavaScript
JavaScript
define and a declaration in requirejs-config.js. Refer to the “Where to place your hook” guidelines to implement an algoliaHooks override to guarantee that Algolia’s libraries (like Autocomplete and InstantSearch) load in the proper sequence after you have registered your custom hooks.
For an example of how to implement this kind of customization, see the CustomAlgolia sample module on GitHub.
How to use event hooks
The extension provides hooks to help you add or change parameters for implementation. Thealgolia window variable is an object that handles the setting and firing of these events. (To ensure that the algolia object is available to your custom implementation, declare algoliaCommon as a RequireJS dependency.)
Try typing algolia in your browser’s console when on your Magento store. Your console outputs an object with methods that help trigger events throughout the frontend implementation for Autocomplete, InstantSearch, and Insights.
You can see all available events by entering algolia.allowedHooks in your browser’s console.

It’s highly recommended that you find where these events dispatch in the extension to help understand what parameters are available to use and what to return.
algolia object is to add your callback function to the available events. To add your hook, use the algolia.registerHook method, which accepts the parameters of eventName and the callback function.
JavaScript
beforeWidgetInitialization event. The event fires from the instantsearch.js file as:
JavaScript
allWidgetConfiguration. This variable holds all the InstantSearch widgets configuration already created for you based on your extension settings. Using this event, you can return a modified allWidgetConfiguration for the InstantSearch implementation.
Where to place your hook
You must add your hook before the event fires by placing your JavaScript customization in your custom module or theme. Don’t edit the extension or override the extension files unless necessary. The extension adds JavaScript files using RequireJS. Using the dependencyalgoliaHooks ensures that your hooks fire at the appropriate time before the InstantSearch or Autocomplete UI is built.
Override this file using requirejs-config.js as demonstrated below:
JavaScript
Debugging your hook
If your hook doesn’t fire, use your browser’s developer tools to add a break-point on the event. You can then inspectalgolia.registeredHooks to see all the registered hooks added at that point of the script.

common.js. The extension requires that common.js runs before to access the algolia global.
Please remember that hooks don’t need to wait for DOM ready. In fact, running it after DOM ready registers the hooks after the event fires which isn’t ideal.
Autocomplete menu events
The following diagram demonstrates the process by which events are triggered when Autocomplete loads:
algolia object.
To learn how to add a custom JavaScript, see Create a custom extension.
Possible hooks:
afterAutocompleteSources(sources, algoliaClient)- Can be used to modify default data sources
- The hook must return
sourcesvariable
afterAutocompletePlugins(plugins, algoliaClient)- Can be used to modify default plugins
- The hook must return
pluginsvariable
afterAutocompleteOptions(options)- Can be used to modify default options of autocomplete menu
- The hook must return
optionsvariable
afterAutocompleteProductSourceOptions(options)- Can be used to modify default options of the products source of autocomplete menu
- Are also referred to as search parameters
- The hook must return
optionsvariable
JavaScript
afterAutocompleteStart(algoliaAutocompleteInstance)- can be used to observe events on the autocomplete element
- the hook must return
algoliaAutocompleteInstancevariable
JavaScript
InstantSearch page events
You can adjust all the logic of the InstantSearch.js integration by registering a couple of custom hooks:beforeInstantsearchInit(instantsearchOptions)- Modifies default
instantsearchoptions.
- Modifies default
beforeWidgetInitialization(allWidgetConfiguration)- Adds, removes, or modifies any widgets.
beforeInstantsearchStart(search)- Modifies the
instantsearchinstance before calling thestart()method.
- Modifies the
afterInstantsearchStart(search)- Modifies the
instantsearchinstance after calling thestart()method.
- Modifies the
beforeFacetInitialization(builders)- Modifues the logic for any facet by means of “builder” functions which return a facet configuration that will drive the rendering and behavior of the facet.
beforeInstantsearchInit(instantsearchOptions) hook:
toggleRefinement widget to the InstantSearch page:
allWidgetConfiguration object and can be removed or modified in this method.
However, some widgets, like hits, can not be added multiple times. In that case, you should add the widget manually in beforeInstantsearchStart:
color facet in any number of ways using the beforeFacetInitialization(builders) hook:
JavaScript
refinementList widget.
Insights events
You can add new events for Click and Conversion Analytics and Personalization by registering this custom hook:afterInsightsBindEvents(algoliaInsights)- can be used to add new events
-
algoliaInsightsgives you access to methods for tracking:trackClick(eventData)trackView(eventData)trackConversion(eventData)
-
to format
eventDatafor insights you can use:buildEventData(eventName, objectID, indexName, position = null, queryID = null)- Click and Conversion Analytics requires the optional parameters for
positionandqueryID
JavaScript
Example usages
You can use this section as inspiration for your customization.Add category sorting in the Autocomplete menu
This examples creates a new section that displays categories with the product results. To learn more about templating, see Create a custom extension. To get the categories that are returned by a product search, you need to create a new section that queries the products index. When you define the source of this section, pass thefacets parameter that returns the categories attribute which the extension indexes as: categories.level0 or categories.level2.
The search returns product results as hits, so you need to render the facets instead of products.
You can render the facets in the template suggestions function.
Make sure that your template variables match those returned by the facets to render them in autocomplete.
JavaScript
Add filters to your autocomplete search
To add a new filter, modify the section’s search parameters to include the new filter condition. To do this, you need to recreate the source with the updated options. The example below adds a newnumericFilters to the search parameters, in_stock=1.
To recreate the source value, you need to recreate the options passed into the product source as defined in common.js.
The numericFilters returns an array of conditions that includes the new condition.
JavaScript
Add custom rules context to InstantSearch
If you have to add new contexts for some of your custom rules, use the specified frontend hook for your InstantSearch version. You should add your new context to the preconfigured contexts:Sort facet values with InstantSearch
By default, facet values displayed for a search are sorted by the number of matching results. The following example shows, how to sort the size facet values based on thesize values.
To do this, you need to hook into the event beforeWidgetInitialization to modify the facet for size.
To sort the values in the refinementList based on size, add the sortBy function to the refinementList. This function lets you match values against each other and sort each value.
To determine the order, use the orderedSizes array variable defined in the code below. This can be pulled from any source, like Magento or from another index if needed.
JavaScript