# Server-Side Client ID

> Set up server-side client ID generation for scenarios where the AnyTrack Tracking Tag cannot load. Send PageView events, track conversions with standard events, and enrich data with event attributes using the collect endpoint.

> ⚠️ **Warning**
>
> This is an advanced implementation that requires working knowledge of JavaScript, server-side HTTP requests, and JSON payloads. If you can install the Tracking Tag on your website, use that instead — it handles session creation and event tracking automatically with no code required.

When the Tracking Tag cannot run on your page — Facebook Lead Ads, offline campaigns, headless checkouts, or landing pages you do not control — you can generate a client ID server-side and send events directly to the AnyTrack collect endpoint.

This creates a visitor session without any browser-side JavaScript. Once the session exists, you can track conversions through the [webhook endpoint](/docs/webhooks) using the `click_id` or `refId` parameter, just like any other integration.

> ❗ **Important**
>
> Replace `{propertyid}` with your actual AnyTrack Property ID. You can find it in your [Property Settings](/docs/property-settings).

## How It Works

1. Generate a random 14-character alphanumeric `cid` (client ID)
2. POST a JSON payload to the collect endpoint with the `cid`, event name, page URL, and timestamp
3. AnyTrack creates a visitor session tied to that `cid`
4. Send subsequent conversion events through the webhook endpoint using the `click_id` or `refId`

## Collect Endpoint

```
https://t1.anytrack.io/assets/{propertyid}/collect
```

Send a `POST` request with a JSON body containing the following fields.

## Collect Payload Fields

| Field | Type | Required | Description | Example |
| :---- | :--- | :------- | :---------- | :------ |
| `cid` | string | Yes | Client ID: a unique, random 14-character alphanumeric string. You generate this value. | `o5p4JFQGh1Yku4` |
| `en` | string | Yes | Event name. Use `PageView` to create the initial session. | `PageView` |
| `ts` | string | Yes | Timestamp in milliseconds (Unix epoch). | `1662315001464` |
| `nc` | integer | Yes | New client flag. Set to `1` for a new visitor, `0` for a returning one. | `1` |
| `dl` | string | Yes | Document location. The full page URL including UTM parameters and click IDs. | `https://example.com/?utm_source=google&gclid=abc` |
| `dt` | string | Yes | Document title. The page title associated with this event. | `My Landing Page` |
| `dr` | string | No | Document referrer. The URL the visitor came from. | `https://google.com` |
| `rid` | string | Recommended | External ID from your database (customer ID, lead ID). Use this to resolve identity and attribute conversions via `refId` instead of `click_id`. | `customer_12345` |

### Example Payload

```json
{
  "cid": "o5p4JFQGh1Yku4",
  "en": "PageView",
  "dl": "https://example.com/?utm_source=google&utm_medium=cpc&utm_campaign=tof-campaign&utm_id=12312321&gclid=abc123",
  "dt": "My Landing Page",
  "dr": "https://google.com",
  "nc": 1,
  "ts": "1662315001464",
  "rid": "lead_98765"
}
```

> 📘 **Note**
>
> Include all UTM parameters and ad platform click IDs (`gclid`, `fbclid`, `ttclid`) in the `dl` field. AnyTrack parses these automatically to attribute the session to the correct campaign.

## How to Generate the `cid`

The `cid` is a random 14-character alphanumeric string (a-z, A-Z, 0-9). Each visitor must receive a unique value. Here are examples for common automation tools:

### Zapier (Code by Zapier — JavaScript)

```javascript
// Generates a random 14-character alphanumeric client ID
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let cid = '';
for (let i = 0; i < 14; i++) {
  cid += chars.charAt(Math.floor(Math.random() * chars.length));
}
output = [{ cid }];
```

### Make (JavaScript Module)

```javascript
// Use in a Make Custom JavaScript module
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let cid = '';
for (let i = 0; i < 14; i++) {
  cid += chars.charAt(Math.floor(Math.random() * chars.length));
}
return cid;
```

### Python (Server-Side)

```python
import random
import string

# Generate a 14-character alphanumeric client ID
cid = ''.join(random.choices(string.ascii_letters + string.digits, k=14))
```

### Node.js

```javascript
const crypto = require('crypto');

// Generate a 14-character alphanumeric client ID
const cid = crypto.randomBytes(10).toString('base64url').slice(0, 14);
```

## Standard Events

After creating the initial session with a `PageView` event, you can send conversion events through the AnyTrack webhook endpoint. Use [standard event names](/docs/standard-events) so AnyTrack automatically maps them to each ad platform's format.

| Event Name | Description | Event Source |
| :--------- | :---------- | :----------- |
| `PageView` | Page view, automatically triggered on page load. Use this to create the initial session via the collect endpoint. | Browser |
| `ViewContent` | User views a product page | Browser |
| `OutboundClick` | User clicks an external link (generates a `click_id` automatically) | Browser |
| `AddToCart` | User adds an item to cart | Browser & Server-side |
| `InitiateCheckout` | User starts a checkout | Browser & Server-side |
| `AddPaymentInfo` | User adds payment details during checkout | Browser & Server-side |
| `FormSubmit` | User submits an online form | Browser |
| `Lead` | Lead event with a 0 monetary value | Server-side |
| `CompleteRegistration` | Lead event with a monetary value | Server-side |
| `Purchase` | Purchase event with a monetary value | Server-side |
| `Upsell` | Subsequent sale after the initial purchase | Server-side |

Send these events via the webhook endpoint after the session is created:

```json
{
  "click_id": "{propertyid}{cid}",
  "event_name": "Lead",
  "value": 0,
  "currency": "USD",
  "transactionId": "txn_abc123"
}
```

Or use the `refId` (external ID) if you passed a `rid` during session creation:

```json
{
  "refid": "lead_98765",
  "event_name": "Purchase",
  "value": 200,
  "currency": "USD",
  "transactionId": "order_xyz789"
}
```

> 📘 **Note**
>
> The `click_id` for server-side sessions is the Property ID concatenated with the `cid` you generated: `{propertyid}{cid}`. If you passed a `rid` during the collect call, you can use `refid` instead. Learn more about [using an external ID as click ID](/docs/external-id-as-click-id).

## Enrich Conversions with Event Attributes

Include [event attributes](/docs/event-attributes) in your webhook payload to improve Event Match Quality and give ad platforms richer signals for optimization.

### Customer Data

Customer traits are automatically hashed and normalized before being sent to ad platforms. There is no need to hash the data yourself.

| Attribute | Type | Example |
| :-------- | :--- | :------ |
| `email` | string | `jsmith@example.com` |
| `firstName` | string | `John` |
| `lastName` | string | `Smith` |
| `phone` | string | `+16505554444` |
| `city` | string | `Menlo Park` |
| `state` | string | `CA` |
| `zipcode` | string | `94025` |
| `country` | string | `US` |

### Transaction and Product Data

| Attribute | Type | Example |
| :-------- | :--- | :------ |
| `value` | number | `142.50` |
| `currency` | string | `USD` |
| `transactionId` | string | `order_xyz789` |
| `brand` | string | `Nike` |
| `shippingPrice` | number | `9.99` |
| `taxPrice` | number | `12.50` |
| `items` | array | See [Product Attributes](/docs/event-attributes#product-attributes) |

### Example: Enriched Purchase Event

```json
{
  "refid": "lead_98765",
  "event_name": "Purchase",
  "value": 142.50,
  "currency": "USD",
  "transactionId": "order_xyz789",
  "email": "jsmith@example.com",
  "firstName": "John",
  "lastName": "Smith",
  "phone": "+16505554444",
  "brand": "Nike",
  "items": [
    {
      "id": "SKU-001",
      "name": "Air Max 90",
      "price": 142.50,
      "quantity": 1,
      "brand": "Nike",
      "category": "Shoes"
    }
  ]
}
```

The more attributes you include, the higher your Event Match Quality score. Higher scores mean better attribution, more accurate lookalike audiences, and improved return on ad spend (ROAS). See the full list of supported attributes in the [Event Attributes](/docs/event-attributes) article.

## Use Cases

### Facebook and TikTok Lead Ads

Lead ad forms run natively inside the ad platform — your website never loads, so the Tracking Tag cannot execute. Generate a `cid` server-side when the lead arrives (via Zapier, Make, or the platform's webhook), send a `PageView` to the collect endpoint with the ad URL and UTM parameters, then send a `Lead` event through the webhook with the customer's email and phone.

### Offline Conversions

Phone calls, in-store visits, and events that happen outside a browser. Create a session with the campaign URL that drove the offline action, then post the conversion event (e.g., `Purchase`, `Lead`) when the sale closes. Include customer data for identity matching.

### Headless Checkouts and Custom Funnels

Checkout pages hosted on a third-party domain where you cannot install the Tracking Tag. Generate a `cid` on your server, pass it through the checkout flow, and send `InitiateCheckout` and `Purchase` events from your backend when the order completes.

### CRM-to-AnyTrack via Zapier or Make

When a CRM status changes (e.g., a lead moves to "Qualified" or "Closed Won" in HubSpot or Salesforce), trigger a Zap or Make scenario that generates a `cid`, creates a session with the original campaign URL, and sends the appropriate conversion event to AnyTrack.

### Landing Pages You Do Not Control

Affiliate campaigns or partner pages where you cannot add JavaScript. Use the `rid` field to pass your own lead or customer ID during session creation, then track conversions server-side as they happen in your system.

<FaqAccordion
  title="Server-Side Client ID FAQ"
  icon="fa-duotone fa-circle-question"
  items={[
    {
      question: "What is the AnyTrack Client ID?",
      answer: "The Client ID (cid) is a unique cookie ID — a random 14-character alphanumeric string that anonymously identifies a user in AnyTrack. When the Tracking Tag runs in the browser, it generates and stores this value as a first-party cookie automatically. In server-side implementations where the Tracking Tag cannot load, you generate the cid yourself and send it to the collect endpoint to create the session manually."
    },
    {
      question: "Why is the Client ID required to track conversions?",
      answer: "Every conversion in AnyTrack must be tied to an identified user. The cid is the anonymous identifier that makes this possible — it connects a conversion event (like a Purchase or Lead) back to the ad click, UTM parameters, and campaign data that started the journey. Without a cid, AnyTrack has no way to create the user session or build the attribution chain. No cid means no session, no attribution, and no data sent to your ad platforms."
    },
    {
      question: "What is an external ID (refid) and why do I need it?",
      answer: "The external ID (refid) is used to alias an AnyTrack cid with an external identifier that you control — such as an email address, phone number, or anonymous ID from your own system. You pass it as rid when creating the session via the collect endpoint. This is often needed when the AnyTrack click_id cannot be passed to your CRM, cart, or conversion source (for example, HubSpot, GoHighLevel, or other CRMs). In simple terms: when AnyTrack tracks an event in the browser, a click_id is generated. When you associate it with an external ID, you are telling AnyTrack that this click_id belongs to a specific email, and every future event sent identified by this email should be matched to the same user."
    },
    {
      question: "What is the difference between the Client ID and the Click ID?",
      answer: "The Client ID (cid) is a unique cookie ID that anonymously identifies a user. The Click ID (click_id) is a unique event identifier generated by the Tracking Tag on the client side — it associates a specific action that a cid performs to a Property ID and a timestamp. All event attributes (customer data, transaction details, product information) are associated to the click_id. In server-side implementations, you generate the cid yourself and construct the click_id by concatenating the Property ID with the cid."
    },
    {
      question: "Does each visitor need a unique cid value?",
      answer: "Yes. The cid is a unique 14-character alphanumeric string that identifies a single visitor session. Reusing the same cid across different visitors will merge their data into one session, corrupting attribution. Generate a new random value for every visitor."
    },
    {
      question: "When should I use server-side client ID instead of the Tracking Tag?",
      answer: "Use server-side client ID only when the Tracking Tag cannot run: Facebook or TikTok Lead Ads (no website loads), offline conversions, headless checkouts on third-party domains, or landing pages you do not control. In all other cases, the Tracking Tag is the recommended approach because it captures richer browser-side data automatically."
    },
    {
      question: "Can I send multiple conversion events after creating a single session?",
      answer: "Yes. Create one session with a PageView event via the collect endpoint, then send as many conversion events as needed through the webhook endpoint using the same click_id or refid. For example, you can track Lead, then InitiateCheckout, then Purchase as the customer progresses through your funnel."
    },
    {
      question: "Which AnyTrack plans support the server-side collect endpoint?",
      answer: "The collect endpoint is available on all AnyTrack plans. There are no plan-specific restrictions for server-side client ID generation."
    }
  ]}
/>

<Cards columns={2}>

  <Card title="Standard Conversions" href="/docs/standard-events" icon="fa-list-check" iconColor="green-500">
    Full list of standard event names and how they map to each ad platform.
  </Card>

  <Card title="Event Attributes" href="/docs/event-attributes" icon="fa-tags" iconColor="green-500">
    All supported attributes for enriching conversion data and improving Event Match Quality.
  </Card>

</Cards>
