# Cart viewed

Learn how to track when a user views a shopping cart.

This event tracks when a user views their shopping cart on an online store.

Cart viewed also automatically records a [Cart abandoned](/reference/event/types/ecommerce/cart-abandoned) event when the session is closed, and a [Product abandoned](/reference/event/types/ecommerce/product-abandoned) event for each product in the cart.

> **When should I track this event?**
>
> Track this event whenever a user views the shopping cart page or summary.

## Implementation

Here is an example of how to track this event:

**JavaScript**

```javascript
import croct from '@croct/plug';

croct.track('cartViewed', {
  cart: {
    currency: 'USD', 
    total: 899.99, 
    items: [
      {
        index: 0, 
        product: {
          productId: '12345', 
          name: 'Black iPhone 12', 
          displayPrice: 899.99
        }, 
        quantity: 1, 
        total: 899.99
      }
    ]
  }
});
```

**HTML**

```html
<!DOCTYPE html>
<html>
<head>
  <title>My awesome application</title>
  <script src="https://cdn.croct.io/js/v1/lib/plug.js"></script>
  <script>croct.plug({appId: 'APPLICATION_ID'});</script>
</head>
<body>
  <script>
    const track = () => croct.track('cartViewed', {
        cart: {
          currency: 'USD', 
          total: 899.99, 
          items: [
            {
              index: 0, 
              product: {
                productId: '12345', 
                name: 'Black iPhone 12', 
                displayPrice: 899.99
              }, 
              quantity: 1, 
              total: 899.99
            }
          ]
        }
      });
  </script>
  <button onclick="track()">Trigger</button>
</body>
</html>
```

**React — JavaScript**

```jsx
import {useCroct} from '@croct/plug-react';

export function Example() {
const croct = useCroct();

const track = () => croct.track('cartViewed', {
    cart: {
      currency: 'USD', 
      total: 899.99, 
      items: [
        {
          index: 0, 
          product: {
            productId: '12345', 
            name: 'Black iPhone 12', 
            displayPrice: 899.99
          }, 
          quantity: 1, 
          total: 899.99
        }
      ]
    }
  });

return (<button onClick={track}>Trigger</button>);
}
```

**React — TypeScript**

```typescript
import {useCroct} from '@croct/plug-react';
import type {ReactElement} from 'react';

export function Example(): ReactElement {
const croct = useCroct();

const track = () => croct.track('cartViewed', {
    cart: {
      currency: 'USD', 
      total: 899.99, 
      items: [
        {
          index: 0, 
          product: {
            productId: '12345', 
            name: 'Black iPhone 12', 
            displayPrice: 899.99
          }, 
          quantity: 1, 
          total: 899.99
        }
      ]
    }
  });

return (<button onClick={track}>Trigger</button>);
}
```

**Next — JavaScript**

```jsx
import {useCroct} from '@croct/plug-next';

export function Example() {
const croct = useCroct();

const track = () => croct.track('cartViewed', {
    cart: {
      currency: 'USD', 
      total: 899.99, 
      items: [
        {
          index: 0, 
          product: {
            productId: '12345', 
            name: 'Black iPhone 12', 
            displayPrice: 899.99
          }, 
          quantity: 1, 
          total: 899.99
        }
      ]
    }
  });

return (<button onClick={track}>Trigger</button>);
}
```

**Next — TypeScript**

```tsx
import {useCroct} from '@croct/plug-next';
import type {ReactElement} from 'react';

export function Example(): ReactElement {
const croct = useCroct();

const track = () => croct.track('cartViewed', {
    cart: {
      currency: 'USD', 
      total: 899.99, 
      items: [
        {
          index: 0, 
          product: {
            productId: '12345', 
            name: 'Black iPhone 12', 
            displayPrice: 899.99
          }, 
          quantity: 1, 
          total: 899.99
        }
      ]
    }
  });

return (<button onClick={track}>Trigger</button>);
}
```

**Vue — JavaScript**

```vue
<script setup>
import {useCroct} from '@croct/plug-vue'

const croct = useCroct()

const track = () => croct.track('cartViewed', {
    cart: {
      currency: 'USD', 
      total: 899.99, 
      items: [
        {
          index: 0, 
          product: {
            productId: '12345', 
            name: 'Black iPhone 12', 
            displayPrice: 899.99
          }, 
          quantity: 1, 
          total: 899.99
        }
      ]
    }
  })
</script>

<template>
  <button @click="track">Trigger</button>
</template>
```

**Vue — TypeScript**

```vue
<script setup lang="ts">
import {useCroct} from '@croct/plug-vue'

const croct = useCroct()

const track = (): void => croct.track('cartViewed', {
    cart: {
      currency: 'USD', 
      total: 899.99, 
      items: [
        {
          index: 0, 
          product: {
            productId: '12345', 
            name: 'Black iPhone 12', 
            displayPrice: 899.99
          }, 
          quantity: 1, 
          total: 899.99
        }
      ]
    }
  })
</script>

<template>
  <button @click="track">Trigger</button>
</template>
```

**Nuxt — JavaScript**

```vue
<script setup>
const croct = useCroct()

const track = () => croct.track('cartViewed', {
    cart: {
      currency: 'USD', 
      total: 899.99, 
      items: [
        {
          index: 0, 
          product: {
            productId: '12345', 
            name: 'Black iPhone 12', 
            displayPrice: 899.99
          }, 
          quantity: 1, 
          total: 899.99
        }
      ]
    }
  })
</script>

<template>
<button @click="track">Trigger</button>
</template>
```

**Nuxt — TypeScript**

```vue
<script setup lang="ts">
const croct = useCroct()

const track = (): void => croct.track('cartViewed', {
    cart: {
      currency: 'USD', 
      total: 899.99, 
      items: [
        {
          index: 0, 
          product: {
            productId: '12345', 
            name: 'Black iPhone 12', 
            displayPrice: 899.99
          }, 
          quantity: 1, 
          total: 899.99
        }
      ]
    }
  })
</script>

<template>
<button @click="track">Trigger</button>
</template>
```

## Input properties

These are the supported properties:

- `cart`: `object`

  The shopping cart being viewed.

  - `currency`: `string`

    The currency in which the monetary values are expressed in the shopping cart, such as "USD" or "EUR".

    We recommend using the 3-letter currency codes defined by the [ISO 4217](https://en.wikipedia.org/wiki/ISO_4217) standard. For currencies having no official recognition in the standard, consider using ISO-like codes adopted locally or commercially, such as "XBT" for BitCoin.

  - `items`: `Array<object>`

    The list of items in the shopping cart.

    - `index`: `number`

      The position of the item in the shopping cart, starting from 0.

      This value typically represents the order in which the items were added to the cart. For example, the first item has an index of 0, the second item has an index of 1, and so on.

      The index must be non-negative and unique within the shopping cart.

    - `product`: `object`

      The product in the cart.

      - `productId`: `string`

        The ID that uniquely identifies the product on your store. For example, "3108" or "CD4371".

        The value must be between 1 and 50 characters long.

      - `sku`: `string` (optional)

        The SKU of the product. For example, "IPH-GRE-64", "153-169-172-182887".

        The value must be between 1 and 50 characters long.

      - `name`: `string`

        The name of the product. For example, "iPhone" or "Nike Air Max".

        The value must be between 1 and 200 characters long.

      - `category`: `string` (optional)

        The category of the product. For example, "Smartphones" or "Running shoes".

        The value must be between 1 and 100 characters long.

      - `brand`: `string` (optional)

        The brand of the product. For example, "Apple" or "Nike".

        The value must be between 1 and 100 characters long.

      - `variant`: `string` (optional)

        The variant of the product, such as size, color and style. For example, "Black", "M", "XL".

        The value must be between 1 and 50 characters long.

      - `currency`: `string` (optional)

        The currency in which the monetary values are expressed in the shopping cart, such as "USD" or "EUR".

        We recommend using the 3-letter currency codes defined by the [ISO 4217](https://en.wikipedia.org/wiki/ISO_4217) standard. For currencies having no official recognition in the standard, consider using ISO-like codes adopted locally or commercially, such as "XBT" for BitCoin.

      - `displayPrice`: `number`

        The price of the product displayed in the store. For example, 899.99.

        The value must be non-negative.

      - `originalPrice`: `number` (optional)

        The regular price of the product before any discounts. For example, 999.99.

        The value must be non-negative.

      - `url`: `string` (optional)

        The URL of the product page.

      - `imageUrl`: `string` (optional)

        The URL of the product image.

    - `quantity`: `number`

      The number of units of the item.

      The value must be greater than or equal to 1.

    - `discount`: `number` (optional)

      The amount of the discount applied to the item.

      The value must be non-negative.

    - `coupon`: `string` (optional)

      The coupon applied to the item. For example, "SUPER\_DEALS".

      The value must be between 1 and 50 characters long.

    - `total`: `number`

      The total for the item, including discounts and any other adjustment.

      The value must be non-negative.

  - `subtotal`: `number` (optional)

    The total of all items and quantities in the shopping cart including applied item promotions.

    The value must be non-negative.

  - `shippingPrice`: `number` (optional)

    The total shipping price for the items in the shopping cart, including any handling charges.

    The value must be non-negative.

  - `taxes`: `object` (optional)

    The taxes associated with the transaction.

    It must be expressed as a dictionary where the key is the tax name (up to 50 characters) and the value is the tax amount (non-negative).

    Example:

    ```json
    {
      "state": 3.1,
      "local": 1.5
    }
    ```

  - `costs`: `object` (optional)

    The costs associated with the transaction, such as manufacturing costs, shipping costs not paid by the customer, or other costs.

    It must be expressed as a dictionary where the key is the cost name (up to 50 characters) and the value is the cost amount (non-negative).

    Example:

    ```json
    {
      "manufacturing": 5.0,
      "packaging": 2.0
    }
    ```

  - `discount`: `number` (optional)

    The amount of discount applied to the shopping cart, as an absolute value. For example, `10.00` for a $10 discount.

    The value must be non-negative.

  - `coupon`: `string` (optional)

    The coupon applied to the shopping cart. For example, "SUPER\_DEALS".

    The value must be between 1 and 50 characters long.

  - `total`: `number`

    The total revenue or grand total associated with the transaction, including shipping, tax, and any other adjustment.

    The value must be non-negative.

  - `lastUpdateTime`: `number` (optional)

    The last time the shopping cart was updated in milliseconds since the Unix epoch.

    If not specified, the SDK considers the time at which you tracked the event to be the last update time.

## Processed properties

This event has no processed properties.

## Payload examples

Below are some payload examples for this event:

**Basic input**

```json
{
  "cart": {
    "currency": "USD",
    "total": 899.99,
    "items": [
      {
        "index": 0,
        "product": {
          "productId": "12345",
          "name": "Black iPhone 12",
          "displayPrice": 899.99
        },
        "quantity": 1,
        "total": 899.99
      }
    ]
  }
}
```

**Full input**

```json
{
  "cart": {
    "currency": "USD",
    "coupon": "SUMMER10",
    "discount": 110,
    "subtotal": 919.98,
    "shippingPrice": 10,
    "total": 929.98,
    "taxes": {
      "vat": 10
    },
    "costs": {
      "packaging": 5
    },
    "items": [
      {
        "index": 0,
        "quantity": 1,
        "total": 899.99,
        "product": {
          "productId": "12345",
          "sku": "PHONE-BLACK-64GB",
          "name": "Black iPhone 12",
          "category": "Smartphones",
          "brand": "Apple",
          "variant": "64GB",
          "displayPrice": 899.99,
          "originalPrice": 999.99,
          "url": "https://example.com/products/12345",
          "imageUrl": "https://example.com/images/12345.jpg"
        }
      },
      {
        "index": 1,
        "quantity": 1,
        "total": 19.99,
        "product": {
          "productId": "12346",
          "sku": "COVER-BLACK",
          "name": "Black iPhone 12 Cover",
          "category": "Accessories",
          "brand": "Apple",
          "variant": "Black",
          "displayPrice": 19.99,
          "originalPrice": 29.99,
          "url": "https://example.com/products/12346",
          "imageUrl": "https://example.com/images/12346.jpg"
        }
      }
    ]
  },
  "lastUpdateTime": 1499839200000
}
```

**Processed input**

```json
{
  "type": "cartViewed",
  "cart": {
    "currency": "USD",
    "coupon": "SUMMER10",
    "discount": 110,
    "subtotal": 919.98,
    "shippingPrice": 10,
    "total": 929.98,
    "taxes": {
      "vat": 10
    },
    "costs": {
      "packaging": 5
    },
    "items": [
      {
        "index": 0,
        "quantity": 1,
        "total": 899.99,
        "product": {
          "productId": "12345",
          "sku": "PHONE-BLACK-64GB",
          "name": "Black iPhone 12",
          "category": "Smartphones",
          "brand": "Apple",
          "variant": "64GB",
          "displayPrice": 899.99,
          "originalPrice": 999.99,
          "url": "https://example.com/products/12345",
          "imageUrl": "https://example.com/images/12345.jpg"
        }
      },
      {
        "index": 1,
        "quantity": 1,
        "total": 19.99,
        "product": {
          "productId": "12346",
          "sku": "COVER-BLACK",
          "name": "Black iPhone 12 Cover",
          "category": "Accessories",
          "brand": "Apple",
          "variant": "Black",
          "displayPrice": 19.99,
          "originalPrice": 29.99,
          "url": "https://example.com/products/12346",
          "imageUrl": "https://example.com/images/12346.jpg"
        }
      }
    ]
  },
  "lastUpdateTime": 1499839200000
}
```

## Explore

- [Analytics](/reference/analytics): Learn how to analyze your dashboards.
- [CQL](/explanation/audience/examples/shopping-cart): Learn how to create audiences based on the shopping cart.
