--- title: Capture purchase events (Event Protocol) slug: o1n93059 canonical_url: https://docs.coveo.com/en/o1n93059/ collection: coveo-for-commerce source_format: adoc --- # Capture purchase events (Event Protocol) This article explains how to capture purchase events and send the information to [Coveo Analytics](https://docs.coveo.com/en/182/). ## When to capture a purchase event This event comes after the [capture cart](https://docs.coveo.com/en/n39h1594/) 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](https://sports.barca.group/), which contains product specific information. ![Shopping cart summary showing kayak purchase and complementary products | Coveo](https://docs.coveo.com/en/assets/images/coveo-for-commerce/images/3188_images/barca-purchase-event-checkout.gif) ## How to capture a purchase event Capturing the event depends on your implementation approach. If you're using Headless to build your commerce interfaces, you can capture the event using the Headless controllers. Otherwise, you can use the Relay library to capture the event. [%header,cols="3*"] |=== 2+|Implementation approach |Tracking approach .3+|[Headless library](https://docs.coveo.com/en/lcdf0493/) |[Client-side rendering](https://docs.coveo.com/en/o6r70022/) |[Managing the cart (CSR)](https://docs.coveo.com/en/o7v87042/) |[Server-side rendering](https://docs.coveo.com/en/obif0156/) |[Managing the cart (SSR)](https://docs.coveo.com/en/oc685394/) |[Shopify Hydrogen](https://docs.coveo.com/en/p1oe2163/) |[Managing the cart (Shopify Hydrogen)](https://docs.coveo.com/en/p1oe0470/) 2+|[Atomic](https://docs.coveo.com/en/lcdf0264/) |[Managing the cart using Headless (CSR)](https://docs.coveo.com/en/o7v87042/) 2+|[Relay library](https://docs.coveo.com/en/o9je0322/) |[Capture a purchase event using Relay](#capture-a-purchase-event-using-relay) |=== ### Capture a purchase event using Relay If you're using [Relay library](https://docs.coveo.com/en/o1n90002/) directly, when a user purchases one or more products, you can run the following code to capture the event. ```js const relay = createRelay({...}); <1> relay.emit('ec.purchase', { <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](https://docs.coveo.com/en/o1n90002/). <2> 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. <3> Include the necessary event data, as outlined in the [reference documentation](https://docs.coveo.com/en/n9da0377#ec.purchase), including an array of products that were purchased. For a detailed description of the functions offered by the library, see the [Relay documentation](https://docs.coveo.com/en/relay/latest/usage/). > **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 [Data validation](https://docs.coveo.com/en/o2170365#validating-single-events).