Capture purchase events
Capture purchase events
This is for:
Developer
The Event Protocol and Coveo Relay are currently in open beta. 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).
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.
How to capture a purchase event
Capturing a purchase event depends on your implementation approach.
-
If you’re using Headless, capture the event using the Headless controllers.
-
If you’re not using Headless, use the Relay library directly to capture the event.
Capture a purchase event using Headless
If you’re using the Headless library, you can capture a purchase event by using the Cart
controller.
For more details, see Managing the cart.
Capture a purchase event using Relay
If you’re using Relay library directly, when a user purchases one or more products, you can run the following code to capture the event.
const relay = createRelay({...});
relay.emit('ec.purchase', {
currency: 'USD',
"transaction": {
"id": "TRX_1234",
"revenue": 781.25
},
"products": [
{
"product": {
"productId": "XY-1234",
"name": "Bluewater Canoe",
"price": 540
},
"quantity": 1
},
],
});
Initialize a Relay instance. | |
Use the emit function to send an event with Relay, specifying the event name.
In this example, the value is ec.purchase since it’s sending a purchase event. |
|
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 |
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.