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

# Coordinate SDKs across platforms

> Maintain a unified visitor identity and consistent experiment handling when deploying Kameleoon SDKs across web, mobile, and backend platforms simultaneously.

Coordinate Kameleoon SDKs across websites, mobile apps, and backend servers to maintain a unified visitor identity. This coordination ensures consistent experiment handling and data flow across all platforms.

Use this guide if a project implements multiple Kameleoon SDKs:

* The JavaScript SDK or `engine.js` for web environments.
* The mobile SDK for Android or iOS apps.
* Server-side SDKs for backend integrations and decision logic.

## Architecture overview

A typical multi-environment setup includes the following components:

* **Website**: Uses the JavaScript SDK or `engine.js` to run client-side experiments and personalization campaigns.
* **Mobile app**: Uses a mobile SDK to display variations and track conversions.
* **Backend server**: Uses a server-side SDK to make centralized variation decisions or synchronize visitor data.

## Data and visitor code flow

* The **visitor code** identifies users consistently across all environments.
* The **server-side SDK** coordinates variation decisions when required.
* Each SDK sends analytics and tracking data directly to the Kameleoon platform.

## Visitor code and cross-device management

### Generate and share visitor codes

Each unique visitor must have one consistent visitor code across all platforms. This code allows Kameleoon to unify experiment decisions and tracking.

Follow this cross-platform flow:

1. The engine, web SDK, server-side SDK, or mobile application generates the visitor code.
2. The backend retrieves the existing visitor code when the user logs in on a new platform.
3. The system passes the visitor code to the mobile, server-side, and web SDKs. This ensures all platforms evaluate the same user consistently.

### Synchronize visitor codes

* Store the visitor code securely in the backend or user database and link it to the user ID.
* Follow these steps when a user login occurs on another platform:
  * **Mobile app**: Request the visitor code from the backend.
  * **Server-side SDK**: Use the same visitor code for all variation and tracking calls.
  * **Web SDK**: Set the visitor code using the JavaScript API:

```javascript theme={null}
kameleoon.API.Visitor.setVisitorCode(visitorCode);
```

<Note>
  The web SDK and server-side SDK communicate automatically via the `kameleoonVisitorCode` cookie. You do not need to perform additional steps to link these two.
</Note>

### Best practices

* Map the existing user identifier (such as `userId`) to a single Kameleoon visitor code across all platforms.
* Maintain the backend as the source of truth for visitor codes.
* Prevent SDKs from overwriting existing visitor codes for known users.

Unified identification ensures that every user action, conversion, and variation decision links to the same visitor in Kameleoon.

## Experiment handling across environments

Coordinate decisions and variation displays for experiments that span multiple environments, such as mobile and backend.

### Example scenario

In this scenario:

* The **mobile app** displays the variation.
* The **backend server** determines the assigned variation.

#### Implementation options

**Option 1: Server-side decision logic**

1. The **mobile app** sends the visitor code to the backend.
2. The **server-side SDK** calls `getVariation()` (or its equivalent) to retrieve the assigned variation.
3. The backend returns the variation ID or name to the mobile app.
4. The **mobile app** displays the variation and tracks exposure.
5. Both the **mobile** and **server SDKs** use the same visitor code for tracking events.

**Advantages:**

* Centralizes variation logic and ensures consistent decisions.
* Supports experiments that affect both backend logic and UI elements.

**Option 2: Client-side (mobile) decision logic**

1. The **mobile SDK** retrieves the variation assignment using the visitor code.
2. The mobile app applies the variation locally and tracks exposure directly.
3. (*Optional*) If the backend also participates in tracking or validation, the **server-side SDK** reuses the same visitor code to ensure consistent reporting.

**Advantages:**

* Provides a synchronous, zero-latency decision.
* Reduces latency by avoiding a server round trip.
* Optimizes UI-focused experiments that do not depend on backend logic.

## Choose the right setup

| Use case                     | Decision layer  | Visitor code source | Notes                      |
| :--------------------------- | :-------------- | :------------------ | :------------------------- |
| **Mobile UI experiment**     | Mobile SDK      | Backend             | Handles variation locally  |
| **Backend or API-driven**    | Server-side SDK | App or website      | Centralized decision logic |
| **Cross-device consistency** | Any             | Backend             | Ensures unified tracking   |

## Code examples: Pass visitor code between layers

<CodeGroup>
  ```javascript Web (JS) theme={null}
  const visitorCode = kameleoon.API.Visitor.code;
  fetch("/api/syncVisitor", {
    method: "POST",
    body: JSON.stringify({ visitorCode }),
  });
  ```

  ```swift Mobile (iOS) theme={null}
  let kameleoonClient = try KameleoonClientFactory.create(
      siteCode: siteCode, 
      visitorCode: visitorCodeFromBackend
  )
  let variation = try kameleoonClient.getVariation(featureKey: featureKey)
  ```

  ```python Server (Python) theme={null}
  client = KameleoonClientFactory.create(SITE_CODE, config)
  variation = client.get_variation(visitor_code, feature_key)
  ```
</CodeGroup>

***

To handle SDK implementation across different layers:

* Assign a single visitor code per user across all platforms.
* Manage and distribute visitor codes from the backend.
* Select the variation logic layer based on the experiment type (server for centralized logic, client-side for app-driven UI).
* Maintain consistent, cross-device data by tracking all user activity with the same visitor code.

***
