getXgenPredictions()
is used to retrieve multiple sets of XGen’s personalized recommendation predictions. These predictions are used for rendering out personalized elements unique to each user.
This function calls multiple elements. For a simpler syntax when calling a single element, use getXGenPrediction().
If the pages functionality is being used, this function will wait for a page to be matched before the promise resolves.
Syntax
getXgenPredictions(elementIds);
getXgenPredictions(elementId1, /* …, */ elementIdN);
Parameters
elementIds
type: array
description: An array of the element IDs that will be returned from this function.
items:
elementId
type: string
description: The ID of an XGen element. Can be accessed from the XGen platform under ‘Site Personalization.’
elementId1 … elementIdN
type: string
description: Element IDs that will be returned from this function that are passed in as separate parameters
As an alternative to passing in an array of element IDs, you can pass in each element ID as its own parameter and the functionality and output will be the same.
Return Value
A promise
that resolves with an object
in which the keys are element IDs and the values are an array of objects containing product information.
Example:
.png)
Examples
Get list of elements with array syntax:
In this example, we get elements by passing in an array of element IDs. When the returned promise resolves, we console log the prod_code
(aka product ID
) of each product recommended.
// Create integration (use existing integration if you already created one)
const integration = new XG(...config);
// Get two elements from prediction API
const predictions = await integration.getXgenPredictions([
'sm_el_b04ebc323ccb4f49bedcece7652e4fba',
'sm_el_4784cbbec2794c87a4d40a87881d8ce0'
]);
// Get products from first element
const products = predictions['sm_el_b04ebc323ccb4f49bedcece7652e4fba'];
// Console log prod_code of each product recommended
products.forEach(product => console.log(product.prod_code));
Get list of elements with multiple parameter syntax:
In this example, we get elements by passing in each element ID as its own parameter. When the returned promise resolves, we console log the prod_code (aka product ID) of each product recommended.
// Create integration (use existing integration if you already created one)
const integration = new XG(...config);
// Get two elements from prediction API
const predictions = await integration.getXgenPredictions(
'sm_el_b04ebc323ccb4f49bedcece7652e4fba',
'sm_el_4784cbbec2794c87a4d40a87881d8ce0'
);
// Get products from first element
const products = predictions['sm_el_b04ebc323ccb4f49bedcece7652e4fba'];
// Console log prod_code of each product recommended
products.forEach(product => console.log(product.prod_code));