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

# JS SDK: Read the Referral Tracking Code from Cookies

> Use kiflo('getTrackingCode') to read the current partner referral code from the browser cookie and pass it to your backend for server-side attribution.

When a visitor arrives on your website through a partner referral link, Kiflo stores a unique tracking code in a first-party browser cookie (`kfl_key`). The `kiflo('getTrackingCode')` method lets you read that value from JavaScript at any time, so you can use it in your own code — most commonly to pass it to your backend when you need to create customers or leads via the Kiflo REST API rather than the JS SDK.

## Syntax

```javascript theme={null}
var trackingCode = kiflo('getTrackingCode');
```

This call is **synchronous** — it returns the value immediately without any callbacks or promises.

## Return value

`kiflo('getTrackingCode')` returns one of two values:

| Return value                      | Meaning                                                                                                                |
| --------------------------------- | ---------------------------------------------------------------------------------------------------------------------- |
| A string (e.g., `"AAAAgCHM5Qk="`) | The visitor arrived through a partner referral link; this is their unique tracking code.                               |
| `undefined`                       | No tracking cookie is present — the visitor did not arrive through a partner referral link, or the cookie has expired. |

## Example

```javascript theme={null}
var trackingCode = kiflo('getTrackingCode');
console.log(trackingCode); // e.g. "AAAAgCHM5Qk=" or undefined

if (trackingCode) {
  console.log('Visitor was referred by a partner:', trackingCode);
} else {
  console.log('No referral tracking code found.');
}
```

## When to use this method

Use `kiflo('getTrackingCode')` when you create customers or leads **from your backend code** instead of calling `kiflo('customer', ...)` or `kiflo('lead', ...)` in the browser.

The browser-based SDK methods automatically bundle the tracking code with the customer data. But if your sign-up logic runs on the server, the backend has no access to the visitor's browser cookie. The solution is to:

<Steps>
  <Step title="Read the tracking code in the browser">
    Call `kiflo('getTrackingCode')` in the browser, for example inside your form's submit handler.

    ```javascript theme={null}
    var trackingCode = kiflo('getTrackingCode');
    ```
  </Step>

  <Step title="Send the tracking code to your server">
    Include the tracking code in your form data or API request payload. A common pattern is to store it in a hidden form field:

    ```javascript theme={null}
    document.getElementById('kifloTrackingCode').value = trackingCode || '';
    ```

    Or include it directly in a JSON request:

    ```javascript theme={null}
    fetch('/api/signup', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        email: formEmail,
        name: formName,
        kifloTrackingCode: trackingCode,
      }),
    });
    ```
  </Step>

  <Step title="Create the customer via the Kiflo REST API">
    From your backend, call the Kiflo REST API to create the customer and pass the `trackingCode` in the request body. Kiflo uses this value to attribute the customer to the correct partner.

    See the [Kiflo API reference](https://docs-api.kiflo.com) for the full request schema.
  </Step>
</Steps>

<Note>
  The Kiflo JS SDK must be installed on the page for `kiflo('getTrackingCode')` to work. Make sure the snippet in your `<head>` has fully loaded before you call this method. If you need to react to the exact moment the snippet is ready, use the [`ready` event](/developers/js-sdk/subscribe-to-events) instead.
</Note>
