Skip to main content

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:
// 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:
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: '[email protected]',
        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:
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 for details.

See Also