Capture cart events

This is for:

Developer
Important

The Event Protocol and Relay library are currently in early access. If you’re interested in using the Event Protocol and Relay library, reach out to your Customer Success Manager (CSM).

This article explains how to capture cart events and send the information to Coveo Usage Analytics (Coveo UA) using the Relay library.

When to capture a cart event

Anytime the content of a cart is modified by a user, the interface must log a cart event, together with the product information payload. Cart content modifications may include the addition or removal of products, or changes in quantity of existing products in the cart. Depending on your site’s design, cart events can be triggered from different pages, such as:

  • From a page which shows the current content of a visitor's shopping cart

  • From an individual {product-detail-page}

  • From a search results page

  • From a listing page

  • From a recommendations component

The following image is from the cart page view of the online sporting goods store Barca Sports, which provides visitors with the ability to modify the contents of their cart by increasing or decreasing the quantity of items. Minimally, you would want to log product information and the quantity added or removed in the product data payload. Coveo calculates quantity as the increase or decrease in value, and not the new total amount. For example, if a client changes the quantity of the item in cart from 1 to 2, the quantity to pass in the cart event is 1.

Barca cart view

Capture a cart event using Relay

When a user clicks on a product, you can run the following code to capture the click event and send the information to Coveo UA.

const relay = createRelay({...}); 1
relay.emit('ecCartAction', { 2
    action: 'add', 3
    currency: 'USD',
    quantity: 1,
    product: {
        productId: 'SP03929_00014',
        name: 'Blue Bottle',
        price: 16,
    },
});
1 Initialize a Relay instance.
2 Use the emit function to send an event with Relay, specifying the event name. In this example, the value is ecCartAction since it’s capturing a cart event.
3 Include the necessary event data, as outlined in the reference documentation. Here a single product is being added to the cart.

For a detailed description of the functions offered by the library, see the Relay documentation.

Note

When sending events with Relay, you don’t need to include the meta object mentioned in the Event Protocol Reference documentation, as it’s automatically added by the library.

Validating the cart event

It’s important to verify that your event is valid and contains the correct payload. Relay seamlessly integrates with Coveo Explorer, which can be used for event validation.

For more details on using Coveo Explorer to validate events, see Validating events.