Display recommendations with javascript

rek.ai API - Javascript

Get a recommendation with Javascript

By using Javascript you get full control over how your recommendations are rendered. The easiest way to test Javascript recommendations is to run the following code in the console:

function myCallback(data) {
  console.log(data)
}
 
var options = {}
 
window.__rekai.predict(options, myCallback);

To use the parameters added via data parameter

All parameters that can be added to a div can be added to an "overwrite" object.

function myCallback(data) {
  console.log(data)
}
 
var options = {
  overwrite: {
    subtree: 'news',
    addcontent: true
  }
}
 
window.__rekai.predict(options, myCallback);

Example rendering and Javascript

It is easy to print out recommended hits as html so that you can add your dedicated classes or structures.

<div class="output"></div>
 
<script>
  // Function for rendering predictions
  function renderHTML(data) {
    var s = '<ul>';
    // Iterate recommendations
    for(var i = 0; i <  data.predictions.length; i++) {
      s += '<li class="my-recommendation"><a href="' + data.predictions[i].url + '">' + data.predictions[i].title + '</a></li>';
    }
    s += '</ul>';
    $('.output').html(s);
  }
 
  $( document ).ready(function() {
    var options = {
      overwrite: { // Add for example subtree. All parameters that can be added as data can be added here
        addcontent: true
      }
    }
 
    // Do prediction with options and the render function
    window.__rekai.predict(options, renderHTML);
  });
</script>

A longer example of how you can use your own template