# Purchase

The following describes how purchase tracking is done for Firework.

## How to implement the "purchase" tracking event

{% hint style="info" %}
Please ensure you send all the purchase events happening in your website to us. Please do not filter out any events on your side. This information is required to identify your Firework attributed vs Non Firework purchases. This also helps us calculate the lift in GMV and AOV with Firework.
{% endhint %}

```javascript
<script>
function onPurchase() {
    let purchasePayload = {
        "order_id": "123456789",
        "currency": "USD",
        "subtotal": "40.00",
        "total_discounts": "5.00",
        "order_value": "50.00",
        "line_items": [{
                "sku": "TSHIRT-RED-M",
                "quantity": 2,
                "price": "15.00",
                "name": "Red T-shirt (M)"
            },
            {
                "sku": "BANANA",
                "quantity": 10,
                "price": "1.00",
                "name": "Organic Banana"
            }
        ]
    };
    window._fwn.analytics.purchase(purchasePayload);
}
//Call the function when purchase happens or on the load of thank you or order confirmation page
document.addEventListener('DOMContentLoaded', function() {
    onPurchase();
});
</script>
```

#### Payload for purchase tags:

| Key              | Type   | Required?                  | Description                                                                                                                  |
| ---------------- | ------ | -------------------------- | ---------------------------------------------------------------------------------------------------------------------------- |
| order\_id        | string | required                   | The order ID. e.g. ‘12334’                                                                                                   |
| currency         | string | required                   | The currency used in this order. Has to be in ISO 4217 format.                                                               |
| subtotal         | number | required                   | The cost of the items purchased before discounts, taxes and shipping costs are added                                         |
| total\_discounts | number | optional                   | The total amount of all discounts applied to the order in the currency's subunit                                             |
| order\_value     | number | required                   | The total purchase order value is the final amount a customer pays, including all taxes, shipping, and any discounts applied |
| line\_items      | array  | required                   | See line\_items parameters table below. The tag needs to pass in either line\_items or product                               |
| product          | array  | for backward compatibility | See product parameters table below                                                                                           |

#### Payload for line\_item:

| Key           | Type   | Required? | Description                                                                                                                                          |
| ------------- | ------ | --------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- |
| sku           | string | required  | The ID that we can link back to the product or product unit. It can be the SKU, barcode, GTIN, MPN, product\_unit.external\_id, product.external\_id |
| quantity      | number | required  | The quantity bought                                                                                                                                  |
| price         | number | required  | The unit price of the line item. This includes any line-level discounts                                                                              |
| product\_name | string | optional  | Name of the product variant or product unit                                                                                                          |

#### Payload for product:

| Key                      | Type   | Required? | Description                                                                     |
| ------------------------ | ------ | --------- | ------------------------------------------------------------------------------- |
| ext\_parent\_product\_id | string | required  | Product ID from the client system, this maps to product.external\_id            |
| ext\_product\_id         | string | required  | Product Unit ID from the client system, this maps to product\_unit.external\_id |
| parent\_product\_name    | string | required  | Name of the product                                                             |
| product\_name            | string | required  | Name of the product variant or product unit                                     |
| image                    | string | optional  | <p><br></p>                                                                     |
| sku                      | string | optional  | <p><br></p>                                                                     |
| price                    | number | required  | The unit price of the line item. This includes any line-level discounts         |
| currency                 | string | required  | Currency in ISO 4217 format                                                     |
| quantity                 | number | required  | The quantity bought                                                             |
| url                      | string | required  | URL for the variant or product                                                  |

### Debugging issues with purchase tracking

To ensure that the purchase tracking is implemented correctly you can follow the steps below

1. Open browser developer tools
2. Goto "Sources" tab
3. Choose the "/thank\_you" page
4. Find the part of the DOM which contains our tracking code snippet
5. Ensure there are no errors appearing anywhere in that code

<figure><img src="https://688917408-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MLoGG8m6bokS9YTmS7m%2Fuploads%2Fgit-blob-46fa5f3b4bf808cf89fff8629e81fe55301d22d3%2Fimage__24___1_.png?alt=media" alt=""><figcaption></figcaption></figure>

To ensure that the data you are sending is accurate you may try printing the transaction data to the console. You may find all the information related to the Shopify transaction data here - <https://shopify.dev/docs/api/liquid/objects/transaction>

You will not be able to print out the entire transaction object as Shopify prevents this. However you will be able to inspect the part that you are interested in. For example something like this

```javascript
let debugTransaction = {
    payment_details: { { transaction.payment_details | json } }
}
console.log("debugTransaction: ", debugTransaction)
```

## Shopify <> Firework Purchase Tracking

With the latest Firework app in Shopify, you do not need explicitly do any steps for implementing purchase tracking. The purchase tracking code is integrated automatically when the Firework app is installed in Shopify.

{% hint style="info" %}
If you already have added the Firework purchase tracking snippet under your Order Status Page, we recommend removing that as updating to the latest Shopify app version will add the tracking code automatically.
{% endhint %}

### Verifying purchase tracking in Shopify

You could perform a test purchase and on the order confirmation page, check for the `user:purchase` event in your network tab

<figure><img src="https://688917408-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MLoGG8m6bokS9YTmS7m%2Fuploads%2Fgit-blob-61be8826258302b19e623d13b20c32b5a51b7eb1%2FScreenshot_2024-01-25_at_10_45_28%E2%80%AFAM.png?alt=media" alt=""><figcaption></figcaption></figure>
