Browser Session Based Recommendation


Session-based recommendations provide personalized suggestions based on real-time user interactions within a session. This approach is ideal for environments where users' preferences might shift rapidly, such as during a single visit to a website.

Use Case

This method is suited for scenarios where users interact with various items in a short period, and these interactions can be leveraged to recommend additional, relevant items before the session ends.

Gathering Session Information

To implement session-based recommendations, first collect user interactions within the current session. This typically involves tracking clicks, views, or other forms of engagement with items, and can be achieved using client-side scripting (like JavaScript) to capture these events and timestamps.

Example of Gathering User Interactions in a Browser

Use JavaScript to listen for user interactions and store them in an object, which will later be passed to the recommendation engine. Here's a basic example:

// Object to store user interactions
let userInteractions = {};

// Function to add interactions
function addInteraction(itemId, rating) {
  userInteractions[itemId] = {
    timestamp: Math.floor( / 1000), // Current UNIX timestamp
    rating: rating  // A simulated rating based on user interaction

// Example interaction
document.getElementById('item-89898').addEventListener('click', function() {
  addInteraction('89898', 4.5);

Making the API Call

After gathering the necessary data, make an API request to retrieve recommendations. Set recommend_seen to true to allow the model to recommend items the user has already interacted with during the session, which can be useful for re-engagement.

API Call Using CURL

Here is how you would send the collected interaction data to your recommendation API using CURL:

POST /iid_recommend HTTP/1.1
Content-Type: application/json

  "user_interactions": {
    "89898": {"timestamp": 1704060026, "rating": 4.5},
    "22898": {"timestamp": 1704060026, "rating": 4.5},
    "21255": {"timestamp": 1704060026, "rating": 4.5}
  "k": 10,
  "recommend_seen": true

Key Parameters

  • user_interactions (object): An object containing user interactions, with each key being an item ID and its value being an object that includes timestamp and rating.

  • k (int): The number of recommendations to return.

  • recommend_seen (bool): If set to true, the model will include items the user has previously interacted with in the recommendations.

Understanding the Recommendations Output

The response from the API will include a list of recommended items along with their relevance scores, indicating the potential interest for the user.

Example Output

  "recommendations": [
    {"id": "21098", "score": 0.950},
    {"id": "22139", "score": 0.900},
    {"id": "23000", "score": 0.850},
    // additional recommendations
  "message": "Recommendations based on session interactions."

Last updated