Skip to content

Product recommendations with our .NET client

Relewise has a .NET SDK which, among other things, will make implementing recommendations very easy.
It is available via NuGet.

Start by making an IRecommender-instance.

csharp
IRecommender recommender = new Recommender(
  new Guid("00000000-0000-0000-0000-000000000001"), 
  "your api key");

Before going any further, make sure you have read the following:

  • Read about how to authenticate against our API here.
  • Read about handling different types of users here.

Also remember to specify your Server URL in your request.

Product Details Page (PDP)

On the Product Details Page (PDP), the most commonly used recommendations are PurchasedWithProduct and ProductsViewedAfterViewingProduct. Here are examples on how to implement those requests. We recommend that you batch the request into a single request. Read more about why and how here.

PurchasedWithProduct

csharp
var id = new ProductAndVariantId("productId", "variantId");

var purchasedWithProductRequest = new PurchasedWithProductRequest(
  new Language("da"),
  new Currency("DKK"),
  "Product details page",
  GetUser(),
  id);

purchasedWithProductRequest.Settings.SelectedProductProperties = new SelectedProductPropertiesSettings
{
  DisplayName = true
};

purchasedWithProductRequest.Settings.SelectedVariantProperties = new SelectedVariantPropertiesSettings
{
  DisplayName = true
};

ProductRecommendationResponse response = await recommender.RecommendAsync(
  purchasedWithProductRequest, 
  cancellationToken);

ProductsViewedAfterViewingProduct

csharp
var id = new ProductAndVariantId("productId", "variantId");

var productsViewedAfterViewingProductRequest = new ProductsViewedAfterViewingProductRequest(
  new Language("da"),
  new Currency("DKK"),
  "Product details page",
  GetUser(),
  id);
  
productsViewedAfterViewingProductRequest.Settings.SelectedProductProperties = new SelectedProductPropertiesSettings
{
  DisplayName = true
};

productsViewedAfterViewingProductRequest.Settings.SelectedVariantProperties = new SelectedVariantPropertiesSettings
{
  DisplayName = true
};

ProductRecommendationResponse response = await recommender.RecommendAsync(
  productsViewedAfterViewingProductRequest, 
  cancellationToken);

By using SelectedProductPropertiesSettings or SelectedVariantPropertiesSettings it is posible to select the specific properties, from Relewise, that you need for rendering a product tile, list etc.,. That way we can store fields that make the filtering or merchandising better, but maybe is not needed when rendering the actual product. So we can reduce the HTTP responses by only returning a minimum set of the information that is needed.

Power Step

On the Power Step the most used recommendation is PurchasedWithProduct.

csharp
var id = new ProductAndVariantId("productId", "variantId");

var purchasedWithProductRequest = new PurchasedWithProductRequest(
  new Language("da"),
  new Currency("DKK"),
  "Product details page",
  GetUser(),
  id);

purchasedWithProductRequest.Settings.SelectedProductProperties = new SelectedProductPropertiesSettings
{
  DisplayName = true
};

purchasedWithProductRequest.Settings.SelectedVariantProperties = new SelectedVariantPropertiesSettings
{
  DisplayName = true
};

ProductRecommendationResponse response = await recommender.RecommendAsync(
  purchasedWithProductRequest, 
  cancellationToken);

Cart / Basket Page

On the Basket Page use the PurchasedWithCart-recommendation, if the user has accepted cookies:

csharp
var request = new PurchasedWithCurrentCartRequest(
  new Language("da"),
  new Currency("DKK"),
  "Basket page",
  GetUser());

request.Settings.AllowFillIfNecessaryToReachNumberOfRecommendations = true;

ProductRecommendationResponse response = await recommender.RecommendAsync(request, cancellationToken);

If the user has declined cookies and thus is anonymous, then instead you should use the PurchasedWithMultipleProducts-recommendation.

csharp
var id1 = new ProductAndVariantId("123456789", "variantId1");
var id2 = new ProductAndVariantId("987654321", "variantId2");

var request = new PurchasedWithMultipleProductsRequest(
  new Language("da"),
  new Currency("DKK"),
  "Basket page",
  GetUser(),
  new[] { id1, id2 });

ProductRecommendationResponse response = await recommender.RecommendAsync(request, cancellationToken);

Since the user is anonymous, Relewise does not know anything about the user, so you need to provide the content of the cart/basket to Relewise in order to get recommendations for the anonymous user's basket.

Batching recommendation requests.

It's best practice to batch all recommendation requests on a page into a single request against the Relewise API. This has the benefit of ensuring unique products between different recommendations - and also minimizing network traffic.

csharp
var collection = new ProductRecommendationRequestCollection(
  requireDistinctProductsAcrossResults: true, 
  purchasedWithProductRequest,
  productsViewedAfterViewingProductRequest);
  
recommender.RecommendAsync(collection, cancellationToken);

The example here uses product recommendations, but the same can be done for product categories, content, and content categories.

Don't know us? Don't worry - you can find more information about us, by visiting our main page www.relewise.com