# Track Purchases

You can use the OnlineSales.ai pixel to track the visitors actions like a `Purchase`Tracked product purchases are used to compute the sales score, user's affinity to product, user's spend capacity and other signals which helps in measurement & optimising the campaigns.&#x20;

### Requirements

The pixel's [base code](/user-tracking-events/js-sdk/installing-the-tag.md#base-code) must already be installed on every page where you want to track conversions.

### Trigger

`purchase` event should be fired when a user completes the purchase of the product. i.e. after the payment is successful and the order id is generated.

### JS API

All events are tracked by calling one of the library's function with a JSON object as it's parameters.

Following is a function call to track when a visitor views cart on your website.

```javascript
// with minimum required parameters
_osSaleComplete({
    "products": [{
        "skuId": "XYZ-1231-1233",
        "sellerId": "SID-9999",
        "productPrice": "899",
        "quantity": "2" // optional. Defaults to 1
    },{
        "skuId": "ABC-1231-1233",
        "sellerId": "XID-123123",
        "productPrice": "99",
    }],
    "totalAmount": "998",
    "currency": "INR",
    "orderId": "OR-94716273-S",
    "cli_ubid": "ubid-83789ssb"
}) 
```

{% hint style="success" %}
This function takes the same parameters as the `_osViewCart` The only different is ViewCart event is fired on the load of cart page and `_osCheckout` is fired on a button click (which initiates the billing & payment process) of the cart page.
{% endhint %}

List of all the standard events can be found [here](/user-tracking-events/js-sdk/references.md#standard-events).

### Object Properties of *Products* key

You can include the following predefined object properties with any events that support them. Format your parameter object data using JSON.

| **Property Key** | **Parameter Description**                                                                                                                                   |
| ---------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- |
| category         | Category of the menu item. This should be the same as what is given in the menu item catalog. While this is optional, this parameter is highly recommended. |
| productPrice     | Price of single menu item without the currency symbol                                                                                                       |
| currency         | Currency of the menu item's price. Eg: "INR". This should be a 3 letter ISO standard                                                                        |
| discount         | Discounted price of single menu item                                                                                                                        |
| skuId            | The id of the menu item. This should be the same as what is given in the menu item catalog. This is a mandatory parameter.                                  |
| sellerId         | The id of the restaurant who is selling this menu item. This is mandatory in case of aggregators.                                                           |
| quantity         | Number of product quantity added to the cart. This is optional and defaults to 1                                                                            |
| totalAmount      | Total cart amount. Sum of all the menu item's prices. This may include taxes / discounts in case of saleComplete event.                                     |
| orderId          | The order id which is generated and shown to the users on the website.                                                                                      |
| paymentMethod    | The payment method used to complete the transaction                                                                                                         |
| cli\_ubid        | Client generated user id. If set, this takes precedence over ubid passed in the cookie. Once generated for a user, it should be same for all the API calls. |

Complete list of object properties can be found [here](/user-tracking-events/js-sdk/references.md).

Example call of function with all the product properties

```javascript
// with all parameters
_osSaleComplete({
    "products": [{
        "skuId": "XYZ-1231-1233",
        "sellerId": "SID-9999",
        "category": "Pizza > Veg Pizza",
        "productPrice": "999", // price of single product
        "currency": "INR",
        "discount": "899", // discounted price of single product
        "quantity": "2"
    },{
        "skuId": "ABC-1231-1233",
        "sellerId": "XID-123123",
        "category": "Pizza > Veg Pizza",
        "productPrice": "999", // price of single product
        "currency": "INR",
        "discount": "899", // discounted price of single product
        "quantity": "2"
    }],
    "totalAmount": "3596",
    "currency": "INR",
    "orderId": "OR-94716273-S",
    "paymentMethod": "CreditCard",
    "cli_ubid": "ubid-83789ssb"
})
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://foodcommerce-api-help.onlinesales.ai/user-tracking-events/js-sdk/track-purchases.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
