Capture purchase 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 purchase events and send the information to Coveo Usage Analytics (Coveo UA) using the Relay library.

When to capture a purchase event

This event comes after the capture cart event and is sent when the user completes a purchase. The event can contain multiple products. For example, if two products were purchased at once, both would be grouped in the same purchase event.

The following image shows the action taken by a visitor to checkout (or purchase) the items in their cart from the online sporting goods store Barca Sports, which contains product specific information.

Barca purchase event

Capture a purchase event using Relay

When a user purchases one or more products, you can run the following code to capture the event.

const relay = createRelay({...}); 1
relay.emit('ecPurchase', { 2
    currency: 'USD', 3
    "transaction": {
        "id": "TRX_1234",
        "revenue": 781.25
    },
    "products": [
        {
            "product": {
            "productId": "XY-1234",
            "name": "Bluewater Canoe",
            "price": 540
            },
            "quantity": 1
        },
    ],
});
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 ecPurchase since it’s sending a purchase event.
3 Include the necessary event data, as outlined in the reference documentation, including an array of products that were purchased.

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 purchase 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.