Pages

Recommendations require the Frosmo Recommendations add-on for the Frosmo Control Panel. To get the add-on, if you do not already have it, contact your Frosmo representative or sales@frosmo.com.

This guide shows you how to create and manage recommendations using the Graniitti API. This guide also describes the recommendation resource data model.

For instructions on how to develop recommendations with the Frosmo Platform, see the Frosmo Recommendations guide.

Introduction to the recommendation resource

The recommendation resource of the Graniitti API combines both a recommendation configuration and the recommendation data generated for that configuration:

  • When you retrieve a recommendation, the API returns both the configuration and the data.
  • When you create or update a recommendation, the API only creates or updates the configuration. The Frosmo Platform then separately generates the data and stores it for the recommendation.
  • When you delete a recommendation, the API deletes both the configuration and the data.

For actual recommendations displayed on a site, you retrieve the recommendation data with the Recommendations API.

You can also create, update, and delete recommendations in the Frosmo Control Panel. For more information, see Creating recommendations.

Working with recommendations

You can use the Graniitti API to:

The API provides the /sites/<site_id>/recommendations endpoint for working with recommendations. For complete information about the endpoint, see the Graniitti API reference.

Retrieving recommendations

Retrieving recommendations can take a long time if the recommendations contain huge amounts of recommendation data.

To retrieve all recommendations for a site, send a GET request to /sites/<site_id>/recommendations.

curl -X GET \
--header 'Authorization: Bearer <access_token>' \
'https://<graniitti_api_domain>/v0/sites/<site_id>/recommendations'

To retrieve a single recommendation for a site, send a GET request to /sites/<site_id>/recommendations/<recommendation_id>, where <recommendation_id> is the ID of the recommendation.

curl -X GET \
--header 'Authorization: Bearer <access_token>' \
'https://<graniitti_api_domain>/v0/sites/<site_id>/recommendations/<recommendation_id>'

If the request is successful, the API returns a JSON array of objects for GET /sites/<site_id>/recommendations and a JSON object for GET /sites/<site_id>/recommendations/<recommendation_id>. An object contains the configuration and data for a single recommendation, that is, the full data of a recommendation resource.

Creating recommendations

To create a new recommendation for a site, send a POST request to /sites/<site_id>/recommendations. Include the new recommendation configuration as a JSON object in the request body.

curl -X POST \
--header 'Authorization: Bearer <access_token>' \
--header 'Content-Type: application/json' \
-d '{<recommendation_configuration>}' \
'https://<graniitti_api_domain>/v0/sites/<site_id>/recommendations'

If the request is successful, the Frosmo Platform queues the recommendation for processing.

To track the generation progress, either retrieve the recommendation (optionally with the fields=status query parameter to minimize the response size) and check the status property in the returned JSON object, or check the recommendation status in the Frosmo Control Panel. For more information about the status, see Creating recommendations.

Here's an example of a recommendation configuration to pass in the POST request:

{
  "name": "Most viewed products",
  "description": "Top 10 most viewed products (24 h)",
  "type": "most_viewed",
  "status": "new",
  "frequency": "1h",
  "hours": 24,
  "limit": 10,
  "map_ids": "product",
  "params": [
    "type"
  ],
  "options": {
    "product_attrs": [
      "type"
    ]
  }
}

Updating recommendations

To update an existing recommendation for a site, send a PUT request to /sites/<site_id>/recommendations/<recommendation_id>, where <recommendation_id> is the ID of the recommendation. Include the updated recommendation configuration as a JSON object in the request body.

curl -X PUT \
--header 'Authorization: Bearer <access_token>' \
--header 'Content-Type: application/json' \
-d '{<recommendation_configuration>}' \
'https://<graniitti_api_domain>/v0/sites/<site_id>/recommendations/<recommendation_id>'

If the request is successful, the Frosmo Platform queues the recommendation for processing.

To track the generation progress, either retrieve the recommendation (optionally with the fields=status query parameter to minimize the response size) and check the status property in the returned JSON object, or check the recommendation status in the Frosmo Control Panel. For more information about the status, see Creating recommendations.

Here's an example of a recommendation configuration to pass in the PUT request:

{
  "name": "Most viewed products",
  "description": "Top 5 most viewed products (24 h)",
  "type": "most_viewed",
  "status": "new",
  "frequency": "1h",
  "hours": 24,
  "limit": 5,
  "map_ids": "product",
  "params": [
    "type"
  ],
  "options": {
    "product_attrs": [
      "type"
    ]
  }
}

Deleting recommendations

Deleting a recommendation deletes both the recommendation configuration and all associated recommendation data from the Frosmo back end. Any modification that uses the data will no longer have access to it.

To delete a recommendation for a site, send a DELETE request to /sites/<site_id>/recommendations/<recommendation_id>, where <recommendation_id> is the ID of the recommendation.

curl -X DELETE \
--header 'Authorization: Bearer <access_token>' \
'https://<graniitti_api_domain>/v0/sites/<site_id>/recommendations/<recommendation_id>'

If the request is successful, the Frosmo Platform deletes the recommendation.

Recommendation resource

This section describes the JSON object data model for a recommendation resource.

Recommendation object

The following table describes the main recommendation object.

Table: Recommendation object properties

PropertyDescriptionTypeRoleExample
id

Recommendation ID.

The Frosmo Platform sets this property. You cannot change it.

NumberRequired
"id": 1212
site_id

Site ID.

The Frosmo Platform sets this property. You cannot change it.

NumberRequired
"site_id": 2080
name

Recommendation name.

For users to easily distinguish between recommendations in the Frosmo Control Panel, it is recommended that you define a name that's unique within a site. The Frosmo Platform does not check the name for uniqueness.

StringRequired
"name": "Most viewed products"
descriptionDescription of what the recommendation is about.StringOptional
"description": "Top 10 most viewed products (24 h)"
type

Recommendation type.

The type defines the algorithm or filter that the Frosmo Platform uses to generate the recommendation data.

The possible values for the built-in types are:

  • bundle_converted
  • bundle_viewed
  • bundle_viewed_converted
  • most_converted
  • most_viewed

The default value is most_viewed.

For more information about the types, see Recommendations in the Frosmo Platform.

StringRequired
"type": "most_viewed"
status

Status of the recommendation data generation process.

The possible values are (in process order):

  • new: Recommendation generation has yet to be scheduled.
  • scheduled: Recommendation generation has been scheduled. Depending on how many other recommendations are queued for generation, it might take anywhere from a fraction of a second to several minutes before the generation starts.
  • started: Recommendation is being generated.
  • success: Recommendation was successfully generated.
  • failure: Recommendation generation failed.

The default value is new.

The Frosmo Platform sets this property. You cannot change it.

StringRequired
"status": "success"
duration

Total number of seconds it took to generate the recommendation data.

The Frosmo Platform sets this property. You cannot change it.

NumberOptional
"duration": 2
frequency

How often the recommendation data should be regenerated.

The possible values are:

  • 15m: 15 minutes
  • 1h: 1 hour
  • 1d: 1 day (24 hours)

The default value is 15m.

If you enter a value not listed above and save the configuration, the platform ignores the value and retains the existing one (default value, unless previously saved to another valid value).

The generation time is calculated based on the last time the data was generated (either automatically or manually). For example, if the frequency is 15 minutes, and the data is generated at 10:15, the data is next generated at 10:30. If the frequency was 1 day, the data would be next generated at 10:30 the following day.

It is recommended that you set frequency based on hours as follows:

HoursFrequency
Less than or equal to 24 (1 day)15m
25168 (1 – 7 days)1h
Greater than 168 (7 days)1d

If generating the recommendation data takes longer than the defined frequency, increase the frequency, as there's no point in having a frequency that's shorter than the actual generation time. You can check the time after the recommendation data has been generated at least once.

StringRequired
"frequency": "15m"
hours

Number of past hours of source data on which to base the recommendation.

For example, if you set this to 10, the recommendation will always be generated based on the last 10 hours of data collected from the site.

The maximum value is 336 (14 days).

The default value is 1.

NumberRequired
"hours": 24
limit

Maximum number of items, such as products, to include in the recommendation.

The default value is 100.

The value must be between 1 and 120.

NumberRequired
"limit": 10
map_ids

Logic for mapping the IDs of the recommended items to source data in the Frosmo back end. This determines what data the recommendation actually returns for each item.

The possible values are:

  • product: Map each item ID to a product ID. Return the full product data for each ID.
  • Empty string: Do not map the item IDs to anything. Return the raw recommendation data (product ID, recommendation weight, and optional custom data) for each item. For more information, see Data point.

The default value is product.

StringOptional

Map each item ID to a product ID:

"map_ids": "product"

Do not map the item IDs:

"map_ids": ""
params

Names of the request parameters for filtering the recommendation data. If you also want to filter the recommendation data by segment, define a parameter for the segment ID as the last string in the array.

The parameters must be included in each Recommendations API request that retrieves this recommendation data.

You can filter recommendation data by any field or segment. How exactly you want to filter the data, and therefore what parameters to define for the request, is up to you. You can also freely name the parameters.

If you're using Recommendations API v2, you must define at least one parameter, as the API expects a request to include one or more parameters for filtering the recommendation data.

If you're using Recommendations API v1, do not use "name" as a parameter name, as the name property is reserved for defining the recommendation name in the API request.

The parameters together define a subset of recommended items whose data to return. In the request, you must define a value for each parameter. If you do not want to filter the returned data by a specific parameter, set its value to an empty string.

For example, if you're creating a product recommendation and want to return the recommendation data by product type, define a parameter name for type, such as type.

If the recommendation type is bundle_converted, bundle_viewed, or bundle_viewed_converted, define only the parameter for providing the ID of the product for which to generate the recommendation. Do not define any other parameters.

For product recommendations, the options.product_attrs array defines how the parameters map to fields in the product data from which the recommendation is generated.

The default value is ["type"].

Array of stringsOptional

Filter items by type:

"params": [
  "type"
]

Filter items by name, type, and area:

"params": [
  "name",
  "type",
  "area"
]

Filter items by type and visitor segment:

"params": [
  "type",
  "segment"
]
options

Configuration options specific to recommendation type.

The default value is {}.

For more information, see Options object.

ObjectRequiredFor examples, see Options object.
model

Latest generated recommendation data and configuration details related to generating the data.

For more information, see Model object.

The value of this property describes an object, but the Graniitti API returns the object in stringified form.

The Frosmo Platform sets this property. You cannot change it.

String (stringified object)OptionalFor examples, see Model object.
error

Error trace of what happened when the recommendation data generation failed.

If the data generation was successful, the value is an empty string.

StringOptional
"error": ""
created_at

Date and time when the recommendation was created.

For more information about the data and time format, see Data formats.

The Frosmo Platform sets this property. You cannot change it.

StringRequired
"created_at": "2018-11-08T10:37:25+00:00"
updated_at

Date and time when the recommendation was last updated.

For more information about the data and time format, see Data formats.

The Frosmo Platform sets this property. You cannot change it.

StringRequired
"updated_at": "2018-11-20T12:07:08+00:00"

Options object

The following table describes the options child object of the main recommendation object. options contains the configuration options specific to recommendation type. If you have no options to define, set the value of options to an empty object ({}).

Table: Options object properties

PropertyDescriptionTypeRoleExample
basket_size

This property only applies when the recommendation type is bundle_converted, bundle_viewed, or bundle_viewed_converted.

Maximum number of viewed or purchased products to consider per visitor, starting with the first view or purchase within the time frame over which the recommendation is generated.

For example, if you set this to 10, and a visitor purchases 20 products within the time frame over which the recommendation is generated, the generation will only consider and potentially return the first 10 purchases when determining which products were purchased together.

The default value is 70.

NumberOptional
"basket_size": 20
category_delimiter

This property only applies when the recommendation type is most_converted or most_viewed.

Delimiter string by which to split the product type. If the string is empty, the product type is not split.

The default value is an empty string.

StringOptional
"category_delimiter": ""
product_attrs

This property only applies when the recommendation type is most_converted or most_viewed.

Product data fields for filtering the generated recommendation data.

Define the field names that map to the request parameters defined in the params array. The mapping is done in order, so make sure you provide the field names in the same order as the matching parameter names in the params array.

To find out the available product data fields, use the Product API.

The field names are based on the product object structure of a Product API response. Use the appropriate JavaScript accessor notation to reference nested object properties.

To reference a direct property of the product object, simply use its name (propertyName).

To reference a property of the attributes object, itself a direct property of the product object, use either dot notation (attributes.propertyName) or bracket notation (attributes['propertyName']).

To reference within an array or object property of the attributes object, use dot or bracket notation (attributes.propertyName.nestedName) or index notation (attributes.propertyName[i]) as appropriate, depending on the nesting structure leading up to the target element or property.

The product_attrs array tells the Frosmo Platform how to map the parameters in the Recommendations API request to product data fields.

For example, if you've defined a request parameter for product type, define type as the product data field name to which the parameter maps.

If you define no request parameters, meaning you do not want to filter the recommendation data, leave this array empty.

The default value is [].

Array of stringsOptional

Map a lone request parameter to product type:

"product_attrs": [
  "type"
]

Map the first request parameter to product name, the second request parameter to product type, and the third request parameter to product area:

"product_attrs": [
  "name",
  "type",
  "attributes.area"
]

No request parameters to map to product data:

"product_attrs": []
segments

This property only applies when the recommendation type is most_converted or most_viewed.

IDs of the segments by which you want to allow the returned recommendation data to be filtered. You can use one of the IDs in a Recommendations API request (as the value of the segment parameter) to retrieve the recommendations only for that segment. If you do not define the ID of a given segment, you cannot filter the results by that segment.

If you define one or more segment IDs, the params array must include one parameter for the segment ID as its last item.

For example, if you define segment as the last item in the params array, and you define the segment IDs 123 and 456 in this property, you can use the parameter "segment": "123" or "segment": "456" in the Recommendations API request to filter the returned data by the corresponding segment.

The segment IDs are numerical strings, such as "123", without the sgmt_ prefix, which you see as part of segment IDs in the Frosmo Control Panel.

The default value is [].

Array of stringsOptional

Allow filtering by two segments:

"segments": [
  "123",
  "456"
]

Do not allow filtering by segment:

"segments": []

Model object

The following table describes the model child object of the main recommendation object. model contains the latest generated recommendation data and configuration details related to generating the data.

The Graniitti API returns this object in stringified form.

The Frosmo Platform sets and updates this object. You cannot directly change it.

Table: Model object properties

PropertyDescriptionTypeRoleExample
map_idsCopy of the map_ids property of the main recommendation object.StringRequiredFor examples, see Recommendation object.
paramsCopy of the params property of the main recommendation object.Array of stringsRequiredFor examples, see Recommendation object.
version

Version of the data schema used by the Frosmo Platform to generate and structure the recommendation data.

Currently, the only possible value is 0.

Number (integer)Optional
"version": 0
data

Latest generated recommendation data.

For more information, see Data object.

ObjectRequiredFor examples, see Data object.

Data object

The data object contains the latest generated recommendation data. The data is organized as follows:

  • data consists of one or more data point sets.
  • Data point set is an array of objects (data points) identified by a unique key. For more information, see Data point set.
  • Data point is an object that represents a single recommended item. For more information, see Data point.

The following example shows the high-level structure of recommendation data.

"data": {
  "<data point set 1 key>": [
    {<data point 1>},
    {<data point 2>},
    ...
    {<data point n>}
  ],
  "<data point set 2 key>": [
    {<data point 1>},
    {<data point 2>},
    ...
    {<data point n>}
  ],
  ...
  "<data point set n key>": [
    {<data point 1>},
    {<data point 2>},
    ...
    {<data point n>}
  ]
}

Data point set

A data point set (array of objects) is a prefiltered subset of all recommended items (data points). The data points in a data point set are sorted in descending order by weight (see Data point). The purpose of data point sets is to provide fast and ready access to specific subsets of recommendation results. Since the results are prefiltered, the platform doesn't need to separately perform filtering for each request. Instead, the platform simply returns the subset or subsets matching the request.

Each data point set is based on a unique combination of available values for the request parameters defined in the params property of the main recommendation object. A data point set key is built by concatenating the specific parameter values for that set, in the order in which the parameters are defined in the params property.

For recommendation types that allow empty parameter values:

  • The first data point set has an empty string as its key ("") and contains all the recommended items
  • The data point sets also include combinations for cases where one or more parameters are set to empty values. The keys for these sets are built with the non-empty values.

For example, if a most_converted product recommendation defines the request parameters type and color, the recommendation data will contain data point sets for every combination of type and color values found among the product data stored in the Frosmo database, including data point sets where the other parameter value is empty. The following example shows the data point sets when the database contains products of type Shoe in colors Black and White and products of type Suit in colors Black, Purple, and Red.

"data": {
  "": [<data points for all recommended products, no filtering by any parameter>],
  "Shoe": [<data points for recommended shoes, no filtering by color>],
  "ShoeBlack": [<data points for recommended black shoes>],
  "ShoeWhite": [<data points for recommended white shoes>],
  "Suit": [<data points for recommended suits, no filtering by color>],
  "SuitBlack": [<data points for recommended black suits>],
  "SuitPurple": [<data points for recommended purple suits>],
  "SuitRed": [<data points for recommended red suits>],
  "Black": [<data points for recommended black products, no filtering by type>],
  "Purple": [<data points for recommended purple products, no filtering by type>],
  "Red": [<data points for recommended red products, no filtering by type>],
  "White": [<data points for recommended white products, no filtering by type>]
}

When a client calls the Recommendations API for the above recommendation, the platform constructs the returned data from the data points matching the parameters defined in the client's request. For example, in the request:

  • If "type":"" and "color":"", the platform uses the "" data point set.
  • If "type":"" and "color":"Purple", the platform uses the Purple data point set.
  • If "type":"Shoe" and "color":"", the platform uses the Shoe data point set.
  • If "type":"Suit" and "color":"Red", the platform uses the SuitRed data point set.
  • If "type":"Shoe" and "color":"Orange", the platform returns an empty data object, as there's no ShoeOrange data point.

The platform determines what data to return based on the value of the map_ids property of the main recommendation object:

  • product: For each data point in the selected data point sets, the platform returns the corresponding product data. The platform calls the Product API with the data point IDs, which map to product IDs.
  • Empty string: The platform returns the data points in the selected data point sets as is. For more information, see Data point.

Data point

The following table describes the data point object. A data point represents a single recommended item. Data points are grouped into data point sets in the generated recommendation data.

Table: Data point object

PropertyDescriptionTypeRoleExample
id

Data point ID.

In a product recommendation, this is the ID of the product that the data point represents.

If the recommendation type is bundle_viewed or most_viewed, the ID maps to a product ID in product data. In the data layer, this ID corresponds to the frosmoProductId property of a product object.

If the recommendation type is bundle_converted, bundle_viewed_converted, or most_converted, the ID maps to either a product ID in transaction data or a conversion ID in conversion data, depending on how the Frosmo Platform tracks conversions for products. In the data layer, this ID corresponds to either the id property of a transaction product object of a transaction object or the conversionId property of a conversion object, respectively.

If you track conversions for products using the data layer conversion object, the conversion ID must match a valid product ID. Otherwise, the data point ID does not map to a tracked product, and the Frosmo Platform cannot return product data for that ID.

StringRequired
"id": "123"
weight

Measure of recommendation primacy or strength. The higher the weight, the more popular or recommendable the item. The value range and what the value means depends on the recommendation algorithm. For example, the value could be a count (positive integer) or a percentage (decimal value between 0 and 1).

Data points are organized in descending order by weight.

In a product recommendation, this is the total number of views or purchases for the product that the data point represents.

NumberRequired
"weight": 1056
value

Custom data for the data point in the JSON format.

This property is not used for product recommendations, since the relevant product data is retrieved through the Product API.

ObjectOptional
"value": {
  "images":[
    "/products/123/images/1.png",
    "/products/123/images/2.png",
    "/products/123/images/3.png",
    "/products/123/images/4.png",
    "/products/123/images/5.png"
  ]
}

Example: Recommendation object

The following is a full example of a recommendation object returned by the Graniitti API. You can view the model data in unstringified form below the example.

{
  "id": 16,
  "site_id": 2094,
  "name": "Products purchased together",
  "description": "Top 5 products purchased together in the past 24 hours",
  "type": "bundle_converted",
  "status": "success",
  "duration": 3,
  "frequency": "15m",
  "hours": 24,
  "limit": 5,
  "params": [
    "productId"
  ],
  "map_ids": "product",
  "options": {
    "basket_size": 20
  },
  "model": "{\"params\":[\"productId\"],\"map_ids\":\"product\",\"version\":0,\"data\":{\"1\":[{\"id\":\"4\",\"weight\":2},{\"id\":\"3\",\"weight\":2},{\"id\":\"2\",\"weight\":1},{\"id\":\"7\",\"weight\":1},{\"id\":\"5\",\"weight\":1}],\"2\":[{\"id\":\"4\",\"weight\":2},{\"id\":\"3\",\"weight\":2},{\"id\":\"7\",\"weight\":1},{\"id\":\"5\",\"weight\":1},{\"id\":\"1\",\"weight\":1}],\"3\":[{\"id\":\"4\",\"weight\":3},{\"id\":\"2\",\"weight\":2},{\"id\":\"1\",\"weight\":2},{\"id\":\"7\",\"weight\":1},{\"id\":\"5\",\"weight\":1}],\"4\":[{\"id\":\"3\",\"weight\":3},{\"id\":\"2\",\"weight\":2},{\"id\":\"1\",\"weight\":2},{\"id\":\"7\",\"weight\":1},{\"id\":\"5\",\"weight\":1}],\"5\":[{\"id\":\"7\",\"weight\":2},{\"id\":\"6\",\"weight\":2},{\"id\":\"2\",\"weight\":1},{\"id\":\"4\",\"weight\":1},{\"id\":\"3\",\"weight\":1}]}}",
  "error": "",
  "created_at": "2018-12-10T15:00:55+00:00",
  "updated_at": "2019-01-25T09:10:47+00:00"
}
Example model data unstringified and prettified as a JSON object
{
  "params": [
    "productId"
  ],
  "map_ids": "product",
  "version": 0,
  "data": {
    "1": [
      {
        "id": "4",
        "weight": 2
      },
      {
        "id": "3",
        "weight": 2
      },
      {
        "id": "2",
        "weight": 1
      },
      {
        "id": "7",
        "weight": 1
      },
      {
        "id": "5",
        "weight": 1
      }
    ],
    "2": [
      {
        "id": "4",
        "weight": 2
      },
      {
        "id": "3",
        "weight": 2
      },
      {
        "id": "7",
        "weight": 1
      },
      {
        "id": "5",
        "weight": 1
      },
      {
        "id": "1",
        "weight": 1
      }
    ],
    "3": [
      {
        "id": "4",
        "weight": 3
      },
      {
        "id": "2",
        "weight": 2
      },
      {
        "id": "1",
        "weight": 2
      },
      {
        "id": "7",
        "weight": 1
      },
      {
        "id": "5",
        "weight": 1
      }
    ],
    "4": [
      {
        "id": "3",
        "weight": 3
      },
      {
        "id": "2",
        "weight": 2
      },
      {
        "id": "1",
        "weight": 2
      },
      {
        "id": "7",
        "weight": 1
      },
      {
        "id": "5",
        "weight": 1
      }
    ],
    "5": [
      {
        "id": "7",
        "weight": 2
      },
      {
        "id": "6",
        "weight": 2
      },
      {
        "id": "2",
        "weight": 1
      },
      {
        "id": "4",
        "weight": 1
      },
      {
        "id": "3",
        "weight": 1
      }
    ]
  }
}
  • No labels