Skip to main content
RichRelevance

Android: Cart Page

Overview

The Cart screen typically displays the items in a shopper’s cart or bag. It is a distinct experience separate from Add To Cart, which may be a modal window or an interstitial popover.

Details

Place your code in your fragment's initialization logic, such as onCreate.

Return Personalization

Use this code when you are requesting and serving personalized product recommendations.

Replace the following placeholders in the code:

  • <placement name> with your Placement name. You can find a list of placements in your Dashboard.
  • <product ID 1>, <product ID 2>, <product ID 3>with Product IDs currently in the cart.

You do not need to pass Product IDs if the cart is empty. Add IDs for all the products in the cart.

You only need to specify one ID per product. For example, if 3 quantities of Product ID P12389 are present in the cart, you only need to pass "P12389" once.

Placement placement = new Placement(Placement.PlacementType.CART, "<placement name>");

RichRelevance.buildRecommendationsForPlacements(placement)
  .setProductIds("<product ID 1>", "<product ID 2>", "<product ID 3>")
  .setCallback(new Callback<PlacementResponseInfo>() {
      @Override
      public void onResult(PlacementResponseInfo result) {          
          int index = 0;
          for(PlacementResponse placement : result.getPlacements()) {
              for(final RecommendedProduct recommendedProduct : placement.getRecommendedProducts()) {
                  // Add your view init logic. The actual implementation will depend on your code.
                  // In this example we will assume items are rendered as TableRows in a TableView.
                  TableRow row = new TableRow(context);
                  row.setId(index);
                  row.addView(detailView);
                  index++;
                  tableView.addLayout(row);
              }
          }
      }
      @Override
      public void onError(Error error) {
          // Use this code block to handle errors
          Log.e(getClass().getSimpleName(), "Error: " + error.getMessage());
      }
  }).execute();

Calling multiple placements

If you want to call multiple placements, simply define your placements and pass them to RichRelevance.buildRecommendationsForPlacements() either as individual arguments or as a Collection<Placement>.

IMPORTANT Placements must always be of the same type per request.

// This example defines placements as individual arguments.
Placement topPlacement = new Placement(Placement.PlacementType.CART, "recs_top");
Placement middlePlacement = new Placement(Placement.PlacementType.CART, "recs_middle");

RichRelevance.buildRecommendationsForPlacements(topPlacement, middlePlacement)
// Add callback logic
.execute();
// This example defines placements as an ArrayList. It is equivalent to the example above.
ArrayList<Placement> placements = new ArrayList<>(
    Arrays.asList(
        new Placement(Placement.PlacementType.CART, "recs_top"),
        new Placement(Placement.PlacementType.CART, "recs_middle"))
    );

RichRelevance.buildRecommendationsForPlacements(placements)
// Add callback logic
.execute();

Listening Mode

When you do not render personalization in your Cart UX, you must still send a tracking request using the SDK. This is required so that Personalization Cloud can gather usage data in order to train your personalization models. In this specific case, you will create a listening placement. Listening placements are a special type of placement that can only log usage data without returning personalization data.

Replace <product ID 1>, <product ID 2>, <product ID 3>with Product IDs currently in the cart.

You do not need to pass Product IDs if the cart is empty. Add IDs for all the products in the cart.

You only need to specify one ID per product. For example, if 3 quantities of Product ID P12389 are present in the cart, you only need to pass "P12389" once.

Placement placement = new Placement(Placement.PlacementType.ADD_TO_CART);
RichRelevance.buildRecommendationsForPlacements(placement)
    .setProductIds("<product ID 1>", "<product ID 2>", "<product ID 3>")
    .execute();

Tracking Events

The SDK will automatically log a page view event when executing the builder. It will also automatically log a placement impression for each of the placements returned. For example, if you request three placements but only one is returned, the SDK will log a placement impression for this returned placement only.

You can include this logic in the controller for your personalization placement fragment, inside a click listener.

// Assuming a list of personalized product recommendations is being stored in memory
List<RecommendedProduct> products;

tableRow.setOnClickListener(new View.OnClickListener() {                   
    @Override
    public void onClick(View view) {
        RecommendedProduct product = products.get((int)view.getId());
        if (product) {
            product.trackClick();
        }
        
        // Add your logic to render your Product Detail View.
    }
});

Offline click tracking

In case of poor network connectivity,trackClick() can store click data locally and will send these events at a later stage, for example when there is enough bandwidth or when the network becomes available again. To allow this, your app need to request the relevant permission in your AndroidManifest.xml:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

If you cannot add this permission to your manifest, you can still flush click events programmatically by accessing the following method:

RichRelevance.flushClickTracking(); 

 

  • Was this article helpful?