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

# Suivre un Lead

> Créer un événement de conversion lead

## Endpoint

```
POST https://track.taap.it/api/events/lead
```

## Authentification

<Tabs>
  <Tab title="Côté serveur (Recommandé)">
    Utilisez votre clé API secrète :

    ```bash theme={null}
    Authorization: Bearer taapit_sk_xxxxxxxxxxxx
    ```
  </Tab>

  <Tab title="Côté client">
    Utilisez votre clé publique :

    ```bash theme={null}
    X-Publishable-Key: taapit_pk_xxxxxxxxxxxx
    ```
  </Tab>
</Tabs>

## Corps de la requête

| Champ                  | Type   | Requis | Description                                |
| ---------------------- | ------ | ------ | ------------------------------------------ |
| `trackingId`           | string | ✓      | Le `ta_tid` du cookie ou paramètre URL     |
| `customer`             | object | ✓      | Informations client                        |
| `customer.externalId`  | string | ✓      | Votre ID utilisateur/client interne        |
| `customer.email`       | string |        | Adresse email du client                    |
| `customer.firstname`   | string |        | Prénom du client                           |
| `customer.lastname`    | string |        | Nom du client                              |
| `customer.phoneNumber` | string |        | Numéro de téléphone du client              |
| `customer.avatarUrl`   | string |        | URL de l'avatar du client                  |
| `metadata`             | object |        | Données personnalisées (paires clé-valeur) |

## Exemples

<Tabs>
  <Tab title="cURL">
    ```bash theme={null}
    curl -X POST https://track.taap.it/api/events/lead \
      -H "Authorization: Bearer taapit_sk_xxx" \
      -H "Content-Type: application/json" \
      -d '{
        "trackingId": "rLnWe1uz9t282v7g",
        "customer": {
          "externalId": "user_123",
          "email": "john@example.com",
          "firstname": "John",
          "lastname": "Doe"
        },
        "metadata": {
          "source": "signup-form"
        }
      }'
    ```
  </Tab>

  <Tab title="SDK Node.js">
    ```typescript theme={null}
    import { Taapit } from 'taapit-sdk';

    const taapit = new Taapit({
      apiKey: process.env.TAAPIT_API_KEY,
    });

    await taapit.track.lead({
      trackingId: 'rLnWe1uz9t282v7g',
      customer: {
        externalId: 'user_123',
        email: 'john@example.com',
        firstname: 'John',
        lastname: 'Doe',
      },
      metadata: {
        source: 'signup-form',
      },
    });
    ```
  </Tab>

  <Tab title="Hook React">
    ```tsx theme={null}
    import { useTaapitAnalytics } from 'taapit-sdk/react';

    function SignUpForm() {
      const { trackLead } = useTaapitAnalytics();

      const handleSignUp = async (user) => {
        trackLead({
          customer: {
            externalId: user.id,
            email: user.email,
            firstname: user.firstName,
            lastname: user.lastName,
          },
          metadata: {
            source: 'signup-form',
          },
        });
      };
    }
    ```
  </Tab>

  <Tab title="JavaScript">
    ```html theme={null}
    <script src="https://taap.it/api/sdk" data-publishable-key="taapit_pk_xxx"></script>

    <script>
      taapit.trackLead({
        customer: {
          externalId: 'user_123',
          email: 'john@example.com',
          firstname: 'John',
          lastname: 'Doe',
        },
        metadata: {
          source: 'signup-form',
        },
      });
    </script>
    ```
  </Tab>
</Tabs>

## Réponse

<Tabs>
  <Tab title="200 OK">
    ```json theme={null}
    {
      "ok": true
    }
    ```
  </Tab>

  <Tab title="400 Bad Request">
    ```json theme={null}
    {
      "error": "Missing customer.externalId"
    }
    ```
  </Tab>

  <Tab title="401 Unauthorized">
    ```json theme={null}
    {
      "error": "Invalid API key"
    }
    ```
  </Tab>

  <Tab title="404 Not Found">
    ```json theme={null}
    {
      "error": "Click not found for this trackingId"
    }
    ```
  </Tab>
</Tabs>

## Codes d'erreur

| Code HTTP | Erreur                                         | Description                                |
| --------- | ---------------------------------------------- | ------------------------------------------ |
| 400       | `Invalid payload`                              | Validation du corps de requête échouée     |
| 400       | `Missing customer.externalId`                  | Champ requis non fourni                    |
| 401       | `Invalid API key`                              | Clé API invalide ou révoquée               |
| 401       | `Hostname not in allowed list`                 | Origine non autorisée (côté client)        |
| 403       | `This click does not belong to your workspace` | trackingId appartient à un autre workspace |
| 404       | `Click not found for this trackingId`          | trackingId n'existe pas ou a expiré        |
