> For the complete documentation index, see [llms.txt](https://docs.chord.co/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.chord.co/chord-oms/shopify/developer-tools/gatsby/sdk-reference/useanalytics.md).

# useAnalytics

The `useAnalytics` hook returns a series of methods that fire a tracking event to Segment, our integrated customer data platform.

Most Segment tracking is done automatically by the SDK. For example, when you use the `addToCart` method (see `useCart`), a corresponding `track` API call to Segment is made. However, some `track` calls need to be closely tied to the UI, as in the case of tracking clicks on elements, and these calls need to be explicitly made from your React components. Methods for making these calls are available via the `useAnalytics` hook and listed below.

{% code title="useAnalytics" %}

```javascript
import { useAnalytics } from '@chordcommerce/gatsby-theme-performance'

const {
  trackCartViewed,
  trackCollectionClicked,
  trackEmailCaptured,
  trackProductClicked,
  trackProductListFiltered,
  trackProductListViewed,
  trackProductViewed,
  trackPromotionClicked,
  trackPromotionViewed,
  trackStockRequestCreated
} = useAnalytics()
```

{% endcode %}

### trackCartViewed()

Results in a `Cart Viewed` `track`event in Segment.

**Example:**

{% code title="trackCartViewed" %}

```javascript
trackCartViewed()
```

{% endcode %}

### trackCollectionClicked()

Results in a `Collection Clicked` `track` event in Segment.

**Example:**

{% code title="trackCollectionClicked" %}

```javascript
trackCollectionClicked({ id, description, imageUrl, slug, title })
```

{% endcode %}

### trackEmailCaptured()

Results in a `Email Captured` `track` event in Segment.

**Example:**

{% code title="trackEmailCaptured" %}

```javascript
trackEmailCaptured({
    email: 'test@test.com',
    placement: 'footer'
})
```

{% endcode %}

### trackProductClicked()

Results in a `Product Clicked` `track` event in Segment.

**Example:**

{% code title="trackProductClicked" %}

```javascript
trackProductClicked({ productHandle: 'product-1' })
```

{% endcode %}

### trackProductListFiltered()

Results in a `Product List Filtered` `track` event in Segment.

**Example:**

{% code title="trackProductListFiltered" %}

```javascript
trackProductListFiltered({
    filter: {
        type: 'color',
        value: 'red'
    },
    list: {
        category: 'shirt',
        id: '123'
    },
    sort: {
        type: 'ascending',
        value: 'price'
    }
})
```

{% endcode %}

### trackProductListViewed()

Results in a `Product List Viewed` `track` event in Segment.

**Example:**

{% code title="trackProductListViewed" %}

```javascript
trackProductListViewed({
    list: {
        category: 'shirt',
        id: '123'
    }
})
```

{% endcode %}

### trackProductViewed()

Results in a `Product Viewed` `track` event in Segment.

**Example:**

{% code title="trackProductViewed" %}

```javascript
trackProductViewed({ productHandle: 'product-1' })
```

{% endcode %}

### trackPromotionClicked()

Results in a `Promotion Clicked` `track` event in Segment.

**Example:**

{% code title="trackPromotionClicked" %}

```javascript
trackPromotionClicked({
    creative: 'apple',
    id: '123',
    name: 'red-shirt-sale',
    position: 'banner'
})
```

{% endcode %}

### trackPromotionViewed()

Results in a `Promotion Viewed` `track` event in Segment.

**Example:**

{% code title="trackPromotionClicked" %}

```javascript
trackPromotionClicked({
    creative: 'apple',
    id: '123',
    name: 'red-shirt-sale',
    position: 'banner'
})
```

{% endcode %}

### trackStockRequestCreated()

Results in a `Stock Request Created` `track` event in Segment.

**Example:**

{% code title="trackStockRequestCreated" %}

```javascript
trackStockRequestCreated({
    email: 'test@test.com'.
    productHandle: 'product-1'
})
```

{% endcode %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.chord.co/chord-oms/shopify/developer-tools/gatsby/sdk-reference/useanalytics.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
