Sample snippets

Last updated on
20 January 2021

Custom Item ID

It is possible to implement your own function to return the item ID. The module does ship with its own default implementation based on a metatag which is set by the module. This identifier is the same as used by the Search API Recombee module and so the identity should be aligned. This should work for the majority of people. However, if you have custom needs you can do it as follows.

(function ($) {
  // Attach Item ID callback for Recombee Public Scenario.
  window.RecombeeItemId = function () {
    // Process your logic to fill the item ID.
    window.RecombeeItemId = ... ;
    // Inform main script about success.
    $document.trigger('recombeePropertiesUser');
  }
})(jQuery);

Custom User ID

It is possible to implement your own function to return the user ID. The module does ship with its own default implementation based on a random identifier stored in a first-party cookie. This should work for the majority of people. However, if you have custom needs you can do it as follows.

(function ($) {
  // Attach User ID callback for Recombee Public Scenario.
  window.RecombeeUserId = function () {
    // Process your logic to fill the user ID.
    window.RecombeeUserId = ... ;
    // Inform main script about success.
    window.recombeePropertiesReady = true;
    $(document).trigger('recombeePropertiesReady');
  }
})(jQuery);

Calling the Recombee client directly

The need may arise to log your own custom events into Recombee. For example, a product purchase may trigger an event. If your module needs to do this, the Recombee client is stored in a reliable place for you to use.

function YOUR_MODULE_preprocess_SOME_HOOK(&$variables) {
  // Attach Recombee API Client.
  recombee_attach_client($variables);
}
// Get Recombee Item ID and User ID.
var userId = window.RecombeeUserId;
var itemId = window.RecombeeItemId;
// Define your interaction.
var interaction = new window.recombee.AddPurchase(userId, itemId);
// Send your interaction and catch eventual error.
window.RecombeeClient.send(interaction)
  .catch(function (error) {
    console.error('Recombee AddPurchase: ' + error);
  });

Help improve this page

Page status: No known problems

You can: