Recommendation Widget Block

This is an Optimizely block provided in the Optimizely.Hawksearch.Connector package.

Class: RecommendationWidgetBlock

Base class: BlockData

It contains 3 properties:

  • Widget Guid → the GUID of the widget from Hawksearch Dashboard

  • Render Html → flag that, if set, Widget HTML will be returned from the Hawksearch Dashboard. If not set, recommendation items will be returned in JSON format

  • Enable Preview → flag that indicates whether preview has been enabled in the Hawksearch Dashboard for that widget

 

Recommendation Widget Client

This is a class present in Optimizely.Hawksearch.Connector.Recommendations namespace that uses the Recommendation Widget Block directly to make requests to the Hawksearch Recommendation API.

Class: RecommendationWidgetClient

It provides 2 methods, each one of them using as a parameter the RecommendationWidgetBlock data type together with other types (required and optional):

public RecommendationResponse GetWidgetItems(RecommendationWidgetBlock block, string visitId, string visitorId, string languageCode, string landingPageUrl = null, string uniqueId = null, Dictionary<string, object> contextProperties = null, Dictionary<string, object> customProperties = null, Dictionary<string, object> extendedCustomProperties = null) public async Task<RecommendationResponse> GetWidgetItemsAsync(RecommendationWidgetBlock block, string visitId, string visitorId, string languageCode, string landingPageUrl = null, string uniqueId = null, Dictionary<string, object> contextProperties = null, Dictionary<string, object> customProperties = null, Dictionary<string, object> extendedCustomProperties = null)

 

Block Usage

Steps to integrate this block in your project:

  • Create a Partial View for the block Index.cshtml

  • Create a Block Controller for the block

    public class RecommendationWidgetBlockController : BlockController<RecommendationWidgetBlock>
  • Inject the RecommendationWidgetClient inside your new controller

  • Use the GetWidgetItems method of the client to retrieve the target widget (the target widget will match the Widget Guid from the block created in CMS Edit) and its contained recommendations

    //RecommendationWidgetClient _client; public override ActionResult Index(RecommendationWidgetBlock currentBlock) { //... var visitId = HawksearchCookieManager.GetOrCreateVisitIdCookie(); var visitorId = HawksearchCookieManager.GetOrCreateVisitorIdCookie(); var languageCode = "en"; var response = _client.GetWidgetItems(currentBlock, visitId, visitorId, languageCode); var widget = response.WidgetItems.FirstOrDefault(); var recommendations = widget?.RecommendationItems; //works if 'Render Html' is not set inside the block var html = widget?.Html; //works if 'Render Html' is set inside the block //... return PartialView(viewModel); }
  • Add any other code for your specific business needs inside the controller

 

After building your project, you can check the functionality by following these steps:

  • Create a “Recommendation Widget Block“ from Optimizely CMS → Edit interface

  • Fill in the necessary data

  • Place the block inside any content area on the website (that has no restrictions for this block type)

  • The block should be rendered on the frontend containing your view, style, and Hawksearch recommendations, based on your custom implementation