---
title: Track dynamic variables - Capacitor
description: Track dynamic variables with the Contentsquare SDKs and Capacitor
lastUpdated: 09 October 2025
source_url:
  html: https://docs.contentsquare.com/en/capacitor/track-dynamic-variables/
  md: https://docs.contentsquare.com/en/capacitor/track-dynamic-variables/index.md
---

## General principles

### Usage

Dynamic variables are additional information on the session that can be used to segment sessions.

For example, they can include information on the A/B Test variations displayed to the current user.

### Limits

#### On the server side

* It is possible to save up to 40 distinct dynamic variable keys per screen view. If more are received, only the first 40 keys will be kept.
* If you are using the same key twice, the last value associated with the key will be collected.

#### On the SDK side

* Every dynamic variable is composed of a pair of key (max. 512 characters) and value (max. 255 characters string or a whole number between 0 and 232 - 1 ). The SDK will trim the exceeding characters.
* If key or value is empty, the SDK will send the string "cs-empty".

## Defining dynamic variables

To define and send a dynamic variable, use the API below. For each dynamic variable, we send:

* Key (`string`, mandatory)
* Value (`string` or `integer`, mandatory)

Note that if you send an integer as a value, it has indeed to be an integer. A null or a float will not be accepted as a valid parameter, and will result in an error.

```javascript
import { ContentsquarePlugin, DynamicVarItem } from '@contentsquare/capacitor-plugin';


const myDynamicVar: DynamicVarItem = {
    dynVarKey: 'myKey',
    dynVarValue: 32,
}


ContentsquarePlugin.sendDynamicVar(myDynamicVar)
  .catch(err => {
        // Handle Error
    });
```

## Sending a dynamic variable for each session

You may want to send a dynamic variable for each session (like the user's country or store). While **triggering the dynamic variable at app launch will cover most cases, it will not be enough**. A session can start when the app is put in foreground, after staying in background for more than 30 minutes. See [Session definition](https://docs.contentsquare.com/en/capacitor/how-the-sdk-works/#session-definition) for more information.

That is why we also recommend **sending such dynamic variable every time the app enters foreground**.

You can use the [`appStateChange` event ↗](https://capacitorjs.com/docs/apis/app) triggered by Capacitor to detect foreground and trigger a dynamic variable.
