Algolia is a powerful search-as-a-service solution that powers search for the world’s top brands in a variety of verticals. The Integration with Algolia leverages the Dynamic Yield insightful user engagement attributes and Machine Learning-powered insights, adding a layer of personalization to Algolia.
Use case
Personalize search results to provide the best match for each visitor (affinity-based).
Personalization is enabled by factoring Dynamic Yield user affinity to search results.
Prerequisites
- A Dynamic Yield account and an Algolia account. Each account is sold and managed separately.
- A Dynamic Yield product feed. Multi-language feeds are not supported.
Injecting affinity: How it works
- Create a custom code campaign, and target all pages that are serving Algolia.
- Add a variation and paste the appropriate code. See code examples.
- Save the Custom Action.
The Custom Action exposes an object called DY.CS.affinityToAlgolia that contains the Dynamic Yield Affinity in Algolia format for injecting optional filters.
When you initialize the Algolia API:
- Add the key optionalFilters to the object.
- Assign the object DY.CS.affinityToAlgolia as a value.
Code examples
index.search({
query: 'query',
optionalFilters: DY.CS.affinityToAlgolia
}).then(res => {
// console.log(res);
});
var userId = DYO.StorageUtils.get('_dyid','localStorage');
var dyHost = DYO.hosts.rcom;
window.DY.CS = window.DY.CS || {};
getAffinity(DY.scsec, userId, 2)
function getAffinity(section,userId,limit){
var xhr = new XMLHttpRequest();
xhr.open('GET', dyHost + '/userAffinities?limit='+limit+'&sec='+section+(userId?'&uid='+userId:""));
xhr.onload = function() {
if (xhr.status === 200) {
console.log('got user affinity : ' +xhr.responseText);
DY.CS.affinityAlgolia = DYAffinityToAlgoliaFilter(JSON.parse(xhr.responseText));
}
else {
console.log('error getting affinity');
}
};
xhr.send();
}
function DYAffinityToAlgoliaFilter(affinities){
var filter = [];
if (affinities !== {}){
for (var affinity in affinities){
for (var val in affinities[affinity]){
filter.push(affinity+":"+val+"<score="+affinities[affinity][val]+">")
}
}
}
return filter;
}
Troubleshooting the Algolia integration
Ensure that the DY.CS.affinityToAlgolia object is defined in the browser, and is populating with data properly:
- View several PDPs and add items to the cart, to build user affinity.
- Navigate to a page that serves Algolia, open the dev tools console, and enter DY.CS.affinityToAlgolia. This returns an array with the affinities you built and their associated scores.
- If the expected response is not returned, do the following:
- Check that the custom code campaign is active, that it's targeting the correct pages, and that it includes the correct code.
- Verify that you correctly built user affinity by running the User Affinity client-side API. If the response returns your user affinity data, this part of the implementation is working correctly.
Validate the Algolia network call:
- Navigate to a page that serves Algolia, open the dev tools console, and go to the Network tab.
- Enter "Algolia" in the network filter, and run a search query on the website.
- In the results, look for a network call that begins with "query?x-algolia-agent" and select it. This is the call your site makes to Algolia with the search criteria.
- In the network call payload, locate the parameter optionalFilters in the form data. This parameter passes the Dynamic Yield user affinity to Algolia in an ingestible format.
- If the parameter doesn't exist, implement the user affinity code injection for the example in the Code examples section.
- If the parameter does exist with your user affinity data, this part of the implementation is working correctly.