# Running Your First Experiment

Now that your SDK is set up and you have a context, you're ready to run an experiment. Here's what that looks like from your code's perspective:

1. **Check the variant** to find out which experience this user should see
2. **Render the right experience** based on that variant (or use variables to make it flexible)
3. **Track goals** to measure whether the experiment is working
4. **Send the data** to ABsmartly's collector

Once the context is ready, all `treatment()` calls are resolved locally with no additional network requests. Any experiment that isn't running will return `0` (Variant A, the control group).

Steps 1 and 3 are all you need for a basic experiment. Step 2 (variables) and Step 4 (manual event flushing) are useful but not required for your first test.

## Step 1: Check which variant the user is in

The `treatment()` method is how you find out which variant a user has been assigned to. It returns a number that maps to the variants you see in the Web Console:

| Number | Web Console name | Meaning                           |
| ------ | ---------------- | --------------------------------- |
| `0`    | Variant A        | Control (the baseline experience) |
| `1`    | Variant B        | First treatment                   |
| `2`    | Variant C        | Second treatment                  |
| `3`    | Variant D        | Third treatment                   |
| ...    | ...              | ...                               |

Calling `treatment()` also records an **exposure**, which tells ABsmartly that this user actually saw the experiment. This is how ABsmartly knows to include them in the statistical analysis.

Use an `if/else` block to branch your code based on the variant:

**Javascript**

```js
const treatment = context.treatment("homepage_banner_experiment");

if (treatment === 1) {
  showBanner("Check out our latest deals!");
} else {
  showBanner("Welcome back!");
}
```

**Python3**

```py
treatment = context.get_treatment("homepage_banner_experiment")

if treatment == 1:
    show_banner("Check out our latest deals!")
else:
    show_banner("Welcome back!")
```

**React**

```jsx
const { variant } = useTreatment("homepage_banner_experiment");

if (variant === 1) {
  return <Banner text="Check out our latest deals!" />;
}

return <Banner text="Welcome back!" />;
```

**Alternative: declarative component patterns**

The React SDK also provides component-based approaches. The `Treatment` and `TreatmentVariant` components let you declare variants without conditional logic. You can pass variant letters (`"A"`, `"B"`) or numbers:

```jsx
return (
  <Treatment name="experiment_name" loadingComponent={<MySpinner />}>
    {/* The variant prop can have numbers or letters passed to it */}
    <TreatmentVariant variant="A">
      {/* Variant 0 */}
      <DefaultButton />
    </TreatmentVariant>
    <TreatmentVariant variant="B">
      {/* Variant 1 */}
      <ButtonVariant1 />
    </TreatmentVariant>
    <TreatmentVariant variant="C">
      {/* Variant 2 */}
      <ButtonVariant2 />
    </TreatmentVariant>
  </Treatment>
);
```

For more control, `TreatmentFunction` gives you the variant as a render prop:

```jsx
return (
  <TreatmentFunction name="experiment_name" loadingComponent={<MySpinner />}>
    {({ variant }) =>
      variant === 1 ? (
        <ButtonVariant1 />
      ) : variant === 2 ? (
        <ButtonVariant2 />
      ) : (
        <DefaultButton />
      )
    }
  </TreatmentFunction>
);
```

**Swift**

```swift
let treatment = context.getTreatment("homepage_banner_experiment")

if treatment == 1 {
    showBanner("Check out our latest deals!")
} else {
    showBanner("Welcome back!")
}
```

**Vue2**

```html
<treatment name="homepage_banner_experiment">
  <template #A>
    <banner text="Welcome back!" />
  </template>
  <template #B>
    <banner text="Check out our latest deals!" />
  </template>
</treatment>
```

**Alternative slot patterns**

**By variant index** - use slot names `#0`, `#1`, `#2` to match variant numbers directly. Each slot receives `treatment`, `ready`, and `failed` as scoped properties:

```html
<treatment name="exp_test_experiment">
    <template #0>
        <my-button></my-button>
    </template>
    <template #1="{ treatment }">
        <my-button :variant="treatment"></my-button>
    </template>
    <template #2="{ treatment }">
        <my-other-button :variant="treatment"></my-other-button>
    </template>
    <template #loading>
        <my-spinner></my-spinner>
    </template>
</treatment>
```

**Default slot only** - use a single default slot and handle the branching yourself with the `treatment`, `ready`, and `failed` scoped properties:

```html
<treatment name="exp_test_experiment">
    <template #default="{ treatment, ready, failed }">
        <template v-if="ready">
            <my-button v-if="treatment == 0"></my-button>
            <my-button v-else-if="treatment == 1"></my-button>
            <my-other-button v-else-if="treatment == 2"></my-other-button>
        </template>
        <template v-else><my-spinner></my-spinner></template>
    </template>
</treatment>
```

**Vue3**

```html
<treatment name="homepage_banner_experiment">
  <template #A>
    <banner text="Welcome back!" />
  </template>
  <template #B>
    <banner text="Check out our latest deals!" />
  </template>
</treatment>
```

**Alternative slot patterns**

**By variant index** - use slot names `#0`, `#1`, `#2` to match variant numbers directly. Each slot receives `treatment`, `ready`, and `failed` as scoped properties:

```html
<treatment name="exp_test_experiment">
    <template #0>
        <my-button></my-button>
    </template>
    <template #1="{ treatment }">
        <my-button :variant="treatment"></my-button>
    </template>
    <template #2="{ treatment }">
        <my-other-button :variant="treatment"></my-other-button>
    </template>
    <template #loading>
        <my-spinner></my-spinner>
    </template>
</treatment>
```

**Default slot only** - use a single default slot and handle the branching yourself with the `treatment`, `ready`, and `failed` scoped properties:

```html
<treatment name="exp_test_experiment">
    <template #default="{ treatment, ready, failed }">
        <template v-if="ready">
            <my-button v-if="treatment == 0"></my-button>
            <my-button v-else-if="treatment == 1"></my-button>
            <my-other-button v-else-if="treatment == 2"></my-other-button>
        </template>
        <template v-else><my-spinner></my-spinner></template>
    </template>
</treatment>
```

**Java**

```java
int treatment = context.getTreatment("homepage_banner_experiment");

if (treatment == 1) {
    showBanner("Check out our latest deals!");
} else {
    showBanner("Welcome back!");
}
```

**Go**

```go
treatment, _ := context.GetTreatment("homepage_banner_experiment")

if treatment == 1 {
    showBanner("Check out our latest deals!")
} else {
    showBanner("Welcome back!")
}
```

**.NET**

```csharp
var treatment = context.GetTreatment("homepage_banner_experiment");

if (treatment == 1) {
    ShowBanner("Check out our latest deals!");
} else {
    ShowBanner("Welcome back!");
}
```

**PHP**

```php
$treatment = $context->getTreatment('homepage_banner_experiment');

if ($treatment === 1) {
    showBanner('Check out our latest deals!');
} else {
    showBanner('Welcome back!');
}
```

**Ruby**

```ruby
treatment = context.treatment('homepage_banner_experiment')

if treatment == 1
  show_banner("Check out our latest deals!")
else
  show_banner("Welcome back!")
end
```

**Flutter/Dart**

```dart
final treatment = await context.getTreatment("homepage_banner_experiment");

if (treatment == 1) {
  showBanner("Check out our latest deals!");
} else {
  showBanner("Welcome back!");
}
```

This works well for experiments where the logic change is entirely in your code (show component A vs component B). But if you want the Web Console to control values like colours, copy, or feature flags, read on.

## Step 2: Use variables for flexible experiments

Instead of hardcoding what each variant does, you can use **treatment variables**. These are key-value pairs that you set in the Web Console and pull into your code at runtime.

The benefit: your product team can add new variants or tweak existing ones from the Web Console without anyone touching code.

For example, say you have an experiment to find out what colour of button gets the most clicks. In the Web Console, you'd add a variable called `button.color` with value `"green"` on Variant B. You don't need to set anything on Variant A - that's what the default value in your code is for. Then in your code, `variableValue()` returns the experiment value for the user's assigned variant, or the default if the experiment isn't running or the user is in the control group:

**Javascript**

```js
const buttonColor = context.variableValue("button.color", "blue");
document.getElementById("cta-button").style.backgroundColor = buttonColor;
```

**Python3**

```py
button_color = context.get_variable_value("button.color", "blue")
render_button(color=button_color)
```

**React**

```jsx
const { context } = useABSmartly();
const buttonColor = context.variableValue("button.color", "blue");

return <button style={{ backgroundColor: buttonColor }}>Get Started</button>;
```

**Swift**

```swift
let buttonColor = context.getVariableValue("button.color", defaultValue: "blue")
ctaButton.backgroundColor = UIColor(named: buttonColor)
```

**Vue2**

```js
const buttonColor = this.$absmartly.variableValue("button.color", "blue");
this.ctaStyle = { backgroundColor: buttonColor };
```

**Vue3**

```js
const buttonColor = this.$absmartly.variableValue("button.color", "blue");
this.ctaStyle = { backgroundColor: buttonColor };
```

**Java**

```java
String buttonColor = (String) context.getVariableValue("button.color", "blue");
ctaButton.setBackgroundColor(Color.parseColor(buttonColor));
```

**Go**

```go
buttonColor, _ := context.GetVariableValue("button.color", "blue")

if color, ok := buttonColor.(string); ok {
    renderButton(color)
}
```

**.NET**

```csharp
var buttonColor = context.GetVariableValue("button.color", "blue");
ctaButton.BackgroundColor = Color.FromName(buttonColor);
```

**PHP**

```php
$buttonColor = $context->getVariableValue('button.color', 'blue');
echo '<button style="background-color: ' . htmlspecialchars($buttonColor) . '">Get Started</button>';
```

**Ruby**

```ruby
button_color = context.variable_value('button.color', 'blue')
render_button(color: button_color)
```

**Flutter/Dart**

```dart
final buttonColor = context.getVariableValue("button.color", "blue");
ctaButton.style = ButtonStyle(backgroundColor: colorFromName(buttonColor));
```

## Step 3: Track goals to measure success

An experiment without goals is like running a test without checking the results. Goals are the actions you want to measure: did the user click the button? Complete a purchase? Sign up for a newsletter?

The `track()` method records these actions. Each goal corresponds to a `goal_name` defined in the Web Console. You can also pass properties to create more granular metrics.

The `track()` method records user actions. It takes two arguments:

* **goal\_name**: The name of the goal, matching one you've defined in the Web Console. Any name that doesn't match will be ignored (unless `AUTO_GOAL_CREATION` is enabled on your instance).
* **properties** (optional): Key-value pairs for extra metrics or filtering (e.g., revenue, item category).

**Javascript**

```js
var properties = {
  price: 10000,
  category: "5 stars",
  free_cancellation: true,
  instance_id: 5350,
};

context.track("booking", properties);
```

**Python3**

```py
    context.track("payment", {
            "item_count": 1,
            "total_amount": 1999.99
    })
```

**React**

```jsx
const { context } = useABSmartly();

const properties = {
  price: 10000,
  category: "5 stars",
  free_cancellation: true,
  instance_id: 5350,
};
context.track("booking", properties);
```

**Swift**

```swift
context.track("payment", properties: ["item_count": 1, "total_amount": 1999.99])
```

**Vue2**

```js
var properties = {
  price: 10000,
  category: "5 stars",
  free_cancellation: true,
  instance_id: 5350,
};

this.$absmartly.track("booking", properties);
```

**Vue3**

```js
var properties = {
  price: 10000,
  category: "5 stars",
  free_cancellation: true,
  instance_id: 5350,
};

this.$absmartly.track("booking", properties);
```

**Java**

```java
    context.track("payment", Map.of(
        "item_count", 1,
        "total_amount", 1999.99
    ));
```

**Go**

```go
context.Track("payment", map[string]interface{}{
        "item_count": 1,
        "total_amount": 1999.99
})
```

**.NET**

```csharp
    context.Track("payment", new Dictionary<string, object>() {
        { "item_count", 1 },
        { "total_amount", 1999.99}
    });
```

**PHP**

```php
$context->track(
    'payment',
    (object) ['item_count' => 1, 'total_amount' => 1999.99]
);
```

**Ruby**

```ruby
context.track(
    'payment',
    { item_count: 1, total_amount: 1999.99 }
)
```

**Flutter/Dart**

```dart
    context.track("payment",{
      "item_count": 1,
      "total_amount": 1999.99
    });
```

> **info**
>
> * By default, the `goal_name` must match exactly what you defined in the Web Console. Anything else will be ignored. However, if your instance has `AUTO_GOAL_CREATION` enabled, any unseen goals will be automatically created in the Web Console.
> * Properties are optional but useful for filtering and creating extra metrics.

## Step 4: Make sure your data gets sent

Most SDKs batch events and send them to ABsmartly's collector automatically. For most use cases, you don't need to do anything here.

> **Node.js, PHP, and Ruby users**
>
> The Javascript SDK running in Node.js, the PHP SDK, and the Ruby SDK do not auto-publish events. You must call `publish()` or `finalize()` explicitly at the end of each request, or your exposure and goal data will not be sent.

Even with auto-publishing, there are times when you need to guarantee events are sent before moving on. For example, before a page navigation in a multi-page app, or before a server sends its response. In those cases, call `publish()` explicitly:

**Using publish() to flush events manually**

Call `publish()` before page navigations, server responses, or any point where the process might exit before the SDK's auto-flush fires. This ensures exposure and goal data is sent to the collector before the context is lost.

**Javascript**

```js
// You can just publish
context.publish();

// or wait for it to finish, so if you want to
// navigate to another page without losing impressions,
// you can do that once the promise resolves.
context.publish().then(function () {
  document.location.replace("another_page");
});
```

**Python3**

Python also provides `publish_async()` for non-blocking use.

Without an explicit `publish()`, the Python SDK sends queued events after `ContextConfig.publish_delay` **seconds**, which defaults to **50 seconds**. Set a smaller value (in seconds) to publish sooner, or `0` to publish immediately.

Before the process exits, call `close()` instead of `publish()`. `close()` also stops automatic refresh, whose timer otherwise keeps the Python process running.

```py
    context.publish()
```

**React**

```jsx
const { context } = useABSmartly();

context.publish().then(() => {
  document.location.replace("another_page");
});
```

**Swift**

```swift
context.publish().done {
    print("all pending events published")
}
```

**Vue2**

```js
await this.$absmartly.publish().then(() => {
  window.location = "https://www.absmartly.com";
});
```

**Vue3**

```js
await this.$absmartly.publish().then(() => {
  window.location = "https://www.absmartly.com";
});
```

**Java**

Java also provides `publishAsync()` for non-blocking use.

```java
    context.publish();
```

**Go**

Go also provides `PublishAsync()` for non-blocking use.

```go
context.Publish()
```

**.NET**

.NET also provides `PublishAsync()` for non-blocking use.

```csharp
    context.Publish();
```

**PHP**

```php
$context->publish();
```

**Ruby**

```ruby
context.publish
```

**Flutter/Dart**

```dart
    context.publish();
```

## Putting it all together

Here's a realistic starting point you can copy and adapt. It covers SDK init, context creation, treatment checking, variable usage, goal tracking, and cleanup.

**Javascript**

```js
const absmartly = require("@absmartly/javascript-sdk");

// Initialize the SDK
const sdk = new absmartly.SDK({
  endpoint: "https://your-company.absmartly.io/v1",
  apiKey: "YOUR-API-KEY",
  environment: "production",
  application: "website",
});

// Create a context for this user
const request = {
  units: {
    user_id: "user-12345",
  },
};

const context = sdk.createContext(request);

context.ready().then(() => {
  // Check which variant the user is in
  const treatment = context.treatment("homepage_banner_experiment");

  if (treatment === 1) {
    // Variant B: show a personalized banner
    showBanner("Welcome back, we have new deals for you!");
  } else {
    // Variant A (control): show the existing banner
    showBanner("Welcome back!");
  }

  // Use a variable set in the Web Console for more flexibility
  const buttonColor = context.variableValue("button.color", "blue");
  setButtonColor(buttonColor);

  // Track when the user clicks the CTA button
  document.getElementById("cta-button").addEventListener("click", () => {
    context.track("cta_click", { page: "homepage" });
  });

  // Track a purchase with revenue data
  onPurchaseComplete((order) => {
    context.track("purchase", {
      revenue: order.total,
      item_count: order.items.length,
    });
  });
});

// When the user leaves or the app shuts down, finalize the context
// to flush any remaining events
window.addEventListener("beforeunload", () => {
  context.finalize();
});
```

**Python3**

```py
from absmartly import ClientConfig, Client, DefaultHTTPClient, DefaultHTTPClientConfig, ABSmartlyConfig, ABSmartly, ContextConfig

# Initialize the SDK
client_config = ClientConfig()
client_config.endpoint = "https://your-company.absmartly.io/v1"
client_config.api_key = "YOUR-API-KEY"
client_config.application = "website"
client_config.environment = "production"

sdk_config = ABSmartlyConfig()
sdk_config.client = Client(client_config, DefaultHTTPClient(DefaultHTTPClientConfig()))
sdk = ABSmartly(sdk_config)

# Create a context for this user
context_config = ContextConfig()
context_config.units = {"user_id": "user-12345"}
context = sdk.create_context(context_config)
context.wait_until_ready()

# Check which variant the user is in
treatment = context.get_treatment("homepage_banner_experiment")

if treatment == 1:
    # Variant B: show a personalized banner
    show_banner("Welcome back, we have new deals for you!")
else:
    # Variant A (control): show the existing banner
    show_banner("Welcome back!")

# Use a variable set in the Web Console for more flexibility
button_color = context.get_variable_value("button.color", "blue")
set_button_color(button_color)

# Track when the user completes a key action
context.track("cta_click", {"page": "homepage"})

# Track a purchase with revenue data
context.track("purchase", {
    "revenue": 49.99,
    "item_count": 3,
})

# Close the context to flush remaining events
context.close()
```

**React**

```jsx
// This example shows multiple React patterns - pick the one that fits your component
import ABSmartly, {
  Treatment,
  TreatmentVariant,
  useABSmartly,
  useTreatment,
} from "@absmartly/react-sdk";

function App() {
  const sdkOptions = {
    endpoint: "https://your-company.absmartly.io/v1",
    apiKey: "YOUR-API-KEY",
    environment: "production",
    application: "website",
  };

  const contextOptions = {
    units: { user_id: "user-12345" },
  };

  return (
    <ABSmartly sdkOptions={sdkOptions} contextOptions={contextOptions}>
      <Homepage />
    </ABSmartly>
  );
}

function CtaButton({ onClick }) {
  const { variant } = useTreatment("cta_color_experiment");

  const color = variant === 1 ? "blue" : "green";

  return (
    <button onClick={onClick} style={{ backgroundColor: color }}>
      Get Started
    </button>
  );
}

function Homepage() {
  const { context } = useABSmartly();

  const handleCtaClick = () => {
    context.track("cta_click", { page: "homepage" });
  };

  const handlePurchase = (order) => {
    context.track("purchase", {
      revenue: order.total,
      item_count: order.items.length,
    });
  };

  return (
    <div>
      {/* Option 1: useTreatment hook for programmatic variant logic */}
      <CtaButton onClick={handleCtaClick} />

      {/* Option 2: Treatment components for declarative variant rendering */}
      <Treatment name="homepage_banner_experiment">
        <TreatmentVariant variant={0}>
          <Banner text="Welcome back!" />
        </TreatmentVariant>
        <TreatmentVariant variant={1}>
          <Banner text="Welcome back, we have new deals for you!" />
        </TreatmentVariant>
      </Treatment>
    </div>
  );
}
```

**Swift**

```swift
import ABSmartly

// Initialize the SDK
let clientConfig = ClientConfig(
    apiKey: "YOUR-API-KEY",
    application: "website",
    endpoint: "https://your-company.absmartly.io/v1",
    environment: "production"
)
let client = try DefaultClient(config: clientConfig)
let sdkConfig = ABSmartlyConfig.create()
sdkConfig.client = client
let sdk = try ABSmartlySDK(config: sdkConfig)

// Create a context for this user
let contextConfig = ContextConfig()
contextConfig.setUnit(unitType: "user_id", uid: "user-12345")
let context = sdk.createContext(config: contextConfig)

context.waitUntilReady().done { context in
    // Check which variant the user is in
    let treatment = context.getTreatment("homepage_banner_experiment")

    if treatment == 1 {
        // Variant B: show a personalized banner
        showBanner("Welcome back, we have new deals for you!")
    } else {
        // Variant A (control): show the existing banner
        showBanner("Welcome back!")
    }

    // Use a variable set in the Web Console for more flexibility
    let buttonColor = context.getVariableValue("button.color", defaultValue: "blue")
    setButtonColor(buttonColor)

    // Track when the user completes a key action
    context.track("cta_click", properties: ["page": "homepage"])

    // Track a purchase with revenue data
    context.track("purchase", properties: [
        "revenue": 49.99,
        "item_count": 3
    ])

    // Close the context when done to flush remaining events
    context.close()
}
```

**Vue2**

```js
// main.js - Initialize the SDK as a Vue plugin
import Vue from "vue";
import App from "./App.vue";
import absmartly from "@absmartly/vue2-sdk";

Vue.use(absmartly.ABSmartlyVue, {
  sdkOptions: {
    endpoint: "https://your-company.absmartly.io/v1",
    apiKey: "YOUR-API-KEY",
    environment: "production",
    application: "website",
  },
  context: {
    units: { user_id: "user-12345" },
  },
  attributes: {
    user_agent: navigator.userAgent,
  },
});

new Vue({ render: (h) => h(App) }).$mount("#app");

// In your component, use treatments and track goals:
//
// <template>
//   <div>
//     <treatment name="homepage_banner_experiment">
//       <template #A>  <!-- Variant A (control) -->
//         <banner text="Welcome back!" />
//       </template>
//       <template #B>  <!-- Variant B -->
//         <banner text="Welcome back, we have new deals for you!" />
//       </template>
//     </treatment>
//
//     <button @click="trackClick">Get Started</button>
//   </div>
// </template>
//
// <script>
// export default {
//   methods: {
//     trackClick() {
//       this.$absmartly.track("cta_click", { page: "homepage" });
//     },
//   },
//   beforeDestroy() {
//     this.$absmartly.finalize();
//   },
// };
// </script>
```

**Vue3**

```js
// main.js - Initialize the SDK as a Vue plugin
import { createApp } from "vue";
import App from "./App.vue";
import absmartly from "@absmartly/vue3-sdk";

const app = createApp(App);

app.use(absmartly.ABSmartlyVue, {
  sdkOptions: {
    endpoint: "https://your-company.absmartly.io/v1",
    apiKey: "YOUR-API-KEY",
    environment: "production",
    application: "website",
  },
  context: {
    units: { user_id: "user-12345" },
  },
  attributes: {
    user_agent: navigator.userAgent,
  },
});

app.mount("#app");

// In your component, use treatments and track goals:
//
// <template>
//   <div>
//     <treatment name="homepage_banner_experiment">
//       <template #A>  <!-- Variant A (control) -->
//         <banner text="Welcome back!" />
//       </template>
//       <template #B>  <!-- Variant B -->
//         <banner text="Welcome back, we have new deals for you!" />
//       </template>
//     </treatment>
//
//     <button @click="trackClick">Get Started</button>
//   </div>
// </template>
//
// <script>
// export default {
//   methods: {
//     trackClick() {
//       this.$absmartly.track("cta_click", { page: "homepage" });
//     },
//   },
//   beforeUnmount() {
//     this.$absmartly.finalize();
//   },
// };
// </script>
```

**Java**

```java
import com.absmartly.sdk.*;
import java.util.Map;

public class ExperimentExample {
    public static void main(String[] args) {
        // Initialize the SDK
        final ClientConfig clientConfig = ClientConfig.create()
            .setEndpoint("https://your-company.absmartly.io/v1")
            .setAPIKey("YOUR-API-KEY")
            .setApplication("website")
            .setEnvironment("production");

        final Client client = Client.create(clientConfig);
        final ABSmartlyConfig config = ABSmartlyConfig.create()
            .setClient(client);
        final ABSmartly sdk = ABSmartly.create(config);

        // Create a context for this user
        final ContextConfig contextConfig = ContextConfig.create()
            .setUnit("user_id", "user-12345");
        try (final Context context = sdk.createContext(contextConfig)
            .waitUntilReady()) {

            // Check which variant the user is in
            if (context.getTreatment("homepage_banner_experiment") == 1) {
                // Variant B: show a personalized banner
                showBanner("Welcome back, we have new deals for you!");
            } else {
                // Variant A (control): show the existing banner
                showBanner("Welcome back!");
            }

            // Use a variable set in the Web Console for more flexibility
            String buttonColor = context.getVariableValue("button.color", "blue");
            setButtonColor(buttonColor);

            // Track when the user completes a key action
            context.track("cta_click", Map.of("page", "homepage"));

            // Track a purchase with revenue data
            context.track("purchase", Map.of(
                "revenue", 49.99,
                "item_count", 3
            ));
        }
    }
}
```

**Go**

```go
package main

import (
	"fmt"
	"os"

	absmartly "github.com/absmartly/go-sdk"
)

func main() {
	// Initialize the SDK
	clientConfig := absmartly.ClientConfig{
		Endpoint_:    "https://your-company.absmartly.io/v1",
		ApiKey_:      os.Getenv("ABSMARTLY_APIKEY"),
		Application_: "website",
		Environment_: "production",
	}

	sdkConfig := absmartly.ABSmartlyConfig{
		Client_: absmartly.CreateDefaultClient(clientConfig),
	}
	sdk := absmartly.Create(sdkConfig)

	// Create a context for this user
	contextConfig := absmartly.ContextConfig{
		Units_: map[string]string{
			"user_id": "user-12345",
		},
	}
	ctx := sdk.CreateContext(contextConfig)
	ctx.WaitUntilReady()

	// Check which variant the user is in
	treatment, err := ctx.GetTreatment("homepage_banner_experiment")
	if err != nil {
		fmt.Println("Error getting treatment:", err)
		return
	}

	if treatment == 1 {
		// Variant B: show a personalized banner
		showBanner("Welcome back, we have new deals for you!")
	} else {
		// Variant A (control): show the existing banner
		showBanner("Welcome back!")
	}

	// Use a variable set in the Web Console for more flexibility
	buttonColor, err := ctx.GetVariableValue("button.color", "blue")
	if err != nil {
		fmt.Println("Error getting variable:", err)
		return
	}
	setButtonColor(buttonColor)

	// Track when the user completes a key action
	ctx.Track("cta_click", map[string]interface{}{
		"page": "homepage",
	})

	// Track a purchase with revenue data
	ctx.Track("purchase", map[string]interface{}{
		"revenue":    49.99,
		"item_count": 3,
	})

	// Close the context to flush remaining events
	ctx.Close()
}
```

**.NET**

```csharp
using ABSmartly;
using Microsoft.Extensions.DependencyInjection;

// Initialize the SDK
var serviceProvider = new ServiceCollection()
    .AddHttpClient()
    .BuildServiceProvider();

var httpClientFactory = serviceProvider
    .GetRequiredService<IHttpClientFactory>();

var abSdk = new ABSdk(
    new ABSdkHttpClientFactory(httpClientFactory),
    new ABSmartlyServiceConfiguration
    {
        Environment = "production",
        Application = "website",
        Endpoint = "https://your-company.absmartly.io/v1",
        ApiKey = "YOUR-API-KEY"
    });

// Create a context for this user
var config = new ContextConfig()
    .SetUnit("user_id", "user-12345");

// Using the disposable pattern ensures events are flushed on close
using var context = abSdk.CreateContext(config);
await context.WaitUntilReady();

// Check which variant the user is in
if (context.GetTreatment("homepage_banner_experiment") == 1)
{
    // Variant B: show a personalized banner
    ShowBanner("Welcome back, we have new deals for you!");
}
else
{
    // Variant A (control): show the existing banner
    ShowBanner("Welcome back!");
}

// Use a variable set in the Web Console for more flexibility
var buttonColor = context.GetVariableValue("button.color", "blue");
SetButtonColor(buttonColor);

// Track when the user completes a key action
context.Track("cta_click", new Dictionary<string, object>
{
    { "page", "homepage" }
});

// Track a purchase with revenue data
context.Track("purchase", new Dictionary<string, object>
{
    { "revenue", 49.99 },
    { "item_count", 3 }
});
// Context is automatically closed and flushed by the using statement
```

**PHP**

```php
<?php

use \ABSmartly\SDK\SDK;
use \ABSmartly\SDK\Context\ContextConfig;

// Initialize the SDK
$sdk = SDK::createWithDefaults(
    endpoint: 'https://your-company.absmartly.io/v1',
    apiKey: 'YOUR-API-KEY',
    environment: 'production',
    application: 'website',
);

// Create a context for this user
$contextConfig = new ContextConfig();
$contextConfig->setUnit('user_id', 'user-12345');
$context = $sdk->createContext($contextConfig);

// Check which variant the user is in
$treatment = $context->getTreatment('homepage_banner_experiment');

if ($treatment === 1) {
    // Variant B: show a personalized banner
    showBanner('Welcome back, we have new deals for you!');
} else {
    // Variant A (control): show the existing banner
    showBanner('Welcome back!');
}

// Use a variable set in the Web Console for more flexibility
$buttonColor = $context->getVariableValue('button.color', 'blue');
setButtonColor($buttonColor);

// Track when the user completes a key action
$context->track('cta_click', (object) ['page' => 'homepage']);

// Track a purchase with revenue data
$context->track('purchase', (object) [
    'revenue' => 49.99,
    'item_count' => 3,
]);

// Close the context to flush remaining events
$context->close();
```

**Ruby**

```ruby
require "absmartly"

# Initialize the SDK
Absmartly.configure_client do |config|
  config.endpoint = "https://your-company.absmartly.io/v1"
  config.api_key = "YOUR-API-KEY"
  config.application = "website"
  config.environment = "production"
end

# Create a context for this user
context_config = Absmartly.create_context_config
context_config.units = { user_id: "user-12345" }
context = Absmartly.create_context(context_config)
context.wait_until_ready

# Check which variant the user is in
treatment = context.treatment("homepage_banner_experiment")

if treatment == 1
  # Variant B: show a personalized banner
  show_banner("Welcome back, we have new deals for you!")
else
  # Variant A (control): show the existing banner
  show_banner("Welcome back!")
end

# Use a variable set in the Web Console for more flexibility
button_color = context.variable_value("button.color", "blue")
set_button_color(button_color)

# Track when the user completes a key action
context.track("cta_click", { page: "homepage" })

# Track a purchase with revenue data
context.track("purchase", {
  revenue: 49.99,
  item_count: 3,
})

# Close the context to flush remaining events
context.close
```

**Flutter/Dart**

```dart
import 'package:absmartly_sdk/absmartly_sdk.dart';

void main() async {
  // Initialize the SDK
  final ClientConfig clientConfig = ClientConfig()
    ..setEndpoint("https://your-company.absmartly.io/v1")
    ..setAPIKey("YOUR-API-KEY")
    ..setApplication("website")
    ..setEnvironment("production");

  final ABSmartly sdk = ABSmartly(
    ABSmartlyConfig.create()
        .setClient(Client.create(clientConfig)),
  );

  // Create a context for this user
  final ContextConfig contextConfig = ContextConfig.create()
      .setUnit("user_id", "user-12345");
  final Context context = await sdk
      .createContext(contextConfig)
      .waitUntilReady();

  // Check which variant the user is in
  final int treatment =
      await context.getTreatment("homepage_banner_experiment");

  if (treatment == 1) {
    // Variant B: show a personalized banner
    showBanner("Welcome back, we have new deals for you!");
  } else {
    // Variant A (control): show the existing banner
    showBanner("Welcome back!");
  }

  // Use a variable set in the Web Console for more flexibility
  final String buttonColor =
      await context.getVariableValue("button.color", "blue");
  setButtonColor(buttonColor);

  // Track when the user completes a key action
  context.track("cta_click", {"page": "homepage"});

  // Track a purchase with revenue data
  context.track("purchase", {
    "revenue": 49.99,
    "item_count": 3,
  });

  // Close the context to flush remaining events
  await context.close();
}
```

## Next steps

You now have everything you need to run a basic experiment. When you're ready to go deeper:

* [Managing the Context Lifecycle](https://docs.absmartly.com/docs/APIs-and-SDKs/SDK-Documentation/managing-the-context-lifecycle.md) covers when to close contexts, refresh experiment data, and flush events.
* [Pre-Fetching Context Data](https://docs.absmartly.com/docs/APIs-and-SDKs/SDK-Documentation/pre-fetching-context-data.md) shows how to fetch experiment data on the server and pass it to the client to avoid flickering.
* [Targeting Users and Controlling Assignments](https://docs.absmartly.com/docs/APIs-and-SDKs/SDK-Documentation/targeting-and-audiences.md) shows how to pass user attributes, use custom assignments, and override variants during QA.
* [Checking Variants Without Triggering Exposures](https://docs.absmartly.com/docs/APIs-and-SDKs/SDK-Documentation/checking-variants-without-exposures.md) explains peeking, which is useful for pre-loading assets or debugging.
