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

# Variables

> Read session and experiment context with window._pgnt.variables()

`window._pgnt.variables()` returns a synchronous snapshot of the current visitor's pagent context: session identifiers, the goal you ask about, and the active experiment the visitor is enrolled in. Use it to read identifiers on the page, link backend conversions to a session, or build readable experiment names for your own analytics or naming tools.

Unlike the [`pagent:event` bus](/docs/guides/tracking/event-listener), which pushes events to you as they happen, `variables()` is a pull-based call you make whenever you need the current values.

> **Note:** Call this after the pagent SDK has initialized. Experiment fields are populated once a session is active for an enrolled visitor.

## Call Signature

```javascript theme={"dark"}
window._pgnt.variables()
window._pgnt.variables("goal_label")
```

The argument is optional. Call it with no argument to read session and experiment context; this is all you need for [server-side events](/docs/guides/tracking/server-side-events). Passing a goal label (for example `"purchase"`) additionally resolves a `conversion_id` for a Programmatic goal, which only the deprecated Conversions API uses.

## Fields

| Field | Type | Description |
| - | - | - |
| `session_id` | `string \| null` | The current browser session identifier. |
| `user_id` | `string \| null` | The visitor's persistent user identifier, stable across sessions for the same browser. |
| `visit_id` | `string \| null` | The current page view identifier. |
| `conversion_id` | `string \| null` | Deprecated. UUID of the Programmatic goal matching the label passed as an argument, or `null` if no label is passed or no goal matches. |
| `experiment_id` | `string \| null` | The experiment UUID. |
| `experiment_numeric_id` | `number \| null` | The sequential test number shown in the pagent dashboard as "Test #N" (for example, `27`). Useful for building human-readable names. |
| `variation_id` | `string \| null` | The UUID of the variation assigned to this visitor. |
| `is_control` | `boolean \| null` | `true` if the visitor is in the control group, `false` if in a variant. |
| `endpoint_path` | `string \| null` | The path the test is configured to target, for example `"/pricing"`. For tests targeting a URL pattern (glob), this may be the pattern rather than the exact visited URL. Use `window.location.pathname` if you need the exact current path. |

## Null Semantics

All experiment fields (`experiment_id`, `experiment_numeric_id`, `variation_id`, `is_control`, `endpoint_path`) return `null` when there is no active pagent test on the current page. This includes analytics-only sessions and visitors who are not enrolled in any running test.

`conversion_id` is `null` unless you pass a goal label that matches a configured goal.

> **Important:** Always null-check these fields before composing a name or sending them onward. Recording them unconditionally will produce strings like `"pagent Test null /null"` in your reports.

## Building Readable Experiment Names

The experiment fields let you construct a single machine-readable name that maps back to what you see in the pagent dashboard. Many analytics and naming tools expect one compact token with **no spaces**, so the example below leads with the human-readable parts (the test number) and appends the raw UUIDs at the end.

Wire it to any element on your page, for example a conversion button, and call `convert()` when the visitor acts:

```html theme={"dark"}
<button onclick="convert()">Buy now</button>

<script>
  function convert() {
    var ctx = window._pgnt.variables();

    // Guard: do nothing if no active test is running for this visitor.
    if (
      ctx.experiment_numeric_id == null ||
      ctx.is_control == null
    ) {
      return;
    }

    var experimentName =
      "experiment-" + ctx.experiment_numeric_id +
      "-control-" + ctx.is_control +
      "-eid-" + ctx.experiment_id +
      "-vid-" + ctx.variation_id;

    // Pass experimentName to your analytics or naming tool here.
    // e.g. "experiment-27-control-true-eid-3f2a...-vid-9b1c..."
    console.log(experimentName);
  }
</script>
```

**Notes on the example:**

* Human-readable parts come first (`experiment_numeric_id`, `is_control`) so the name is recognizable at a glance; the UUIDs (`experiment_id`, `variation_id`) are appended for exact reference.
* The output contains no spaces, so it can be used directly as an event name or identifier.

## Server-Side Events

Pass the output of `variables()` to your backend and send it as `session_data` to link backend events to the visitor's session. See [Server-side events](/docs/guides/tracking/server-side-events) for the full flow.
