> ## Documentation Index
> Fetch the complete documentation index at: https://docs.cometly.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Comet Token

> Learn how to use the cometToken() function to improve attribution accuracy when sending events via the API

## Overview

The `cometToken()` function is available through the Cometly tracking pixel and returns a unique identifier that links a user's browser session to their attribution data. When sending events via the API, including the comet token significantly improves attribution accuracy.

## Using cometToken()

The `cometToken()` function returns a string value directly:

```javascript theme={null}
// Get the comet token - returns immediately
const token = cometToken();
console.log(token); // "ddffd8d8338383883838484843838388d8df8f"
```

## Sending Events with Comet Token

When sending events to the API, include the `comet_token` parameter for better attribution:

```javascript theme={null}
const token = cometToken();

const response = await fetch('https://app.cometly.com/public-api/v1/events/track', {
    method: 'POST',
    headers: {
        'Authorization': 'Bearer YOUR_API_KEY',
        'Accept': 'application/json',
        'Content-Type': 'application/json'
    },
    body: JSON.stringify({
        event_name: 'purchase',
        email: 'customer@example.com',
        comet_token: token,
        amount: 99.99
    })
});
```

## Prerequisites

The Cometly tracking pixel must be installed on your website for `cometToken()` to be available.

## Common Use Cases

### Form Submissions

Capture the comet token when a form is submitted:

```javascript theme={null}
document.getElementById('signup-form').addEventListener('submit', (e) => {
    const formData = new FormData(e.target);
    formData.append('comet_token', cometToken());

    fetch('/api/signup', {
        method: 'POST',
        body: formData
    });
});
```

### OAuth Sign Up (Google, Apple, etc.)

When users sign up via OAuth providers, Cometly's automatic form capture doesn't work. You must capture the comet token when the user clicks the OAuth button and include it when sending the event. See [Tracking Events When Using OAuth Sign Up](https://help.cometly.com/en/articles/9898157-tracking-events-when-using-oauth-sign-up-with-google) for details.

## See Also

* [Create Event](/api-reference/endpoint/create-event) - API endpoint for sending events
* [Cometly Help Center](https://help.cometly.com) - Installation guides and troubleshooting
