Create an Omnichannel Recommendations Action

Omnichannel recommendations is an integrated recommendations experience that is consistent across all platforms that you connect to Monetate. Whether a customer is viewing your storefront through a mobile app or your site, Omnichannel recommendations ensure a consistent experience.

You can set up a handler for an Omnichannel recommendations action by using two methods. The addEvents method defines the events that can trigger the action. The getActionsData method is then used as the trigger and requests the decision based on the defined events. getActionsData then returns a JSON object containing recommendations data that you can then handle in code.

Prerequisites

You must first create an Omnichannel experience within Monetate for the methods to reference. Refer to Configure an Omnichannel Recommendations Action for instructions.

Make note of the WHO settings, as these correspond to events your code listens for. The example experience in this article uses the following WHO settings:

  • IP address is 1.0.0.2
  • Screen height is at least 500 pixels and screen width is at least 300 pixels

The example code in this article fulfills these conditions and will trigger the Omnichannel experience.

addEvent

This method records a local event in the defined context.

addEvent(context: <ContextEnum>, event: <MEvent?>)

Parameters:

  • context is name of the event. (Required)
  • event is the event data. (Required)

You can use this method multiple times to add all the necessary events for an experience you might want to trigger. The example code uses multiple method calls to fulfill the experience requirements:

Code Example

Personalization.shared.addEvent(context: .ScreenSize, event: ScreenSize(height: 1000, width: 400))

Personalization.shared.addEvent(context: .IpAddress, event: IPAddress(ipAddress: "192.168.1.52")) 

Personalization.shared.addEvent(context: .PageView, event: PageView(pageType: "PDP", path: "n/a", url: "n/a", categories: [], breadcrumbs: [])) 

getActionsData

This method sends the defined events to Monetate to trigger an experience. If the events fulfill the WHO settings of an experience, then that experience is triggered. A JSON object containing the experience response is then returned.

getActionsData(requestId: <String>, arrActionTypes: <[ActionTypeEnum]>) 

Parameters:

  • requestID is the request ID for the API.
  • actionType is the type of action you want to request. You can specify multiple actions in an array to handle. (Required)

Code Example

Personalization.shared.getActionsData(
  requestId: "123456",
  arrActionTypes: [.OmniChannelRecommendation]
).on { res in
  if res.status == 200 {
    self.handleRecommendations(res: res)
  } else {
  }
}

Full Code Example

Complete code example blocks are listed below.

// Add Context / Events
//-------------------------------------------------------------------------
Personalization.shared.addEvent(context: .ScreenSize, event: ScreenSize(height: 1000, width: 400))

Personalization.shared.addEvent(context: .IpAddress, event: IPAddress(ipAddress: "192.168.1.52"))

Personalization.shared.addEvent(
  context: .PageView,
  event: PageView(pageType: "PDP", path: "n/a", url: "n/a", categories: [], breadcrumbs: []))

// Get Actions
// ----------------------------------------------------
Personalization.shared.getActionsData(
  requestId: "123456",
  arrActionTypes: [.OmniChannelRecommendation]
).on { res in
  if res.status == 200 {
    self.handleRecommendations(res: res)
  } else {
  }
}

private func handleRecommendations(res: APIResponse) {
  let data = JSON(res.data as Any)
  for item in data["data"]["responses"].arrayValue {
    if item["requestId"].string == res.requestId {
      for oneAction in item["actions"].arrayValue {
        if oneAction["component"].string == "iOS_InApp_Recs" {
          recProductArr.removeAll()
          self.recLabel.isHidden = false
          self.recLineSeperatorView.isHidden = false
          self.collectionView.isHidden = false

          for productDict in oneAction["items"].arrayValue {
            for cat in CategoryGeneratorforProducts.categories {
              for prdct in cat.products {
                if prdct.pid == productDict["id"].rawValue as! String {
                  prdct.recToken = productDict["recToken"].rawValue as? String
                  prdct.recSetId = productDict["recSetId"].rawValue as? Double
                  prdct.affinity = productDict["_affinity"].rawValue as? Double
                  prdct.rawAffinity = productDict["_rawAffinity"].rawValue as? Double
                  prdct.slotIndex = productDict["slotIndex"].rawValue as? Int
                  recProductArr.append(prdct)
                }
              }
            }
          }
          self.collectionView.reloadData()
          DispatchQueue.main.asyncAfter(
            deadline: .now() + 2.0,
            execute: {
              self.collectionView.reloadData()
            })
          return
        }
      }
    }
  }
}