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 Action 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
User Affinity code injection example
index.search({
query: 'query',
optionalFilters: DY.CS.affinityToAlgolia
}).then(res => {
// console.log(res);
});
User Affinity custom action code
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;
}